Fallback to using `ls` if `/bin/ls` does not exist (#22)

This commit is contained in:
Tim Byrne 2016-08-25 07:19:43 -05:00
parent ce0acf1505
commit 60eb4c415f
No known key found for this signature in database
GPG Key ID: 6CBE24C2FD8CF76E
2 changed files with 80 additions and 3 deletions

66
test/003_ls_program.bats Normal file
View File

@ -0,0 +1,66 @@
load common
load_fixtures
@test "Default /bin/ls" {
echo "
By default, the value of LS_PROGRAM should be /bin/ls
"
# shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
status=0
output=$( require_ls; echo "$LS_PROGRAM" ) || {
status=$?
true
}
echo "output=$output"
[ "$status" == 0 ]
[ "$output" = "/bin/ls" ]
}
@test "Fallback on 'ls'" {
echo "
When LS_PROGRAM doesn't exist, use 'ls'
"
# shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
status=0
LS_PROGRAM="/ls/missing"
output=$( require_ls; echo "$LS_PROGRAM" ) || {
status=$?
true
}
echo "output=$output"
[ "$status" == 0 ]
[ "$output" = "ls" ]
}
@test "Fail if ls isn't in PATH" {
echo "
When LS_PROGRAM doesn't exist, use 'ls'
"
# shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
status=0
LS_PROGRAM="/ls/missing"
savepath="$PATH"
# shellcheck disable=SC2123
PATH=
output=$( require_ls 2>&1; echo "$LS_PROGRAM" ) || {
status=$?
true
}
PATH="$savepath"
echo "output=$output"
[ "$status" != 0 ]
[[ "$output" =~ functionality\ requires\ .ls.\ to\ be\ installed ]]
}

17
yadm
View File

@ -30,6 +30,7 @@ YADM_ENCRYPT="encrypt"
YADM_ARCHIVE="files.gpg"
GPG_PROGRAM="gpg"
LS_PROGRAM="/bin/ls"
#; flag when something may have changes (which prompts auto actions to be performed)
CHANGES_POSSIBLE=0
@ -231,6 +232,7 @@ function encrypt() {
require_gpg
require_encrypt
require_ls
#; process relative to YADM_WORK
YADM_WORK=$(git config core.worktree)
@ -253,13 +255,13 @@ function encrypt() {
GLOBS=()
while IFS='' read -r glob || [ -n "$glob" ]; do
if [[ ! $glob =~ ^# ]] ; then
GLOBS=("${GLOBS[@]}" $(eval /bin/ls "$glob" 2>/dev/null))
GLOBS=("${GLOBS[@]}" $(eval "$LS_PROGRAM" "$glob" 2>/dev/null))
fi
done < "$YADM_ENCRYPT"
#; report which files will be encrypted
echo "Encrypting the following files:"
ls -1 "${GLOBS[@]}"
"$LS_PROGRAM" -1 "${GLOBS[@]}"
echo
#; encrypt all files which match the globs
@ -378,6 +380,8 @@ function list() {
function perms() {
require_ls
#; TODO: prevent repeats in the files changed
#; process relative to YADM_WORK
@ -406,7 +410,7 @@ function perms() {
if [ -f "$YADM_ENCRYPT" ] ; then
while IFS='' read -r glob || [ -n "$glob" ]; do
if [[ ! $glob =~ ^# ]] ; then
GLOBS=("${GLOBS[@]}" $(eval /bin/ls "$glob" 2>/dev/null))
GLOBS=("${GLOBS[@]}" $(eval "$LS_PROGRAM" "$glob" 2>/dev/null))
fi
done < "$YADM_ENCRYPT"
fi
@ -592,6 +596,13 @@ function require_gpg() {
function require_repo() {
[ -d "$YADM_REPO" ] || error_out "Git repo does not exist. did you forget to run 'init' or 'clone'?"
}
function require_ls() {
if [ ! -f "$LS_PROGRAM" ] ; then
command -v ls >/dev/null 2>&1 || \
error_out "This functionality requires 'ls' to be installed at '$LS_PROGRAM' or listed in your \$PATH"
LS_PROGRAM=ls
fi
}
#; ****** Main processing (when not unit testing) ******