From 1548063b6ac54affecccc19872b1ffeed64f0887 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 22 Mar 2020 13:44:01 +0100 Subject: Work out (most?) issues reported by shellcheck --- bin/rstar | 4 ++- lib/actions/dist.bash | 9 ++++--- lib/actions/fetch.bash | 22 ++++++++++------ lib/actions/install.bash | 19 ++++++++++---- lib/logging.bash | 4 +-- lib/main.bash | 6 ++--- lib/util.bash | 67 +++++++++++++++++++++++++++++------------------- 7 files changed, 82 insertions(+), 49 deletions(-) diff --git a/bin/rstar b/bin/rstar index 1049cd9..27c71ca 100755 --- a/bin/rstar +++ b/bin/rstar @@ -1,6 +1,8 @@ #!/bin/sh -export BASEDIR=$(CDPATH="" cd -- "$(dirname -- "$0")/.." && pwd -P) +BASEDIR="$(CDPATH="" cd -- "$(dirname -- "$0")/.." && pwd -P)" + +export BASEDIR main() { if ! command -v bash > /dev/null diff --git a/lib/actions/dist.bash b/lib/actions/dist.bash index 3b06b4f..5da5e49 100644 --- a/lib/actions/dist.bash +++ b/lib/actions/dist.bash @@ -11,7 +11,7 @@ action() { info "Creating distribution contents at $WORKDIR" - cd -- "$BASEDIR" + chgdir "$BASEDIR" # Include files from this project for file in $(git ls-files) @@ -26,7 +26,7 @@ action() { done # Add a MANIFEST.txt - cd -- "$WORKDIR" + chgdir "$WORKDIR" find . > MANIFEST.txt # Tar it all up into a distribution tarball @@ -35,10 +35,13 @@ action() { local tarball="$BASEDIR/dist/rakudo-star-$version.tar.gz" mkdir -p -- "$(dirname "$tarball")" - cd -- "$BASEDIR/tmp" + chgdir "$BASEDIR/tmp" tar czf "$tarball" "rakudo-star-$version" + # TODO: Create checksums + # TODO: Create PGP signature + info "Distribution tarball available at $tarball" } diff --git a/lib/actions/fetch.bash b/lib/actions/fetch.bash index c489608..2b932be 100644 --- a/lib/actions/fetch.bash +++ b/lib/actions/fetch.bash @@ -1,6 +1,6 @@ #!/usr/bin/env bash -RSTAR_DEPS_BIN=( +RSTAR_DEPS_BIN+=( awk curl git @@ -20,22 +20,26 @@ action() { mkdir -p "$BASEDIR/dist/src/modules" # Download all modules available over http - list_modules "http" | while read -r name proto url prefix + list_modules "http" | while read -r name _ url prefix do download_module_http "$name" "$url" "$prefix" done # Download all modules available over git - list_modules "git" | while read -r name proto url ref + list_modules "git" | while read -r name _ url ref do download_module_git "$name" "$url" "$ref" done } download_core() { - local version="$(config_etc_kv "dist_$1.txt" "version")" - local source="$(echo "$(config_etc_kv "dist_$1.txt" "url")" | sed "s/%s/$version/g")" - local destination="$BASEDIR/dist/src/core/$1-$version" + local version + local source + local destination + + version="$(config_etc_kv "dist_$1.txt" "version")" + source="$(config_etc_kv "dist_$1.txt" "url" | sed "s/%s/$version/g")" + destination="$BASEDIR/dist/src/core/$1-$version" if [[ -d $destination ]] then @@ -76,6 +80,8 @@ download_module_http() { local url=$2 local prefix=$3 local destination="$BASEDIR/dist/src/modules/$name" + local tarball + local extracted if [[ -d "$destination" ]] then @@ -83,8 +89,8 @@ download_module_http() { return 0 fi - local tarball="$(fetch "$url")" - local extracted="$(tempdir)" + tarball="$(fetch "$url")" + extracted="$(tmpdir)" notice "Extracting $tarball into $extracted" tar xzf "$tarball" -C "$extracted" diff --git a/lib/actions/install.bash b/lib/actions/install.bash index 30b88dd..b2faefd 100644 --- a/lib/actions/install.bash +++ b/lib/actions/install.bash @@ -14,12 +14,15 @@ RSTAR_DEPS_PERL+=( action() { local OPTIND + local prefix_absolute + local modules while getopts ":b:p:" opt do case "$opt" in b) RSTAR_BACKEND=$OPTARG ;; p) RSTAR_PREFIX=$OPTARG ;; + *) emerg "Invalid option specified: $opt" ;; esac done @@ -27,7 +30,7 @@ action() { # TODO: Check if binaries are available mkdir -p -- "$RSTAR_PREFIX" - local prefix_absolute="$(CDPATH="" cd -- "$RSTAR_PREFIX" && pwd -P)" + prefix_absolute="$(CDPATH="" cd -- "$RSTAR_PREFIX" && pwd -P)" info "Installing Raku in $prefix_absolute" @@ -45,8 +48,11 @@ action() { # Install community modules failed_modules=() + modules="$(tmpfile)" - for module in $(awk '/^[^#]/ {print $1}' "$BASEDIR/etc/modules.txt") + awk '/^[^#]/ {print $1}' "$BASEDIR/etc/modules.txt" > "$modules" + + while read -r module do info "Installing $module" @@ -54,10 +60,10 @@ action() { && continue failed_modules+=("$module") - done + done < "$modules" # Show a list of all modules that failed to install - if [[ $failed_modules ]] + if [[ ${failed_modules[*]} ]] then crit "The following modules failed to install:" @@ -68,6 +74,7 @@ action() { fi # Friendly message + # TODO: Add information on the time it took" info "Rakudo Star has been installed into $prefix_absolute!" info "You may need to add the following paths to your \$PATH:" info " $prefix_absolute/bin" @@ -113,7 +120,9 @@ build_rakudo() { build_prepare() { local source="$1" - local destination="$(tempdir)" + local destination + + destination="$(tmpdir)" notice "Using $destination as working directory" diff --git a/lib/logging.bash b/lib/logging.bash index 33ead42..ad0bedd 100644 --- a/lib/logging.bash +++ b/lib/logging.bash @@ -7,18 +7,18 @@ log() { local OPTIND local color - local format="[%s] %s\n" while getopts ":c:" opt do case "$opt" in c) color=$OPTARG ;; + *) alert "Unused argument specified: $opt" ;; esac done shift $(( OPTIND - 1 )) - printf "$color[%s] %s\e[0m\n" "$(date +%FT%T)" "$*" >&2 + printf "${color}[%s] %s\e[0m\n" "$(date +%FT%T)" "$*" >&2 } debug() { diff --git a/lib/main.bash b/lib/main.bash index 1d57dcc..cf4c1a3 100644 --- a/lib/main.bash +++ b/lib/main.bash @@ -87,7 +87,7 @@ depcheck_bin() { missing+=("$tool") done - if [[ $missing ]] + if [[ ${missing[*]} ]] then alert "Some required tools are missing:" @@ -113,11 +113,11 @@ depcheck_perl() { missing+=("$tool") done - if [[ $missing ]] + if [[ ${missing[*]} ]] then alert "Some required Perl modules are missing:" - for modules in "${missing[@]}" + for module in "${missing[@]}" do alert " $module" done diff --git a/lib/util.bash b/lib/util.bash index 025aeba..8ca1a5e 100644 --- a/lib/util.bash +++ b/lib/util.bash @@ -1,5 +1,28 @@ #!/usr/bin/env bash +# Change the working directory. In usage, this is the same as using cd, +# however, it will make additional checks to ensure everything is going fine. +chgdir() { + cd -- "$1" || die "Failed to change directory to $1" +} + +# Read a particular value from a key/value configuration file. Using this +# function introduces a dependency on awk. +config_etc_kv() { + local file="$BASEDIR/etc/$1" + shift + + if [[ ! -f $file ]] + then + crit "Tried to read value for $1 from $file, but $file does not exist" + return + fi + + debug "Reading value for $1 from $file" + + awk -F= '$1 == "'"$1"'" { print $NF }' "$file" +} + # Log a message as error, and exit the program. This is intended for serious # issues that prevent the script from running correctly. The exit code can be # specified with -i, or will default to 1. @@ -11,13 +34,14 @@ die() { do case "$opt" in i) code=$OPTARG ;; + *) alert "Unused argument specified: $opt" ;; esac done shift $(( OPTIND -1 )) alert "$@" - exit ${code:-1} + exit "${code:-1}" } # Fetch a file from an URL. Using this function introduces a dependency on curl. @@ -29,12 +53,13 @@ fetch() { do case "$opt" in o) buffer=$OPTARG ;; + *) alert "Unused argument specified: $opt" ;; esac done shift $(( OPTIND -1 )) - [[ -z $buffer ]] && buffer="$(tempfile)" + [[ -z $buffer ]] && buffer="$(tmpfile)" notice "Downloading $1 to $buffer" @@ -49,27 +74,12 @@ fetch() { return $exit_code } -# Read a particular value from a key/value configuration file. Using this -# function introduces a dependency on awk. -config_etc_kv() { - local file="$BASEDIR/etc/$1" - shift - - if [[ ! -f $file ]] - then - crit "Tried to read value for $1 from $file, but $file does not exist" - return - fi - - debug "Reading value for $1 from $file" - - awk -F= '$1 == "'"$1"'" { print $NF }' "$file" -} - # Create a temporary directory. Similar to tempfile, but you'll get a directory # instead. -tempdir() { - local dir="$(mktemp -d)" +tmpdir() { + local dir + + dir="$(mktemp -d)" # Ensure the file was created succesfully if [[ ! -d "$dir" ]] @@ -79,26 +89,28 @@ tempdir() { debug "Temporary file created at $dir" - printf "$dir" + printf "%s" "$dir" } # Create a temporary file. In usage, this is no different from mktemp itself, # however, it will apply additional checks to ensure everything is going # correctly, and the files will be cleaned up automatically at the end. -tempfile() { +tmpfile() { local OPTIND local extension="tmp" + local file while getopts ":x:" opt do case "$opt" in x) extension=$OPTARG ;; + *) alert "Unused argument specified: $opt" ;; esac done shift $(( OPTIND -1 )) - local file="$(mktemp --suffix ".$extension")" + file="$(mktemp --suffix ".$extension")" # Ensure the file was created succesfully if [[ ! -f "$file" ]] @@ -108,11 +120,12 @@ tempfile() { debug "Temporary file created at $file" - printf "$file" + printf "%s" "$file" } +export -f chgdir export -f config_etc_kv export -f die export -f fetch -export -f tempdir -export -f tempfile +export -f tmpdir +export -f tmpfile -- cgit v1.1