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 $?
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() {
_filter "$@" && return $?
echo "would run _hidden: $*"
}
# ------------------ builtin commands and functions ------------------
# (https://github.com/rwxrob/template-bash-command)
############################## BOILERPLATE ###########################
## 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.'
@ -137,6 +124,8 @@ command_usage() {
printf "usage: %s (%s)\n" "$EXE" "${cmds[*]}"
}
# --------------------------- help command ---------------------------
HELP[help]='
```
@ -180,6 +169,18 @@ command_help() {
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]='
## Generate `README.md` File
@ -203,6 +204,8 @@ command_readme() {
echo -e "----\n\n*Autogenerated $(date)*\n"
}
# -------------------------- config command --------------------------
HELP[config]='
```
@ -210,6 +213,9 @@ HELP[config]='
'"$EXE"' config KEY
'"$EXE"' config KEY VALUE
'"$EXE"' config KEY ""
'"$EXE"' config dir
'"$EXE"' config path [file]
'"$EXE"' config edit [file]
```
The `config` command is for reading, writing, and displaying standard
@ -222,10 +228,11 @@ With no arguments outputs all the currently cached configuration
settings.
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
to the current configuration directory containing the `values` file. The
special `path` KEY is reserved and always outputs the full path to the
`values` file itself.
it unless it is one of the following special (reserved) key names:
* `dir` full path to config directory
* `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
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.'
command_config() {
case $1 in
dir) _config_dir ;;
path) _config_path ;;
edit) _config_edit ;;
esac
case $# in
0) _dump_config ;;
1) _get_config "$@" ;;
*) _set_config "$@" ;;
0) _config_dump ;;
1) _config_get "$@" ;;
*) _config_set "$@" ;;
esac
}
_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]}"
_config_edit() {
: "${CONFIG[editor]:="${EDITOR:=vi}"}"
exec "${CONFIG[editor]}" "$(_config_path "${1:-values}")"
}
_config_path() {
_config_dir() {
local dir="$HOME/.config/$EXE"
[[ -n "$XDG_CONFIG_HOME" ]] && dir="$XDG_CONFIG_HOME/$EXE"
[[ -n "$CONFIG_DIR" ]] && dir="$CONFIG_DIR"
@ -286,24 +293,22 @@ _config_path() {
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="$*"
val="${val//$'\n'/\\n}"
CONFIG["$key"]="$val"
_write_config
_config_write
}
_get_config() {
local key="$1"
case $key in
dir) _config_path "" ;;
path) _config_path values ;;
*) printf "${CONFIG[$key]}" ;;
esac
}
_config_get() { printf "${CONFIG[$1]}"; }
_read_config() {
local values="$(_get_config path)"
_config_read() {
local values="$(_config_path)"
[[ -r "$values" ]] || return 0
while IFS= read -r line; do
[[ $line =~ ^([^=]+)=(.+)$ ]] || continue
@ -311,29 +316,55 @@ _read_config() {
done < "$values"
}
_write_config() {
local dir="$(_get_config dir)"
_config_write() {
local dir="$(_config_dir)"
mkdir -p "$dir"
_dump_config > "$dir/values"
_config_dump > "$dir/values"
}
_dump_config() {
_config_dump() {
(( ${#CONFIG[@]} == 0 )) && return 0
paste -d=\
<(printf "%s\n" "${!CONFIG[@]}") \
<(printf "%s\n" "${CONFIG[@]}")
}
_trim() {
local it="${1#"${1%%[![:space:]]*}"}"
echo -e "${it%"${it##*[![:space:]]}"}"
}
# --------------------------- json command ---------------------------
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() {
_buffer "$@" && return $?
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; }
_filter(){
@ -373,7 +404,7 @@ for c in "${COMMANDS[@]}"; do
fi
done
_read_config
_config_read
if [[ -n "$1" ]]; then
declare cmd="$1"; shift