Skip to content

Commit

Permalink
rewrite of user config, major improvements
Browse files Browse the repository at this point in the history
For the full changelog and remaining todos, refer to:

https://github.com/bevry/dorothy/discussions/24

Closes #23 #17

Signed-off-by: Benjamin Lupton <b@lupton.cc>
  • Loading branch information
balupton committed Oct 25, 2021
1 parent 844afcf commit 6f841ad
Show file tree
Hide file tree
Showing 117 changed files with 1,680 additions and 1,074 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
21 changes: 21 additions & 0 deletions TIPS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Scripts

## shell

``` bash
if test -n "${ZSH_VERSION-}"; then
echo 'zsh'
elif test -n "${FISH_VERSION-}"; then
echo 'fish'
elif test -n "${BASH_VERSION-}"; then
echo 'bash'
elif test -n "${KSH_VERSION-}"; then
echo 'ksh'
elif test -n "${FCEDIT-}"; then
echo 'ksh'
elif test -n "${PS3-}"; then
echo 'unknown'
else
echo 'sh'
fi
```

## exit codes

- [Command exit codes](https://gist.github.com/shinokada/5432e491f9992da994fbed05948bfba1)
Expand Down Expand Up @@ -39,6 +59,7 @@ https://stackoverflow.com/a/68310927/130638
- http://unix.stackexchange.com/a/246320/50703
- `-z` is empty string: True if the length of string is zero.
- `-n` is string: True if the length of string is nonzero.
- `-e` is file or directory.
- `-d` is dir: True if file exists and is a directory.
- `-f` is file: True if file exists and is a regular file.
- `-s` is nonempty file: True if file exists and has a size greater than zero.
Expand Down
19 changes: 12 additions & 7 deletions commands/ask
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ if is-help "$@"; then
'Provide [--password] to specify that the prompt should hide the value when entering by using password mode.' \
'Provide [--required] to specify that the prompt should not continue until a value is provided.' \
'Provide [--timeout=...] to specify a custom timeout value in seconds.' \
'Provide [--flag=...] -- <...args> to specify a flag to search the arguments for, to set a default value.'
'Provide [--flag=...] -- <...args> to specify a flag to search the arguments for, to set a default value.' \
'Provide [--main] to not create an alternative TTY shell when promting for questions.'
exit 22 # Invalid argument
fi

Expand All @@ -29,9 +30,10 @@ option_question="$(get-flag-value question -- "${options[@]}")"
option_default="$(get-flag-value default -- "${options[@]}")"
option_confirm="$(get-flag-value confirm -- "${options[@]}")"
option_password="$(get-flag-value password -- "${options[@]}")"
option_required="$(get-flag-value required -- "${options[@]}")"
option_required="$(get-flag-value required -- "${options[@]}" | echo-on-empty-stdin 'no')"
option_timeout="$(get-flag-value timeout -- "${options[@]}")"
option_flag="$(get-flag-value flag -- "${options[@]}")"
option_main="$(get-flag-value main -- "${options[@]}" | echo-on-empty-stdin 'no')"
mapfile -t args < <(echo-after-separator "$@")

# prepare
Expand All @@ -52,7 +54,7 @@ function handle_timeout () {
sleep 10
echo "$result"
return 0
elif test "$option_required" != 'yes'; then
elif test "$option_required" = 'no'; then
stderr echo 'Ask timed out, as the field was optional will use no value.'
sleep 10
return 0
Expand All @@ -63,7 +65,9 @@ function handle_timeout () {
fi
}
function ask () {
tty_auto
if test "$option_main" = 'no'; then
tty_auto
fi
asked='yes'
if test -n "${1-}"; then
echo -e "$1" > /dev/tty
Expand All @@ -75,7 +79,7 @@ function ask () {
fi
if is-value "$result"; then
break
elif test "$option_required" != 'yes'; then
elif test "$option_required" = 'no'; then
result=''
break
fi
Expand Down Expand Up @@ -110,12 +114,12 @@ function validate () {
else
choices+=('custom' 'enter a value')
fi
if test "$option_required" != 'yes'; then
if test "$option_required" = 'no'; then
choices+=('none' 'use no value')
fi

# as need to confirm, adjust the timeout
if test -z "$option_timeout" && (is-value "$result" || test "$option_required" != 'yes'); then
if test -z "$option_timeout" && (is-value "$result" || test "$option_required" = 'no'); then
# timeout of one minute for confirms of existing values, or optional values
option_timeout=60
fi
Expand All @@ -124,6 +128,7 @@ function validate () {
local ec=0; choice="$(choose-option \
--question="$option_question" \
--timeout="$option_timeout" \
--main="$option_main" \
--label \
-- "${choices[@]}")" || ec="$?"

Expand Down
3 changes: 3 additions & 0 deletions commands/checksum
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
source "$DOROTHY/sources/strict.bash"

# dependencies
env QUIET=y CLI='pv' APT='pv' BREW='pv' setup-util

# algorithms
algorithms=()
for alg in 'md5sum' 'shasum' 'sha256sum'; do
Expand Down
10 changes: 5 additions & 5 deletions commands/chmod-partial
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
source "$DOROTHY/sources/strict.bash"

# options
mapfile -t options < <(echo-before-separator "$@")
mapfile -t permissions < <(echo-before-separator "$@")
mapfile -t paths < <(echo-after-separator "$@")

if is-array-empty-or-partial "${options[@]}" || is-array-empty-or-partial "${paths[@]}"; then
if is-array-empty-or-partial "${permissions[@]}" || is-array-empty-or-partial "${paths[@]}"; then
stderr echo "USAGE:"
stderr echo "chmod-partial <options> -- <paths...>"
stderr echo "chmod-partial <permissions> -- <paths...>"
stderr echo ""
stderr echo "EXAPLE:"
stderr echo "chmod-partial -f +x -- path path path"
stderr echo "chmod-partial +x -- path path path"
exit 1
fi

for path in "${paths[@]}"; do
chmod "${options[@]}" "$path" || echo "unable to ${options[*]}: $path"
chmod --changes --recursive "${permissions[@]}" "$path" || echo "unable to ${permissions[*]}: $path"
done
27 changes: 21 additions & 6 deletions commands/choose-menu
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ if is-help-separator "$@"; then
'FLAGS:' \
'Provide [--question=...] to specify the question that the prompt will be answering.' \
'Provide [--multi] to specify that multiple menu items should be able to be selected.' \
'Provide [--timeout=...] to specify a custom timeout value in seconds.'
'Provide [--timeout=...] to specify a custom timeout value in seconds.' \
'Provide [--main] to not create an alternative TTY shell when promting for questions.'
exit 22 # Invalid argument
fi
tty_auto

# options
mapfile -t options < <(echo-before-separator "$@")
option_question="$(get-flag-value question -- "${options[@]}")"
option_multi="$(get-flag-value multi -- "${options[@]}" | echo-on-empty-stdin 'no')"
option_timeout="$(get-flag-value timeout -- "${options[@]}")"
option_main="$(get-flag-value main -- "${options[@]}" | echo-on-empty-stdin 'no')"
option_required="$(get-flag-value required -- "${options[@]}" | echo-on-empty-stdin 'no')"
mapfile -t choices < <(echo-after-separator "$@")

# ensure we have items
Expand All @@ -46,6 +48,9 @@ if is-array-empty-or-partial "${choices[@]}"; then
fi

# prepare
if test "$option_main" = 'no'; then
tty_auto
fi
cursor=0
count="${#choices[@]}"
last="$((count - 1 ))"
Expand Down Expand Up @@ -150,24 +155,34 @@ while test "$action" != 'done'; do
fi
break
elif test "$action" = "escape"; then
break
# todo implement --required with --multi fallback properly here
if test "$option_required" = 'no'; then
break
fi
fi

# no break, so repeat the menu
tty_clear
if test "$option_main" = 'no'; then
tty_clear
fi
done

# if break, then no clear occured, so clear it here
tty_clear
if test "$option_main" = 'no'; then
tty_clear
fi

# if multi with no selection then ask for everything
# todo implement --required properly here
if is-array-empty "${selections[@]}" && test "$option_multi" = 'yes'; then
if confirm-positive "You exited without selecting anything, do you wish to select all?" > /dev/tty; then
for i in "${!choices[@]}"; do
selections[$i]='yes'
done
fi
tty_clear
if test "$option_main" = 'no'; then
tty_clear
fi
fi

# output the custom selections
Expand Down
37 changes: 32 additions & 5 deletions commands/choose-option
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ if is-help-separator "$@"; then
' [--filter=filter] \ ' \
' [--label[=first]] \ ' \
" [--return='\$label'] \ " \
" [--visual='\$label [\$value]' \ " \
" [--multi]' \ " \
" [--confirm]' \ " \
" [--visual='\$label [\$value]'] \ " \
' [--required] \ ' \
" [--multi] \ " \
" [--confirm] \ " \
" [--main] \ " \
' -- <[value]...> <value> <value>' \
'' \
'If you wish to show a question above the menu:' \
Expand All @@ -29,6 +31,9 @@ if is-help-separator "$@"; then
'If you wish to filter the value and/or labels, use:' \
'--filter=filter -- <...>' \
'' \
'If you wish to prevent using the escape key to provide no selection, use:' \
'--required -- <...>' \
'' \
'If you wish to allow multiple selections:' \
'--multi -- <...>' \
'' \
Expand All @@ -54,7 +59,10 @@ if is-help-separator "$@"; then
'' \
'If you wish to return the visual use:' \
"--label --return='\$visual' -- <[value, label]...>" \
"^ the value is eval'd"
"^ the value is eval'd" \
'' \
'If you wish to not use an alternative TTY shell, use:' \
'--main -- <[value, label]...>'
exit 22 # Invalid argument
fi

Expand All @@ -67,6 +75,8 @@ option_visual="$(get-flag-value visual -- "${options[@]}")"
option_multi="$(get-flag-value multi -- "${options[@]}" | echo-on-empty-stdin 'no')"
option_timeout="$(get-flag-value timeout -- "${options[@]}")"
option_confirm="$(get-flag-value confirm -- "${options[@]}")"
option_required="$(get-flag-value required -- "${options[@]}" | echo-on-empty-stdin 'no')"
option_main="$(get-flag-value main -- "${options[@]}" | echo-on-empty-stdin 'no')"
# shellcheck disable=2016
option_return="$(get-flag-value 'return' -- "${options[@]}" | echo-on-empty-stdin '$value')"

Expand Down Expand Up @@ -209,6 +219,8 @@ function act () {
local ec=0; results="$(choose-menu \
--question="$option_question" \
--multi="$option_multi" \
--main="$option_main" \
--required="$option_required" \
--timeout="$option_timeout" \
-- "${filtered_visuals[@]}")" || ec="$?"

Expand All @@ -222,6 +234,15 @@ function act () {
# some other failure
sleep 10
exit "$ec"
elif test -z "$results"; then
# no option
if test "$option_required" = 'no'; then
exit 0
else
echo "No input returned, despite required." > /dev/stderr
sleep 10
exit 5 # Input/output error
fi
fi

# results
Expand All @@ -235,7 +256,13 @@ function act () {
if test "${#values[@]}" -gt 1 -a "$option_confirm" = 'yes'; then
# timeout of one minute for confirms of existing values
confirm_value="$(echo -n "${final_results[@]}")"
local ec=0; confirmed="$(choose-option --timeout=60 --question="$option_question" --label -- \
local ec=0; confirmed="$(choose-option \
--timeout=60 \
--question="$option_question" \
--main="$option_main" \
--required="$option_required" \
--label \
-- \
yes "Use: $confirm_value" \
no "Select something else.")" || ec="$?"

Expand Down
4 changes: 4 additions & 0 deletions commands/config-add
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
source "$DOROTHY/sources/strict.bash"

config-set "$1" "$2" "$2"
11 changes: 11 additions & 0 deletions commands/config-get
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
source "$DOROTHY/sources/strict.bash"

# dependencies
env QUIET=y setup-util-ripgrep

# options
file="$1" # /etc/systemd/resolved.conf
find="$2" # Domains

rg -o "^$find=(.+)" --replace '$1' < "$file"
20 changes: 20 additions & 0 deletions commands/config-replace
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
source "$DOROTHY/sources/strict.bash"

# dependencies
env QUIET=y setup-util-ripgrep

# options
file="$1" # /etc/systemd/resolved.conf
content="$(cat "$file")"
find="$2" # Domains
pattern="^[#]?$find=.+?\n"
replace="$3" # Domains=~.

# act, replacing inline
sudo touch "$file"
if rg -q "$pattern" <<< "$content"; then
(rg --passthru "$pattern" --replace "$replace" <<< "$content") > "$file"
else
echo -e "$content\n\n$replace" > "$file"
fi
11 changes: 7 additions & 4 deletions commands/conf-update → commands/config-set
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#!/usr/bin/env bash
source "$DOROTHY/sources/strict.bash"

# dependencies
env QUIET=y setup-util-sd

# options
file="$1" # /etc/systemd/resolved.conf
find="$2" # Domains
pattern="^[#]?$find=.+?\n"
replace="$3" # Domains=~.

env QUIET=y setup-util-sd

# act, replacing at the end
sudo touch "$file"
contents="$(sd -p "^[#]?$find=.+?\n" '' < "$file")"
if is-nonempty-string "$contents"; then
contents="$(sd -p "$pattern" '' < "$file")"
if test -n "$contents"; then
contents+="\n"
fi
echo -e "$contents$replace" | sudo tee "$file" &> /dev/null # silent everything
6 changes: 3 additions & 3 deletions commands/ddns
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
source "$DOROTHY/sources/user.sh"
source "$DOROTHY/sources/strict.bash"
source "$DOROTHY/sources/config.sh"
load_dorothy_config 'ddns.bash'

# dependencies
env QUIET=y setup-util-httpie
Expand All @@ -10,5 +11,4 @@ env QUIET=y setup-util-httpie
# tunnel="$(fetch localhost:4040/api/tunnels | jq -r .tunnels[0].public_url)"
# tunnel="$(get-url-hostname "$tunnel")"

tunnel="coolflower3649.cotunnel.com"
http --verbose --check-status "$DDNS_WORKER" Authorization:"${DDNS_WORKER_AUTH-}" content="$tunnel"
http --verbose --check-status "${DDNS_WORKER}" Authorization:"${DDNS_WORKER_AUTH-}" content="${DDNS_URL}"
2 changes: 1 addition & 1 deletion commands/debug-docker
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ sudo service docker restart

dnsip="$(what-is-my-exposed-dns)"
set -x
sudo docker run --rm --cap-add=NET_ADMIN --net=bridge --dns="$dnsip" alpine sh -c "apk add curl bind-tools; $(cat "$DOROTHY/commands/debug-network")"
sudo docker run --rm --cap-add=NET_ADMIN --net=bridge --dns="$dnsip" alpine sh -c "$(cat "$DOROTHY/commands/debug-network")"
set +x
Loading

0 comments on commit 6f841ad

Please sign in to comment.