feat: more skills
This commit is contained in:
1 parent
fee46057a1
commit
348ef9f6b5
16 files changed
+143
-45
No files matched your search
+25
-20
@@ -70,7 +70,7 @@ open_url() {
|
||||
elif command -v explorer.exe >/dev/null 2>&1; then explorer.exe "$url"
|
||||
elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$url"
|
||||
elif command -v open >/dev/null 2>&1; then open "$url"
|
||||
else warn "couldn't open a browser; visit it manually: $url"; fi
|
||||
else false; fi
|
||||
} >/dev/null 2>&1 || warn "couldn't open a browser, so visit it manually: $url"
|
||||
}
|
||||
|
||||
@@ -88,52 +88,57 @@ confirm() {
|
||||
[[ "$reply" =~ ^[Yy] ]]
|
||||
}
|
||||
|
||||
# _existing KEY: current value of KEY in ENV_FILE, if any.
|
||||
# _existing KEY: current value of KEY in ENV_FILE, if any, with write_env's quoting undone.
|
||||
_existing() {
|
||||
[[ -f "$ENV_FILE" ]] || return 1
|
||||
local line; line=$(grep -E "^${1}=" "$ENV_FILE" | tail -n1) || return 1
|
||||
printf '%s' "${line#*=}"
|
||||
local line value sq="'\\''"; line=$(grep -E "^${1}=" "$ENV_FILE" | tail -n1) || return 1
|
||||
value="${line#*=}"
|
||||
if [[ "$value" == \'*\' ]]; then value="${value:1:${#value}-2}"; value="${value//"$sq"/\'}"; fi
|
||||
printf '%s' "$value"
|
||||
}
|
||||
|
||||
# ask KEY "Prompt" reads a value into $KEY. Offers the existing .env value as
|
||||
# a default on re-runs (Enter keeps it). Visible input (non-secret).
|
||||
# a default on re-runs (Enter keeps it). Visible input (non-secret), edited
|
||||
# with Readline. Fails at EOF with no input.
|
||||
ask() {
|
||||
local key="$1" prompt="$2" current input
|
||||
local key="$1" prompt="$2" current input s=$'\001' e=$'\002' p rc=0
|
||||
current=$(_existing "$key" || true)
|
||||
if [[ -n "$current" ]]; then
|
||||
printf ' %s%s%s %s[Enter keeps current]%s ' "$BOLD" "$prompt" "$RESET" "$DIM" "$RESET"
|
||||
else
|
||||
printf ' %s%s%s ' "$BOLD" "$prompt" "$RESET"
|
||||
fi
|
||||
read -r input || true
|
||||
# Readline needs the colour codes wrapped in \001..\002 to place the cursor.
|
||||
p=" $s$BOLD$e$prompt$s$RESET$e "
|
||||
[[ -n "$current" ]] && p+="$s$DIM$e[Enter keeps current]$s$RESET$e "
|
||||
read -e -r -p "$p" input || rc=1
|
||||
if (( rc )) && [[ -z "$input" ]]; then return 1; fi
|
||||
[[ -z "$input" && -n "$current" ]] && input="$current"
|
||||
printf -v "$key" '%s' "$input"
|
||||
}
|
||||
|
||||
# ask_secret KEY "Prompt" is like ask, but input is hidden.
|
||||
ask_secret() {
|
||||
local key="$1" prompt="$2" current input
|
||||
local key="$1" prompt="$2" current input rc=0
|
||||
current=$(_existing "$key" || true)
|
||||
if [[ -n "$current" ]]; then
|
||||
printf ' %s%s%s %s[Enter keeps current]%s ' "$BOLD" "$prompt" "$RESET" "$DIM" "$RESET"
|
||||
else
|
||||
printf ' %s%s%s ' "$BOLD" "$prompt" "$RESET"
|
||||
fi
|
||||
read -rs input || true
|
||||
read -rs input || rc=1
|
||||
printf '\n'
|
||||
if (( rc )) && [[ -z "$input" ]]; then return 1; fi
|
||||
[[ -z "$input" && -n "$current" ]] && input="$current"
|
||||
printf -v "$key" '%s' "$input"
|
||||
}
|
||||
|
||||
# write_env KEY VALUE upserts KEY=VALUE into ENV_FILE (creates it; replaces
|
||||
# any existing line). Idempotent.
|
||||
# write_env KEY VALUE upserts KEY='VALUE' into ENV_FILE (replaces any existing
|
||||
# line). Idempotent. Creates ENV_FILE at mode 0600; writes through a symlink
|
||||
# and keeps an existing file's mode.
|
||||
write_env() {
|
||||
local key="$1" value="$2" tmp
|
||||
touch "$ENV_FILE"
|
||||
local key="$1" value="$2" tmp sq="'\\''"
|
||||
[[ -e "$ENV_FILE" ]] || (umask 077 && : > "$ENV_FILE")
|
||||
tmp=$(mktemp)
|
||||
grep -vE "^${key}=" "$ENV_FILE" > "$tmp" || true
|
||||
printf '%s=%s\n' "$key" "$value" >> "$tmp"
|
||||
mv "$tmp" "$ENV_FILE"
|
||||
printf "%s='%s'\n" "$key" "${value//\'/$sq}" >> "$tmp"
|
||||
cat "$tmp" > "$ENV_FILE"
|
||||
rm -f "$tmp"
|
||||
WRITTEN_ENV+=("$key")
|
||||
printf ' %s✓ wrote%s %s → %s\n' "$GREEN" "$RESET" "$key" "$ENV_FILE"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user