Resolve shellcheck errors in yadm

This commit is contained in:
Tim Byrne 2016-04-05 08:52:21 -05:00
parent b3209de4dc
commit b662b31cd4
1 changed files with 30 additions and 20 deletions

50
yadm
View File

@ -33,7 +33,7 @@ function main() {
require_git require_git
#; create the YADM_DIR if it doesn't exist yet #; create the YADM_DIR if it doesn't exist yet
[ -d "$YADM_DIR" ] || mkdir -p $YADM_DIR [ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR"
#; parse command line arguments #; parse command line arguments
internal_commands="^(alt|clean|clone|config|decrypt|encrypt|help|init|list|perms|version)$" internal_commands="^(alt|clean|clone|config|decrypt|encrypt|help|init|list|perms|version)$"
@ -43,10 +43,10 @@ function main() {
elif [[ "$1" =~ $internal_commands ]] ; then elif [[ "$1" =~ $internal_commands ]] ; then
#; for internal commands, process all of the arguments #; for internal commands, process all of the arguments
YADM_COMMAND="$1" YADM_COMMAND="$1"
YADM_ARGS="" YADM_ARGS=()
shift shift
while [[ $# > 0 ]] ; do while [[ $# -gt 0 ]] ; do
key="$1" key="$1"
case $key in case $key in
-a) #; used by list() -a) #; used by list()
@ -69,17 +69,13 @@ function main() {
shift shift
;; ;;
*) #; any unhandled arguments *) #; any unhandled arguments
if [ -z "$YADM_ARGS" ] ; then YADM_ARGS+=("$1")
YADM_ARGS="$1"
else
YADM_ARGS+=" $1"
fi
;; ;;
esac esac
shift shift
done done
[ ! -d $YADM_WORK ] && error_out "Work tree does not exist: [$YADM_WORK]" [ ! -d "$YADM_WORK" ] && error_out "Work tree does not exist: [$YADM_WORK]"
$YADM_COMMAND "$YADM_ARGS" $YADM_COMMAND "${YADM_ARGS[@]}"
else else
#; any other commands are simply passed through to git #; any other commands are simply passed through to git
git_command "$@" git_command "$@"
@ -107,7 +103,10 @@ function alt() {
#; process relative to YADM_WORK #; process relative to YADM_WORK
YADM_WORK=$(git config core.worktree) YADM_WORK=$(git config core.worktree)
cd $YADM_WORK cd "$YADM_WORK" || {
debug "Alternates not processed, unable to cd to $YADM_WORK"
return
}
#; only be noisy if the "alt" command was run directly #; only be noisy if the "alt" command was run directly
[ "$YADM_COMMAND" = "alt" ] && loud="YES" [ "$YADM_COMMAND" = "alt" ] && loud="YES"
@ -185,7 +184,7 @@ function config() {
echo TODO: Print help about available yadm configurations echo TODO: Print help about available yadm configurations
else else
#; operate on the yadm configuration file #; operate on the yadm configuration file
git config --file="$YADM_CONFIG" $@ git config --file="$YADM_CONFIG" "$@"
fi fi
} }
@ -222,7 +221,10 @@ function encrypt() {
#; process relative to YADM_WORK #; process relative to YADM_WORK
YADM_WORK=$(git config core.worktree) YADM_WORK=$(git config core.worktree)
cd $YADM_WORK cd "$YADM_WORK" || {
debug "Encryption not processed, unable to cd to $YADM_WORK"
return
}
#; build a list of globs from YADM_ENCRYPT #; build a list of globs from YADM_ENCRYPT
GLOBS=() GLOBS=()
@ -233,7 +235,7 @@ function encrypt() {
done < "$YADM_ENCRYPT" done < "$YADM_ENCRYPT"
#; encrypt all files which match the globs #; encrypt all files which match the globs
tar -cv ${GLOBS[@]} | gpg --yes -c --output "$YADM_ARCHIVE" tar -cv "${GLOBS[@]}" | gpg --yes -c --output "$YADM_ARCHIVE"
if [ $? = 0 ]; then if [ $? = 0 ]; then
echo "Wrote new file: $YADM_ARCHIVE" echo "Wrote new file: $YADM_ARCHIVE"
else else
@ -246,7 +248,7 @@ function encrypt() {
if [[ $archive_status =~ $archive_regex ]] ; then if [[ $archive_status =~ $archive_regex ]] ; then
echo "It appears that $YADM_ARCHIVE is not tracked by yadm's repository." echo "It appears that $YADM_ARCHIVE is not tracked by yadm's repository."
echo "Would you like to add it now? (y/n)" echo "Would you like to add it now? (y/n)"
read answer read -r answer
if [[ $answer =~ ^[yY]$ ]] ; then if [[ $answer =~ ^[yY]$ ]] ; then
git add "$YADM_ARCHIVE" git add "$YADM_ARCHIVE"
fi fi
@ -335,7 +337,10 @@ function list() {
#; process relative to YADM_WORK when --all is specified #; process relative to YADM_WORK when --all is specified
if [ -n "$LIST_ALL" ] ; then if [ -n "$LIST_ALL" ] ; then
YADM_WORK=$(git config core.worktree) YADM_WORK=$(git config core.worktree)
cd $YADM_WORK cd "$YADM_WORK" || {
debug "List not processed, unable to cd to $YADM_WORK"
return
}
fi fi
#; list tracked files #; list tracked files
@ -349,7 +354,10 @@ function perms() {
#; process relative to YADM_WORK #; process relative to YADM_WORK
YADM_WORK=$(git config core.worktree) YADM_WORK=$(git config core.worktree)
cd $YADM_WORK cd "$YADM_WORK" || {
debug "Perms not processed, unable to cd to $YADM_WORK"
return
}
GLOBS=() GLOBS=()
@ -376,6 +384,8 @@ function perms() {
fi fi
#; remove group/other permissions from collected globs #; remove group/other permissions from collected globs
#shellcheck disable=SC2068
#(SC2068 is disabled because in this case, we desire globbing)
chmod -f go-rwx ${GLOBS[@]} >/dev/null 2>&1 chmod -f go-rwx ${GLOBS[@]} >/dev/null 2>&1
#; TODO: detect and report changing permissions in a portable way #; TODO: detect and report changing permissions in a portable way
@ -394,7 +404,7 @@ function process_global_args() {
#; global arguments are removed before the main processing is done #; global arguments are removed before the main processing is done
MAIN_ARGS=() MAIN_ARGS=()
while [[ $# > 0 ]] ; do while [[ $# -gt 0 ]] ; do
key="$1" key="$1"
case $key in case $key in
-Y|--yadm-dir) #; override the standard YADM_DIR -Y|--yadm-dir) #; override the standard YADM_DIR
@ -446,13 +456,13 @@ function configure_repo() {
function debug() { function debug() {
[ -n "$DEBUG" ] && echo -e "DEBUG: $@" [ -n "$DEBUG" ] && echo -e "DEBUG: $*"
} }
function error_out() { function error_out() {
echo -e "ERROR: $@" echo -e "ERROR: $*"
exit 1 exit 1
} }