fix(cli): preserve optional argument boundaries
Preserve CLI argument boundaries and optional values
This commit is contained in:
+3
-3
@@ -53,8 +53,8 @@ cmd_config() {
|
||||
cat "$CONFIG_FILE" | sed 's/^/ /'
|
||||
;;
|
||||
set)
|
||||
local key="$3"
|
||||
local value="$4"
|
||||
local key="${3:-}"
|
||||
local value="${4:-}"
|
||||
[ -z "$key" ] || [ -z "$value" ] && {
|
||||
info "Usage: ./unleash config set KEY VALUE"
|
||||
info "Keys: WEBHOOK, LOG_LEVEL (verbose), LOG_FILE"
|
||||
@@ -63,7 +63,7 @@ cmd_config() {
|
||||
save_config "$key" "$value"
|
||||
;;
|
||||
unset)
|
||||
local key="$3"
|
||||
local key="${3:-}"
|
||||
[ -z "$key" ] && { info "Usage: ./unleash config unset KEY"; return 1; }
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
_rewrite_config "$key" "" unset || return 1
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
run_cli() {
|
||||
local script="$1"; shift
|
||||
local definitions="$BATS_TEST_TMPDIR/${script##*/}.functions"
|
||||
sed '/^main "\$@"$/d' "$script" > "$definitions"
|
||||
run "${UNLEASH_TEST_SHELL:-bash}" -u -c '
|
||||
source "$1"
|
||||
shift
|
||||
load_config() { :; }
|
||||
is_root() { return 1; }
|
||||
is_recovery() { return 1; }
|
||||
cmd_interactive_normal() { printf "interactive verbose=%s\n" "$VERBOSE"; }
|
||||
main "$@"
|
||||
' "$script" "$definitions" "$@"
|
||||
}
|
||||
|
||||
run_capture() {
|
||||
local script="$1"; shift
|
||||
local definitions="$BATS_TEST_TMPDIR/${script##*/}.functions"
|
||||
sed '/^main "\$@"$/d' "$script" > "$definitions"
|
||||
run "${UNLEASH_TEST_SHELL:-bash}" -u -c '
|
||||
source "$1"
|
||||
shift
|
||||
load_config() { :; }
|
||||
cmd_config() {
|
||||
printf "dry=%s verbose=%s log=<%s>\nargc=%s\n" "$DRY_RUN" "$VERBOSE" "$LOG_FILE" "$#"
|
||||
printf "<%s>\n" "$@"
|
||||
}
|
||||
main "$@"
|
||||
' "$script" "$definitions" "$@"
|
||||
}
|
||||
|
||||
@test "optional CLI arguments reach usage or privilege guards under nounset" {
|
||||
local entrypoint
|
||||
for entrypoint in unleash unleash-standalone.sh; do
|
||||
local script="$BATS_TEST_DIRNAME/../$entrypoint"
|
||||
run_cli "$script" audit
|
||||
[ "$status" -ne 0 ]; [[ "$output" == *"Audit needs sudo"* ]]
|
||||
run_cli "$script" --log-file
|
||||
[ "$status" -ne 0 ]; [[ "$output" == *"--log-file requires a path"* ]]
|
||||
run_cli "$script" monitor --webhook
|
||||
[ "$status" -ne 0 ]; [[ "$output" == *"--webhook requires a URL"* ]]
|
||||
run_cli "$script" config set
|
||||
[ "$status" -ne 0 ]; [[ "$output" == *"Usage: ./unleash config set KEY VALUE"* ]]
|
||||
run_cli "$script" config unset
|
||||
[ "$status" -ne 0 ]; [[ "$output" == *"Usage: ./unleash config unset KEY"* ]]
|
||||
done
|
||||
}
|
||||
|
||||
@test "CLI values retain spaces quotes empty strings and literal shell syntax" {
|
||||
local entrypoint value
|
||||
for entrypoint in unleash unleash-standalone.sh; do
|
||||
for value in 'a b $(printf injected) "quoted"' ''; do
|
||||
run_capture "$BATS_TEST_DIRNAME/../$entrypoint" config set LOG_FILE "$value"
|
||||
[ "$status" -eq 0 ]
|
||||
local expected
|
||||
expected=$(printf 'dry=false verbose=false log=<>\nargc=4\n<config>\n<set>\n<LOG_FILE>\n<%s>' "$value")
|
||||
[ "$output" = "$expected" ]
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
@test "global flags around the command persist in the command shell" {
|
||||
local entrypoint
|
||||
for entrypoint in unleash unleash-standalone.sh; do
|
||||
run_capture "$BATS_TEST_DIRNAME/../$entrypoint" --dry-run config --verbose set LOG_FILE value --log-file 'a b.log'
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == 'dry=true verbose=true log=<a b.log>'* ]]
|
||||
[[ "$output" == *'argc=4'* ]]
|
||||
done
|
||||
}
|
||||
|
||||
@test "global flags without a command preserve an empty argument list" {
|
||||
local entrypoint
|
||||
for entrypoint in unleash unleash-standalone.sh; do
|
||||
run_cli "$BATS_TEST_DIRNAME/../$entrypoint" --verbose
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = 'interactive verbose=true' ]
|
||||
done
|
||||
}
|
||||
|
||||
@test "CLI option terminator preserves values that look like global options" {
|
||||
local entrypoint
|
||||
for entrypoint in unleash unleash-standalone.sh; do
|
||||
run_capture "$BATS_TEST_DIRNAME/../$entrypoint" config set LOG_LEVEL -- --verbose
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == 'dry=false verbose=false log=<>'* ]]
|
||||
[[ "$output" == *'<--verbose>' ]]
|
||||
done
|
||||
}
|
||||
|
||||
@test "log-file rejects an empty path or another option as its missing value" {
|
||||
local entrypoint value
|
||||
for entrypoint in unleash unleash-standalone.sh; do
|
||||
for value in '' --dry-run; do
|
||||
run_cli "$BATS_TEST_DIRNAME/../$entrypoint" --log-file "$value" version
|
||||
[ "$status" -ne 0 ]
|
||||
[[ "$output" == *"--log-file requires a path"* ]]
|
||||
done
|
||||
done
|
||||
}
|
||||
@@ -371,7 +371,9 @@ cmd_monitor() {
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
install|uninstall) args+=("$1"); shift ;;
|
||||
--webhook) webhook="$2"; shift 2 ;;
|
||||
--webhook)
|
||||
if [ $# -lt 2 ] || [ -z "$2" ]; then error_exit "--webhook requires a URL"; fi
|
||||
webhook="$2"; shift 2 ;;
|
||||
*) args+=("$1"); shift ;;
|
||||
esac
|
||||
done
|
||||
@@ -531,22 +533,29 @@ cmd_interactive_normal() {
|
||||
}
|
||||
|
||||
parse_global_opts() {
|
||||
local args=()
|
||||
PARSED_ARGS=()
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--verbose) VERBOSE=true; shift ;;
|
||||
--dry-run) DRY_RUN=true; shift ;;
|
||||
--log-file) LOG_FILE="$2"; shift 2 ;;
|
||||
*) args+=("$1"); shift ;;
|
||||
--log-file)
|
||||
if [ $# -lt 2 ] || [ -z "$2" ]; then error_exit "--log-file requires a path"; fi
|
||||
case "$2" in --*) error_exit "--log-file requires a path" ;; esac
|
||||
LOG_FILE="$2"; shift 2 ;;
|
||||
--) shift; PARSED_ARGS+=("$@"); break ;;
|
||||
*) PARSED_ARGS+=("$1"); shift ;;
|
||||
esac
|
||||
done
|
||||
set -- "${args[@]}"
|
||||
echo "$@"
|
||||
}
|
||||
|
||||
main() {
|
||||
load_config
|
||||
eval "set -- $(parse_global_opts "$@")"
|
||||
parse_global_opts "$@"
|
||||
if [ "${PARSED_ARGS[0]+present}" = present ]; then
|
||||
set -- "${PARSED_ARGS[@]}"
|
||||
else
|
||||
set --
|
||||
fi
|
||||
|
||||
# Per-command help
|
||||
if [ "${2:-}" = "--help" ] || [ "${2:-}" = "-h" ]; then
|
||||
@@ -563,7 +572,7 @@ main() {
|
||||
firewall|fw) cmd_firewall ;;
|
||||
firewall-off|fw-off) cmd_firewall_off ;;
|
||||
harden) cmd_harden ;;
|
||||
audit) cmd_audit "$2" ;;
|
||||
audit) cmd_audit "${2:-}" ;;
|
||||
whitelist|wl) cmd_whitelist ;;
|
||||
backup) cmd_backup ;;
|
||||
restore) cmd_restore ;;
|
||||
@@ -581,13 +590,13 @@ main() {
|
||||
version|-v|--version) cmd_version ;;
|
||||
init) cmd_init ;;
|
||||
suggest) cmd_suggest ;;
|
||||
remediate) cmd_remediate "$2" ;;
|
||||
predict) cmd_predict "$2" ;;
|
||||
telemetry) telemetry_opt_in "$2" ;;
|
||||
remediate) cmd_remediate "${2:-}" ;;
|
||||
predict) cmd_predict "${2:-}" ;;
|
||||
telemetry) telemetry_opt_in "${2:-}" ;;
|
||||
demo) run_demo ;;
|
||||
update) do_self_update ;;
|
||||
uninstall) do_uninstall ;;
|
||||
report) generate_report "$2" ;;
|
||||
report) generate_report "${2:-}" ;;
|
||||
config) cmd_config "$@" ;;
|
||||
vpn-kill) vpn_kill_install "$@" ;;
|
||||
vpn-kill-remove) vpn_kill_remove ;;
|
||||
@@ -599,7 +608,7 @@ main() {
|
||||
install_monitor_launchdaemon ""
|
||||
success "Reinstall complete"
|
||||
;;
|
||||
discord-bot) cmd_discord_bot_install "$2" "$3" ;;
|
||||
discord-bot) cmd_discord_bot_install "${2:-}" "${3:-}" ;;
|
||||
discord-bot-stop) cmd_discord_bot_stop ;;
|
||||
discord-bot-status) cmd_discord_bot_status ;;
|
||||
help|-h|--help) show_help; exit 0 ;;
|
||||
|
||||
+25
-16
@@ -178,8 +178,8 @@ cmd_config() {
|
||||
cat "$CONFIG_FILE" | sed 's/^/ /'
|
||||
;;
|
||||
set)
|
||||
local key="$3"
|
||||
local value="$4"
|
||||
local key="${3:-}"
|
||||
local value="${4:-}"
|
||||
[ -z "$key" ] || [ -z "$value" ] && {
|
||||
info "Usage: ./unleash config set KEY VALUE"
|
||||
info "Keys: WEBHOOK, LOG_LEVEL (verbose), LOG_FILE"
|
||||
@@ -188,7 +188,7 @@ cmd_config() {
|
||||
save_config "$key" "$value"
|
||||
;;
|
||||
unset)
|
||||
local key="$3"
|
||||
local key="${3:-}"
|
||||
[ -z "$key" ] && { info "Usage: ./unleash config unset KEY"; return 1; }
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
_rewrite_config "$key" "" unset || return 1
|
||||
@@ -3015,7 +3015,9 @@ cmd_monitor() {
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
install|uninstall) args+=("$1"); shift ;;
|
||||
--webhook) webhook="$2"; shift 2 ;;
|
||||
--webhook)
|
||||
if [ $# -lt 2 ] || [ -z "$2" ]; then error_exit "--webhook requires a URL"; fi
|
||||
webhook="$2"; shift 2 ;;
|
||||
*) args+=("$1"); shift ;;
|
||||
esac
|
||||
done
|
||||
@@ -3175,22 +3177,29 @@ cmd_interactive_normal() {
|
||||
}
|
||||
|
||||
parse_global_opts() {
|
||||
local args=()
|
||||
PARSED_ARGS=()
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--verbose) VERBOSE=true; shift ;;
|
||||
--dry-run) DRY_RUN=true; shift ;;
|
||||
--log-file) LOG_FILE="$2"; shift 2 ;;
|
||||
*) args+=("$1"); shift ;;
|
||||
--log-file)
|
||||
if [ $# -lt 2 ] || [ -z "$2" ]; then error_exit "--log-file requires a path"; fi
|
||||
case "$2" in --*) error_exit "--log-file requires a path" ;; esac
|
||||
LOG_FILE="$2"; shift 2 ;;
|
||||
--) shift; PARSED_ARGS+=("$@"); break ;;
|
||||
*) PARSED_ARGS+=("$1"); shift ;;
|
||||
esac
|
||||
done
|
||||
set -- "${args[@]}"
|
||||
echo "$@"
|
||||
}
|
||||
|
||||
main() {
|
||||
load_config
|
||||
eval "set -- $(parse_global_opts "$@")"
|
||||
parse_global_opts "$@"
|
||||
if [ "${PARSED_ARGS[0]+present}" = present ]; then
|
||||
set -- "${PARSED_ARGS[@]}"
|
||||
else
|
||||
set --
|
||||
fi
|
||||
|
||||
# Per-command help
|
||||
if [ "${2:-}" = "--help" ] || [ "${2:-}" = "-h" ]; then
|
||||
@@ -3207,7 +3216,7 @@ main() {
|
||||
firewall|fw) cmd_firewall ;;
|
||||
firewall-off|fw-off) cmd_firewall_off ;;
|
||||
harden) cmd_harden ;;
|
||||
audit) cmd_audit "$2" ;;
|
||||
audit) cmd_audit "${2:-}" ;;
|
||||
whitelist|wl) cmd_whitelist ;;
|
||||
backup) cmd_backup ;;
|
||||
restore) cmd_restore ;;
|
||||
@@ -3225,13 +3234,13 @@ main() {
|
||||
version|-v|--version) cmd_version ;;
|
||||
init) cmd_init ;;
|
||||
suggest) cmd_suggest ;;
|
||||
remediate) cmd_remediate "$2" ;;
|
||||
predict) cmd_predict "$2" ;;
|
||||
telemetry) telemetry_opt_in "$2" ;;
|
||||
remediate) cmd_remediate "${2:-}" ;;
|
||||
predict) cmd_predict "${2:-}" ;;
|
||||
telemetry) telemetry_opt_in "${2:-}" ;;
|
||||
demo) run_demo ;;
|
||||
update) do_self_update ;;
|
||||
uninstall) do_uninstall ;;
|
||||
report) generate_report "$2" ;;
|
||||
report) generate_report "${2:-}" ;;
|
||||
config) cmd_config "$@" ;;
|
||||
vpn-kill) vpn_kill_install "$@" ;;
|
||||
vpn-kill-remove) vpn_kill_remove ;;
|
||||
@@ -3243,7 +3252,7 @@ main() {
|
||||
install_monitor_launchdaemon ""
|
||||
success "Reinstall complete"
|
||||
;;
|
||||
discord-bot) cmd_discord_bot_install "$2" "$3" ;;
|
||||
discord-bot) cmd_discord_bot_install "${2:-}" "${3:-}" ;;
|
||||
discord-bot-stop) cmd_discord_bot_stop ;;
|
||||
discord-bot-status) cmd_discord_bot_status ;;
|
||||
help|-h|--help) show_help; exit 0 ;;
|
||||
|
||||
Reference in New Issue
Block a user