Upgrade `_config_*` functions `config` command

This commit is contained in:
rwxrob 2021-08-26 09:15:09 -04:00
parent 7c67b896b7
commit 39d17d9234
No known key found for this signature in database
GPG Key ID: 1CCACEDD2F65578E
1 changed files with 86 additions and 55 deletions

141
cmd
View File

@ -101,29 +101,16 @@ command_bar() {
_buffer "$@" && return $? _buffer "$@" && return $?
echo "would bar: $*" echo "would bar: $*"
} }
HELP[json]='
## Convert to JSON String
```
'"$EXE"' json STRING
'"$EXE"' json <<< STRING
'"$EXE"' json < FILE
'"$EXE"' json < <(COMMAND)
```
Converts input into JSON string using `jq` (if found) containing only
escaped (`\\n`) line returns.'
command_json() { _jsonstr "$@"; }
command__hidden() { command__hidden() {
_filter "$@" && return $? _filter "$@" && return $?
echo "would run _hidden: $*" echo "would run _hidden: $*"
} }
# ------------------ builtin commands and functions ------------------ ############################## BOILERPLATE ###########################
# (https://github.com/rwxrob/template-bash-command) ## Everything from here to the end of file can be snipped and updated
## with latest from https://github.com/rwxrob/template-bash-command.
# -------------------------- utility command -------------------------
HELP[usage]='Displays a summary of usage.' HELP[usage]='Displays a summary of usage.'
@ -137,6 +124,8 @@ command_usage() {
printf "usage: %s (%s)\n" "$EXE" "${cmds[*]}" printf "usage: %s (%s)\n" "$EXE" "${cmds[*]}"
} }
# --------------------------- help command ---------------------------
HELP[help]=' HELP[help]='
``` ```
@ -180,6 +169,18 @@ command_help() {
echo -e "$title\n\n$body" | "$PAGER" echo -e "$title\n\n$body" | "$PAGER"
} }
_help_title() {
_filter "$@" && return $?;
local name="$1"
while IFS= read -r line; do
[[ $line =~ ^[:space]*$ ]] && continue
[[ $line =~ ^#\ (.+) ]] && echo "${BASH_REMATCH[1]}" && return 0
return 1
done <<< "${HELP[$name]}"
}
# -------------------------- readme command --------------------------
HELP[readme]=' HELP[readme]='
## Generate `README.md` File ## Generate `README.md` File
@ -203,6 +204,8 @@ command_readme() {
echo -e "----\n\n*Autogenerated $(date)*\n" echo -e "----\n\n*Autogenerated $(date)*\n"
} }
# -------------------------- config command --------------------------
HELP[config]=' HELP[config]='
``` ```
@ -210,6 +213,9 @@ HELP[config]='
'"$EXE"' config KEY '"$EXE"' config KEY
'"$EXE"' config KEY VALUE '"$EXE"' config KEY VALUE
'"$EXE"' config KEY "" '"$EXE"' config KEY ""
'"$EXE"' config dir
'"$EXE"' config path [file]
'"$EXE"' config edit [file]
``` ```
The `config` command is for reading, writing, and displaying standard The `config` command is for reading, writing, and displaying standard
@ -222,10 +228,11 @@ With no arguments outputs all the currently cached configuration
settings. settings.
With a single KEY argument fetches the value for that key and outputs With a single KEY argument fetches the value for that key and outputs
it. The special `dir` KEY is reserved and always outputs the full path it unless it is one of the following special (reserved) key names:
to the current configuration directory containing the `values` file. The
special `path` KEY is reserved and always outputs the full path to the * `dir` full path to config directory
`values` file itself. * `path` full path to specific config file (default: `values`)
* `edit` opens config file in editor (default: `editor` or `$EDITOR)
With more than one argument the remaining arguments after the KEY will With more than one argument the remaining arguments after the KEY will
be combined into the VALUE and written to a `values` file in the be combined into the VALUE and written to a `values` file in the
@ -261,24 +268,24 @@ format. It is designed for ultimate simplicity, efficiency, and
portability.' portability.'
command_config() { command_config() {
case $1 in
dir) _config_dir ;;
path) _config_path ;;
edit) _config_edit ;;
esac
case $# in case $# in
0) _dump_config ;; 0) _config_dump ;;
1) _get_config "$@" ;; 1) _config_get "$@" ;;
*) _set_config "$@" ;; *) _config_set "$@" ;;
esac esac
} }
_help_title() { _config_edit() {
_filter "$@" && return $?; : "${CONFIG[editor]:="${EDITOR:=vi}"}"
local name="$1" exec "${CONFIG[editor]}" "$(_config_path "${1:-values}")"
while IFS= read -r line; do
[[ $line =~ ^[:space]*$ ]] && continue
[[ $line =~ ^#\ (.+) ]] && echo "${BASH_REMATCH[1]}" && return 0
return 1
done <<< "${HELP[$name]}"
} }
_config_path() { _config_dir() {
local dir="$HOME/.config/$EXE" local dir="$HOME/.config/$EXE"
[[ -n "$XDG_CONFIG_HOME" ]] && dir="$XDG_CONFIG_HOME/$EXE" [[ -n "$XDG_CONFIG_HOME" ]] && dir="$XDG_CONFIG_HOME/$EXE"
[[ -n "$CONFIG_DIR" ]] && dir="$CONFIG_DIR" [[ -n "$CONFIG_DIR" ]] && dir="$CONFIG_DIR"
@ -286,24 +293,22 @@ _config_path() {
echo "$dir" echo "$dir"
} }
_set_config() { _config_path() {
local file=${1:-values}
printf "%s/%s\n" "$(_config_dir)" "$file"
}
_config_set() {
local key="$1"; shift; local val="$*" local key="$1"; shift; local val="$*"
val="${val//$'\n'/\\n}" val="${val//$'\n'/\\n}"
CONFIG["$key"]="$val" CONFIG["$key"]="$val"
_write_config _config_write
} }
_get_config() { _config_get() { printf "${CONFIG[$1]}"; }
local key="$1"
case $key in
dir) _config_path "" ;;
path) _config_path values ;;
*) printf "${CONFIG[$key]}" ;;
esac
}
_read_config() { _config_read() {
local values="$(_get_config path)" local values="$(_config_path)"
[[ -r "$values" ]] || return 0 [[ -r "$values" ]] || return 0
while IFS= read -r line; do while IFS= read -r line; do
[[ $line =~ ^([^=]+)=(.+)$ ]] || continue [[ $line =~ ^([^=]+)=(.+)$ ]] || continue
@ -311,29 +316,55 @@ _read_config() {
done < "$values" done < "$values"
} }
_write_config() { _config_write() {
local dir="$(_get_config dir)" local dir="$(_config_dir)"
mkdir -p "$dir" mkdir -p "$dir"
_dump_config > "$dir/values" _config_dump > "$dir/values"
} }
_dump_config() { _config_dump() {
(( ${#CONFIG[@]} == 0 )) && return 0 (( ${#CONFIG[@]} == 0 )) && return 0
paste -d=\ paste -d=\
<(printf "%s\n" "${!CONFIG[@]}") \ <(printf "%s\n" "${!CONFIG[@]}") \
<(printf "%s\n" "${CONFIG[@]}") <(printf "%s\n" "${CONFIG[@]}")
} }
_trim() { # --------------------------- json command ---------------------------
local it="${1#"${1%%[![:space:]]*}"}"
echo -e "${it%"${it##*[![:space:]]}"}" HELP[json]='
} ## Convert to JSON String
```
'"$EXE"' json STRING
'"$EXE"' json <<< STRING
'"$EXE"' json < FILE
'"$EXE"' json < <(COMMAND)
```
Converts input into JSON string using `jq` (if found) containing only
escaped (`\\n`) line returns.'
command_json() { _jsonstr "$@"; }
_jsonstr() { _jsonstr() {
_buffer "$@" && return $? _buffer "$@" && return $?
jq -MRsc <<< "$1" jq -MRsc <<< "$1"
} }
# ----------------------------- utilities ----------------------------
_reduce() {
local -n name="${1:?"name of array required"}"
while IFS= read -r key; do
[[ $key =~ $2 ]] && echo "$key"
done < <(printf "%s\n" "${name[@]}")
}
_trim() {
local it="${1#"${1%%[![:space:]]*}"}"
echo -e "${it%"${it##*[![:space:]]}"}"
}
_have(){ type "$1" &>/dev/null; } _have(){ type "$1" &>/dev/null; }
_filter(){ _filter(){
@ -373,7 +404,7 @@ for c in "${COMMANDS[@]}"; do
fi fi
done done
_read_config _config_read
if [[ -n "$1" ]]; then if [[ -n "$1" ]]; then
declare cmd="$1"; shift declare cmd="$1"; shift