diff --git a/bin/omarchy-sudo-passwordless b/bin/omarchy-sudo-passwordless index badf65ea477f..6f73dca25934 100755 --- a/bin/omarchy-sudo-passwordless +++ b/bin/omarchy-sudo-passwordless @@ -1,7 +1,8 @@ #!/bin/bash -p -# omarchy:summary=Toggle passwordless sudo for the current user. +# omarchy:summary=Toggle passwordless sudo for the current user, asking how long when turning it on. # omarchy:args=[MINUTES|permanent|--active|--disable] +# omarchy:examples=omarchy sudo passwordless | omarchy sudo passwordless 30 | omarchy sudo passwordless permanent | omarchy sudo passwordless --disable # omarchy:requires-sudo=true if [[ $- != *p* && ${BASH_SOURCE[0]} == "$0" ]]; then @@ -33,8 +34,17 @@ readonly INSTALLED_SELF=/usr/bin/omarchy-sudo-passwordless readonly STATUS_INACTIVE=3 usage() { - echo "Usage: omarchy-sudo-passwordless [MINUTES|permanent|--active|--disable]" >&2 - echo "MINUTES must be between 1 and $MAX_MINUTES." >&2 + echo "Usage: omarchy-sudo-passwordless [MINUTES|permanent|--active|--disable]" + echo " (no args) turn passwordless sudo off when it is on; otherwise ask how long" + echo " to allow it (15 minutes, 1 hour, 1 day, or permanently)" + echo " MINUTES allow it for 1-$MAX_MINUTES minutes, or change how long an active grant lasts" + echo " permanent allow it until disabled, surviving reboots and updates" + echo " --disable turn it off without asking" + echo " --active exit 0 while it is on and 1 otherwise, printing nothing" +} + +invalid_usage() { + usage >&2 exit 1 } @@ -498,6 +508,10 @@ show_enabled() { } case "${1:-}" in + -h|--help) + usage + exit 0 + ;; __status|__enable|__disable|__expire|__cleanup-all|__package-removing|__migrate|__migration-complete) action=$1 shift @@ -511,7 +525,7 @@ esac # listpw settings require the root-status fallback. Neither path prompts or # trusts cached credentials as evidence of passwordless access. if [[ ${1:-} == "--active" ]]; then - (($# == 1)) || usage + (($# == 1)) || invalid_usage uid=$(/usr/bin/id -u) if policy=$(/usr/bin/sudo -n -N -l -l -- "$INSTALLED_SELF" __status "$uid" 2>/dev/null); then /usr/bin/grep -q '!authenticate' <<<"$policy" @@ -527,9 +541,9 @@ if [[ ${1:-} == "--active" ]]; then exit fi -(($# <= 1)) || usage +(($# <= 1)) || invalid_usage minutes=${1:-} -(($# == 0)) || [[ $minutes == "--disable" ]] || valid_duration "$minutes" || usage +(($# == 0)) || [[ $minutes == "--disable" ]] || valid_duration "$minutes" || invalid_usage uid=$(/usr/bin/id -u) valid_uid "$uid" || { echo "omarchy-sudo-passwordless: cannot grant passwordless sudo to this account" >&2 diff --git a/test/shell.d/nopasswd-sudo-expiry-test.sh b/test/shell.d/nopasswd-sudo-expiry-test.sh index bc123a6583ae..6e09439949bb 100755 --- a/test/shell.d/nopasswd-sudo-expiry-test.sh +++ b/test/shell.d/nopasswd-sudo-expiry-test.sh @@ -286,6 +286,13 @@ done assert_status 1 env TEST_POLICY_FAILURE=1 /usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" --active pass "menu probe reads policy tags without executing a privileged action or changing the timestamp" +: >"$test_tmp/commands" +/usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" --help >"$test_tmp/public.log" +grep -q '^ (no args)' "$test_tmp/public.log" || fail "help must explain the bare invocation" +[[ ! -s $test_tmp/commands ]] || fail "help must not run sudo or gum" +assert_status 1 /usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" 0 2>/dev/null +pass "help prints the modes and exits cleanly while invalid durations still fail" + for status in 0 3; do : >"$test_tmp/commands" TEST_STATUS=$status /usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" --disable >"$test_tmp/public.log" 2>&1