From d7d8728b9df5d22ba88666fe17f62e8b79d6446a Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 6 Jan 2022 19:53:38 +0300 Subject: db.sh: consistent logging --- src/db.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db.sh b/src/db.sh index 5eb1697..d987e85 100644 --- a/src/db.sh +++ b/src/db.sh @@ -272,7 +272,7 @@ chmod_entries() { local mode="$1" shift - echo "Applying mode $mode to shared files..." + dump "applying mode $mode to shared files..." if [ "${#shared_paths[@]}" -ne 0 ]; then is_dry_run || chmod -- "$mode" ${shared_paths[@]+"${shared_paths[@]}"} fi -- cgit v1.2.3 From 47c8a75f97f21ae6cffa61e61e62000642f82ab7 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 6 Jan 2022 20:11:05 +0300 Subject: db.sh: add parse_mode function --- links-chmod | 2 +- links-update | 6 +++--- src/db.sh | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/links-chmod b/links-chmod index 9c7a427..8eb8849 100755 --- a/links-chmod +++ b/links-chmod @@ -79,7 +79,7 @@ parse_script_options() { update_shared_dir "$value" ;; -m|--mode) - mode="$value" + mode="$( parse_mode "$value" )" ;; *) script_usage "unrecognized parameter: $key" >&2 diff --git a/links-update b/links-update index 1e1c829..6a802bc 100755 --- a/links-update +++ b/links-update @@ -32,8 +32,6 @@ readonly script_dir src_dir="$( cd -- "$script_dir/src" && pwd )" readonly src_dir -mode='' - . "$src_dir/common.sh" . "$src_dir/os.sh" . "$src_dir/path.sh" @@ -93,7 +91,7 @@ parse_script_options() { update_shared_dir "$value" ;; -m|--mode) - mode="$value" + mode="$( parse_mode "$value" )" ;; *) script_usage "unrecognized parameter: $key" >&2 @@ -103,6 +101,8 @@ parse_script_options() { done } +mode='' + main() { parse_script_options "$@" check_symlinks_enabled diff --git a/src/db.sh b/src/db.sh index d987e85..b057202 100644 --- a/src/db.sh +++ b/src/db.sh @@ -263,6 +263,23 @@ unlink_obsolete_entries() { done } +parse_mode() { + if [ "$#" -ne 1 ]; then + echo "usage: ${FUNCNAME[0]} MODE" >&2 + return 1 + fi + + local mode="$1" + shift + + if [ -z "$mode" ]; then + dump "mode cannot be empty" >&2 + return 1 + fi + + echo "$mode" +} + chmod_entries() { if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} MODE" >&2 -- cgit v1.2.3 From a19a6d4fe1dab27daf20cd6a532f85d9bca3218c Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 6 Jan 2022 20:25:02 +0300 Subject: links-chmod: mode parameter is mandatory --- links-chmod | 33 +++++++++++++++++++++++++-------- test/test.sh | 2 +- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/links-chmod b/links-chmod index 8eb8849..9810cf6 100755 --- a/links-chmod +++ b/links-chmod @@ -5,7 +5,7 @@ # For details, see https://github.com/egor-tensin/config-links. # Distributed under the MIT License. -# usage: ./links-chmod [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-m|--mode MODE] [-n|--dry-run] +# usage: ./links-chmod [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run] MODE set -o errexit set -o nounset @@ -32,16 +32,18 @@ script_usage() { echo "$script_name: $msg" done - echo "usage: $script_name [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run] + echo "usage: $script_name [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run] MODE + MODE shared files mode (as in chmod) -h,--help show this message and exit -d,--database set database file path -s,--shared-dir set top-level shared directory path (current working directory by default) - -m,--mode shared files mode (as in chmod) -n,--dry-run don't actually do anything intrusive" } parse_script_options() { + local -a args=() + while [ "$#" -gt 0 ]; do local key="$1" shift @@ -55,12 +57,19 @@ parse_script_options() { set_dry_run continue ;; - -d|--database|-s|--shared-dir|-m|--mode) + -d|--database|-s|--shared-dir) ;; - *) + --) + break + ;; + -*) script_usage "unrecognized parameter: $key" >&2 exit 1 ;; + *) + args+=("$key") + continue + ;; esac if [ "$#" -eq 0 ]; then @@ -78,17 +87,25 @@ parse_script_options() { -s|--shared-dir) update_shared_dir "$value" ;; - -m|--mode) - mode="$( parse_mode "$value" )" - ;; *) script_usage "unrecognized parameter: $key" >&2 exit 1 ;; esac done + + args+=("$@") + + if [ "${#args[@]}" -ne 1 ]; then + script_usage "missing parameter MODE" >&2 + exit 1 + fi + + mode="$( parse_mode "${args[0]}" )" } +mode='' + main() { parse_script_options "$@" read_database diff --git a/test/test.sh b/test/test.sh index de4dd85..0e02480 100755 --- a/test/test.sh +++ b/test/test.sh @@ -359,7 +359,7 @@ $test_dest_dir/foo/2.txt->$test_src_dir/%DEST%/foo/2.txt" echo 'Verifying 1.txt (the shared file) permissions...' verify_mode 0644 "$test_src_dir/%DEST%/1.txt" - call_chmod --mode 0600 + call_chmod 0600 verify_mode 0600 "$test_src_dir/%DEST%/1.txt" } -- cgit v1.2.3