Skip to content

Commit

Permalink
sudo prompts are no longer excessively noisey
Browse files Browse the repository at this point in the history
- consistent clean outputs
- echo-lines:
    - support variable columns
- eval-helper:
    - support title option
    - output wrapping information to tty, such that stdout is useful
- sudo-helper:
    - support quiet, wrap, and confirm options
    - sudo commands are output in yellow when wrapping
- fs-own: readable path outputs
- setup-git: consistent coloring
- setup-node, setup-util-node: consistent coloring and version displays
- setup-dns: remove unused quiet option
- service-helper: fixed typos in reasons

/ref f0afd9d
  • Loading branch information
balupton committed Jan 29, 2024
1 parent 4524035 commit b003e9c
Show file tree
Hide file tree
Showing 21 changed files with 647 additions and 301 deletions.
49 changes: 49 additions & 0 deletions commands.beta/is-shapeshifter
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

function is_shapeshifter() (
source "$DOROTHY/sources/stdinargs.bash"

# =====================================
# Arguments

function help {
cat <<-EOF >/dev/stderr
ABOUT:
Checks whether the <input> is a shapeshifter (something that outputs TTY control sequences)
USAGE:
is-shapeshifter [...options] [--] ...<input>
echo-lines ...<input> | is-shapeshifter [...options]
OPTIONS:
$(stdinargs_options_help --)
EOF
if test "$#" -ne 0; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

# =====================================
# Action

local found='no'
function on_input {
local line="$1"
# regexp should match [echo-clear-lines] [echo-revolving-door] [is-shapeshifter]
if [[ $line =~ $'\e\[[0-9]*[AKGFJ]' ]]; then
found='yes'
return 210 # ECUSTOM 210 Processing complete, exit early
fi
}
function on_finish {
test "$found" = 'yes'
}

stdinargs "$@"
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
is_shapeshifter "$@"
fi
69 changes: 23 additions & 46 deletions commands/btrfs-helper
Original file line number Diff line number Diff line change
Expand Up @@ -95,46 +95,38 @@ function btrfs_helper() (

# Show the structure of a filesystem
# usage: btrfs filesystem show [options] [<path>|<uuid>|<device>|label]
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- btrfs filesystem show "$path"

if test -n "$existing_mount"; then
# Show detailed information about internal allocations in devices.
# usage: btrfs device usage [options] <path> [<path>..]
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- btrfs device usage "$existing_mount"

# Show detailed information about internal filesystem usage .
# usage: btrfs filesystem usage [options] <path> [<path>..]
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- btrfs filesystem usage "$existing_mount"

# Show space usage information for a mount point
# usage: btrfs filesystem df [options] <path>
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- btrfs filesystem df "$existing_mount"

# Show space usage via system
# -a, --all include pseudo, duplicate, inaccessible file systems
# -h, --human-readable print sizes in powers of 1024 (e.g., 1023M)
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- df -ah "$existing_mount"
fi

# Show status
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- btrfs balance status "$path" || : # if still running, exit code 1
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- btrfs scrub status "$path" || : # if still running, exit code 1
eval-helper --no-quiet \
-- sudo-helper \
sudo-helper --wrap \
-- btrfs device stats "$path"

# Get all devices of the mount
Expand All @@ -143,39 +135,32 @@ function btrfs_helper() (
fi

# Summarize disk usage of every nested file inside the directory
# eval-helper --no-quiet \
# -- sudo-helper \
# sudo-helper --wrap \
# -- btrfs filesystem du --summarize --human-readable "$path"

# Rename
# eval-helper --no-quiet \
# -- sudo-helper \
# sudo-helper --wrap \
# -- btrfs filesystem label "$path" "$name"

# Change unallocated to free
# eval-helper --no-quiet \
# -- sudo-helper \
# sudo-helper --wrap \
# -- btrfs filesystem resize max "$path"

# Defragment (balance also defrags)
# eval-helper --no-quiet \
# -- sudo-helper \
# sudo-helper --wrap \
# -- btrfs filesystem defragment "$path"

# Balance as raid1
# eval-helper --no-quiet \
# -- sudo-helper \
# sudo-helper --wrap \
# -- btrfs balance start --background -dconvert=raid1 -mconvert=raid1 "$path"

# Scrub (check filesystem for errors and auto-heal)
# eval-helper --no-quiet \
# -- sudo-helper \
# sudo-helper --wrap \
# -- btrfs scrub start "$path"

# Check devices for errors
# WARNING: the repair mode is considered dangerous and should not be used without prior analysis of problems found on the filesystem.
# eval-helper --no-quiet \
# -- sudo-helper \
# sudo-helper --wrap \
# -- btrfs check --repair --progress /dev/...

# Logs
Expand All @@ -195,8 +180,7 @@ function btrfs_helper() (
)"

# format
eval-helper --no-quiet --wrap --confirm \
-- sudo-helper \
sudo-helper --wrap --confirm \
-- mkfs.btrfs -f -L "$label" "$node"
}
function act_mount {
Expand Down Expand Up @@ -262,8 +246,7 @@ function btrfs_helper() (
# -K|--nodiscard do not perform whole device TRIM on devices that report such capability
# -f|--force force overwrite existing filesystem on the disk
# --enqueue wait if there's another exclusive operation running, otherwise continue
eval-helper --no-quiet --wrap --confirm \
-- sudo-helper \
sudo-helper --wrap --confirm \
-- btrfs device add --force --enqueue "$extra_node" "$existing_mount"

# run a balance
Expand Down Expand Up @@ -298,17 +281,15 @@ function btrfs_helper() (
fi

# wait until balance completes
while ! eval-helper --no-quiet --wrap \
-- sudo-helper \
while ! sudo-helper --wrap \
-- btrfs balance status --verbose "$existing_mount"; do
waiter 5
done

# usage: btrfs device remove <device>|<devid> [<device>|<devid>...] <path>
# Remove a device from a filesystem, specified by a path to the device or as a device id in the filesystem. The btrfs signature is removed from the device.
# --enqueue wait if there's another exclusive operation running, otherwise continue
eval-helper --no-quiet --wrap --confirm \
-- sudo-helper \
sudo-helper --wrap --confirm \
-- btrfs device remove --enqueue "$extra_node" "$existing_mount"

# run a balance
Expand All @@ -335,23 +316,20 @@ function btrfs_helper() (
# usage: btrfs balance status [-v] <path>
# Show status of running or paused balance
# -v|--verbose increase output verbosity
if ! eval-helper --no-quiet --wrap \
-- sudo-helper \
if ! sudo-helper --wrap \
-- btrfs balance status --verbose "$existing_mount"; then

# balance is running, ask what to do
if test "$(choose-option --required --label --question="Do you wish to?" -- 'resume' "Resume the existing balance, and abort a new $strategy balance." 'start' "Cancel the existing balance and resume, and start a new $strategy balance.")" = 'resume'; then
# usage: btrfs balance resume <path>
# Resume interrupted balance
eval-helper --no-quiet --wrap --confirm \
-- sudo-helper \
sudo-helper --wrap --confirm \
-- btrfs balance resume "$existing_mount"
return
else
# usage: btrfs balance cancel <path>
# Cancel running or paused balance
eval-helper --no-quiet --wrap --confirm \
-- sudo-helper \
sudo-helper --wrap --confirm \
-- btrfs balance cancel "$existing_mount"
fi
fi
Expand All @@ -367,8 +345,7 @@ function btrfs_helper() (
if test "$strategy" = 'single'; then
args+=('--force')
fi
eval-helper --no-quiet --wrap --confirm \
-- sudo-helper \
sudo-helper --wrap --confirm \
-- btrfs balance start --verbose --background --enqueue "${args[@]}" -dconvert="$strategy" -mconvert="$strategy" "$existing_mount"
}

Expand Down
4 changes: 2 additions & 2 deletions commands/config-helper
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ function config_helper() (
mapfile -t lines <<<"$value" # read lines into an array, such that indentation works

# addition
value=$'(\n'"$(echo-lines --indent=$'\t' --quoted --columns="${option_columns:-"1"}" -- "${lines[@]}")"$'\n)'
value=$'(\n'"$(echo-lines --indent=$'\t' --quoted --columns="${option_columns:-"1"}" --width=inputs -- "${lines[@]}")"$'\n)'
addition="$field=$value"

# replacement
value=$'(\n'"$(echo-lines --indent=$'${indent}\t' --quoted --columns="${option_columns:-"1"}" -- "${lines[@]}")"$'\n${indent})'
value=$'(\n'"$(echo-lines --indent=$'${indent}\t' --quoted --columns="${option_columns:-"1"}" --width=inputs -- "${lines[@]}")"$'\n${indent})'
replace_pattern="\${indent}$field=$value"

# reset columns
Expand Down
24 changes: 12 additions & 12 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -804,27 +804,27 @@ function dorothy_() (
source "$(type -P eval-helper)"

eval_helper --quiet --no-wrap \
--pending="$(echo-style --bold="Correcting permissions...")" \
--success="$(echo-style --success="Corrected permissions.")" \
--failure="$(echo-style --error="Failed to correct permissions.")" \
--pending="$(echo-style --bold='Correcting permissions...')" \
--success="$(echo-style --success='Corrected permissions.')" \
--failure="$(echo-style --error='Failed to correct permissions.')" \
-- correct_permissions

eval_helper --quiet --no-wrap \
--pending="$(echo-style --bold="Staging new files...")" \
--success="$(echo-style --success="Staged new files.")" \
--failure="$(echo-style --error="Failed to stage new files.")" \
--pending="$(echo-style --bold='Staging new files...')" \
--success="$(echo-style --success='Staged new files.')" \
--failure="$(echo-style --error='Failed to stage new files.')" \
-- stage_new_files "$DOROTHY"

eval_helper --quiet --no-wrap \
--pending="$(echo-style --bold="Staging changed files...")" \
--success="$(echo-style --success="Staged changed files.")" \
--failure="$(echo-style --error="Failed to stage changed files.")" \
--pending="$(echo-style --bold='Staging changed files...')" \
--success="$(echo-style --success='Staged changed files.')" \
--failure="$(echo-style --error='Failed to stage changed files.')" \
-- stage_changed_files "$DOROTHY"

eval_helper --quiet --no-wrap \
--pending="$(echo-style --bold="Removing junk files...")" \
--success="$(echo-style --success="Removed junk files.")" \
--failure="$(echo-style --error="Failed to remove junk files.")" \
--pending="$(echo-style --bold='Removing junk files...')" \
--success="$(echo-style --success='Removed junk files.')" \
--failure="$(echo-style --error='Failed to remove junk files.')" \
-- rm-junk "$DOROTHY"
}

Expand Down
1 change: 1 addition & 0 deletions commands/echo-clear-lines
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function echo_clear_lines() (
# if we are piping (there is newline we want to keep from pressing return in the terminal), or if the input is deleting rather than clearing lines, then we can only safely clear the current line
# touch test.txt; fs-rm --no-confirm --quiet --trash -- test.txt
# setup-util-sd --uninstall; (create an error in do_brew); setup-util-sd
# regexp should match [echo-clear-lines] [echo-revolving-door] [is-shapeshifter]
if [[ $option_piped == 'yes' || $line =~ $'\e\[[0-9]*[AKGFJ]' ]]; then
printf '\e[G\e[2K' # set cursor to start of line, and clear from there
else
Expand Down
Loading

0 comments on commit b003e9c

Please sign in to comment.