2021-08-15 15:56:56 -05:00
|
|
|
#!/bin/bash
|
2021-08-06 12:48:11 -05:00
|
|
|
# shellcheck disable=SC2016
|
|
|
|
set -e
|
2021-08-15 15:56:56 -05:00
|
|
|
# export PATH="/bin:/usr/bin:/usr/local/bin" # safer, if you can
|
2021-08-06 12:48:11 -05:00
|
|
|
|
|
|
|
(( BASH_VERSINFO[0] < 4 )) && echo "Bash 4+ required." && exit 1
|
|
|
|
|
|
|
|
: "${PAGER:=more}"
|
|
|
|
: "${EDITOR:=vi}"
|
|
|
|
: "${HELP_BROWSER:=}"
|
2021-08-23 21:10:27 -05:00
|
|
|
: "${EXE:="${0##*/}"}"
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
declare -A HELP
|
2021-08-31 22:10:03 -05:00
|
|
|
declare -A CONF
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-12-15 23:22:08 -06:00
|
|
|
declare black="\e[30m"
|
|
|
|
declare red="\e[31m"
|
|
|
|
declare green="\e[32m"
|
|
|
|
declare yellow="\e[33m"
|
|
|
|
declare blue="\e[34m"
|
|
|
|
declare magenta="\e[35m"
|
|
|
|
declare cyan="\e[36m"
|
|
|
|
declare white="\e[37m"
|
|
|
|
declare reset="\e[0m"
|
|
|
|
|
2021-08-27 12:11:03 -05:00
|
|
|
_initialize() {
|
|
|
|
: # put initialization code here
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:06:25 -05:00
|
|
|
_alternatives() {
|
|
|
|
# put alternative argument possibilities here
|
|
|
|
if [[ $CMD = f ]];then
|
|
|
|
x.foo "$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
HELP[main]='
|
2021-08-10 17:47:07 -05:00
|
|
|
# Bash Template Command
|
|
|
|
|
|
|
|
*This `README.md` is autogenerated.*
|
|
|
|
|
|
|
|
This is a GitHub template repo that will be copied instead of forked to
|
|
|
|
create a new Bash command with a command something like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
gh repo create rwxrob/mycmd -p rwxrob/template-bash-command
|
|
|
|
```
|
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
This `cmd` inside can then be renamed and finished.
|
2021-08-10 17:47:07 -05:00
|
|
|
|
|
|
|
## Naming Conventions
|
|
|
|
|
|
|
|
* Name repos containing single bash commands with `cmd-`
|
|
|
|
* Name template repos beginning with `template-`
|
2021-08-31 21:38:13 -05:00
|
|
|
* Start command functions with `x.` to be completed
|
2021-08-31 22:10:03 -05:00
|
|
|
* Name `CONF` accessors with `x.` and full path
|
|
|
|
* Use dot (`.`) pathing in `CONF` key names
|
2021-08-31 20:39:57 -05:00
|
|
|
|
2021-08-31 21:00:16 -05:00
|
|
|
Think of `x` as in "executable" command.
|
2021-08-31 08:47:57 -05:00
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
> ⚠️
|
|
|
|
> Note that all versions of Vim current have a bug that does not allow
|
|
|
|
> dot (`.`) to be included in the function name even though it is
|
|
|
|
> explicitly allowed by bash. So you may have to add `.` to your
|
|
|
|
> `/usr/share/vim/vim82/syntax/sh.vim` file anywhere a function name
|
|
|
|
> expression is defined. I have yet to isolate it out and override it in
|
|
|
|
> my own `.vimrc`. It is a minimal edit.
|
|
|
|
|
2021-08-31 08:47:57 -05:00
|
|
|
## Builtins and Utilities
|
|
|
|
|
|
|
|
A number of builtin and frequently used utility functions have been
|
|
|
|
included for convenience. These save developers from adding other
|
|
|
|
moronic things like `sed` and `awk` subprocesses. Obviously, not all of
|
|
|
|
this is needed for many Bash scripts. Just remove what you do not need
|
|
|
|
or want.
|
|
|
|
|
|
|
|
### `_initialize`
|
|
|
|
|
|
|
|
The `_initialize` function is meant to contain initialization code and
|
|
|
|
be placed at the beginning of the script to be found easily even though
|
2021-09-08 09:06:25 -05:00
|
|
|
it is called at the bottom of the script (as bash requires). It is
|
|
|
|
passed the arguments that are passed to the script itself. This function can be omitted.
|
|
|
|
|
|
|
|
### `_alternatives`
|
|
|
|
|
|
|
|
The `_alternatives` function (usually placed after `_initialize`
|
|
|
|
provides a hook for dealing with alternative arguments to those that
|
|
|
|
identify commands (`x.*`). If the first argument to the script does not
|
|
|
|
match a command function then this function will be called before the
|
|
|
|
default `x.usage` command allowing shortcuts and other argument
|
|
|
|
alternatives and intelligent sensing of what command function is wanted
|
|
|
|
by simply examining the argument list. This can be useful when you wish
|
|
|
|
to provide shortcuts for longer commands but do not want to clutter the
|
|
|
|
command usage and completion list. For example, `zet dex.titles` could
|
|
|
|
be trapped in `_alternatives` to call `zet titles`.
|
2021-08-31 08:47:57 -05:00
|
|
|
|
|
|
|
### `_have`
|
|
|
|
|
|
|
|
Returns true (0) if the first argument exists as an executable in the
|
|
|
|
current `PATH`. Otherwise, return false (1).
|
|
|
|
|
|
|
|
### `_checkdep`
|
|
|
|
|
|
|
|
Checks that the first argument exists as an executable in the current
|
|
|
|
`PATH`. If so, returns true (0). If not, prints a generic error message
|
|
|
|
in English and returns false (1). The "progressive enhancement" design
|
|
|
|
principle requires minimal functionality using what is available and
|
|
|
|
progressively upgrading based on what is detected.
|
|
|
|
|
|
|
|
### `_newest`
|
|
|
|
|
|
|
|
Uses `ls` to return the newest file or directory in the specified
|
|
|
|
directory.
|
|
|
|
|
|
|
|
### `_trim`
|
|
|
|
|
|
|
|
Removes all whitespace (`[:space:]`) from the beginning and ending
|
|
|
|
of a string without invoking a subprocess.
|
|
|
|
|
|
|
|
### `_filter`
|
|
|
|
|
|
|
|
Reads the first argument or each line of standard input passing
|
|
|
|
each individually as the first argument to the calling function one at
|
|
|
|
a time. The UNIX philosophy requires all commands be filters whenever possible.
|
|
|
|
|
|
|
|
### `_buffer`
|
|
|
|
|
|
|
|
Reads the first argument or all lines of standard input and then
|
|
|
|
passes them to the calling function as the first argument. The UNIX philosophy requires all command be filters whenever possible.
|
|
|
|
|
|
|
|
### `_reduce`
|
|
|
|
|
|
|
|
Takes the name of an array and a bash extended regular expression
|
|
|
|
and prints only the array entries that match, one to a line suitable for
|
2021-08-31 08:54:18 -05:00
|
|
|
converting back into an array with `IFS=$'"'\\\n'"'` or just as an in-memory
|
2021-08-31 08:47:57 -05:00
|
|
|
`grep` replacement.
|
|
|
|
|
|
|
|
### `_jsonstr`
|
|
|
|
|
|
|
|
Encodes first argument or all standard input into a single line of JSON text. This function depends on the `jq` command.
|
|
|
|
|
|
|
|
### `_urlencode`
|
|
|
|
|
|
|
|
Encodes the first argument or all standard input using standard URL
|
|
|
|
encoding suitable for passing to `curl` or whatever. This function has
|
|
|
|
no external dependencies.
|
|
|
|
|
2021-08-10 17:47:07 -05:00
|
|
|
## Dependencies
|
|
|
|
|
|
|
|
Required:
|
|
|
|
|
|
|
|
* Bash 4+
|
|
|
|
|
|
|
|
Optional:
|
|
|
|
|
|
|
|
* `pandoc` - for rich help docs
|
2021-08-23 21:10:27 -05:00
|
|
|
* `jq` - for `json` and anything that uses it
|
2021-08-10 17:47:07 -05:00
|
|
|
|
|
|
|
## Justification
|
|
|
|
|
2021-08-31 08:47:57 -05:00
|
|
|
Bash is the dominant shell scripting language and the official default
|
2021-08-10 17:47:07 -05:00
|
|
|
Linux interactive shell, which reduces cognitive overhead; every command
|
|
|
|
line *is* a line of code that could be put into script as is. Bash
|
|
|
|
scripts are at the core of cloud, containers, and Kubernetes. Bash 4+
|
|
|
|
with its associative array support, powerful regular expressions, and
|
|
|
|
multiple ways of feeding data to loops easily covers the needs
|
|
|
|
previously requiring Python and Perl scripts. Bash scripts are also much
|
|
|
|
more powerful, safer, flexible, and performant than POSIX shell or Zsh.
|
2021-08-10 13:18:33 -05:00
|
|
|
|
2021-08-10 18:37:51 -05:00
|
|
|
## Guidelines
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-08-10 18:37:51 -05:00
|
|
|
* Write GitHub Flavored Markdown only
|
|
|
|
* Use present tense ("outputs" over "will output")
|
2021-08-23 21:10:27 -05:00
|
|
|
* Prefer term "output" and "display" over ~~print~~
|
2021-08-10 18:37:51 -05:00
|
|
|
* Follow the [naming conventions](#naming-conventions)
|
2021-08-15 15:56:56 -05:00
|
|
|
* Use the official bash path: `#!/bin/bash`
|
|
|
|
* Use of `#!/usr/bin/bash` is outdated
|
2021-08-10 17:47:07 -05:00
|
|
|
* Using `#!/usr/bin/env bash` introduces unnecessary risk
|
2021-08-10 18:37:51 -05:00
|
|
|
* Explicitly export `PATH` in script when possible
|
2021-08-10 17:47:07 -05:00
|
|
|
* Always check script with [`shellcheck`] before releasing
|
|
|
|
* Always use `bc` for *any* floating point math
|
|
|
|
|
|
|
|
[`shellcheck`]: <https://www.shellcheck.net>
|
|
|
|
|
2021-12-18 10:13:23 -06:00
|
|
|
## Security
|
|
|
|
|
|
|
|
This script is expected to be installed for a specific user and only
|
|
|
|
ever run by that user. No additional security vetting for running as an
|
|
|
|
untrusted user has been done.
|
|
|
|
|
2021-08-10 17:47:07 -05:00
|
|
|
## Legal
|
|
|
|
|
|
|
|
Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
|
|
|
Released under Apache-2.0 License
|
2021-08-23 21:27:03 -05:00
|
|
|
Please mention rwxrob.tv'
|
2021-08-10 17:47:07 -05:00
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
HELP[foo]='Foos things.'
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.foo () {
|
2021-08-06 12:48:11 -05:00
|
|
|
_filter "$@" && return $?
|
|
|
|
echo "would foo: $*"
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
HELP[bar]='
|
|
|
|
|
|
|
|
```
|
|
|
|
'"$EXE"' bar
|
|
|
|
```
|
|
|
|
|
|
|
|
Bar the things.'
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.bar() {
|
2021-08-09 18:01:36 -05:00
|
|
|
_buffer "$@" && return $?
|
2021-08-06 12:48:11 -05:00
|
|
|
echo "would bar: $*"
|
|
|
|
}
|
2021-08-29 11:19:03 -05:00
|
|
|
|
|
|
|
HELP[some.config.setting]='Get and set `some.config.setting`.'
|
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.some.config.setting() {
|
|
|
|
x.config some.config.setting "$@"
|
2021-08-29 11:19:03 -05:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
############################## 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 -------------------------
|
2021-08-10 17:59:04 -05:00
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
HELP[usage]='Displays a summary of usage.'
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.usage() {
|
2021-08-06 12:48:11 -05:00
|
|
|
local -a cmds
|
|
|
|
for c in "${COMMANDS[@]}"; do
|
|
|
|
[[ ${c:0:1} =~ _ ]] && continue
|
2021-08-18 19:54:40 -05:00
|
|
|
cmds+=("$c")
|
2021-08-06 12:48:11 -05:00
|
|
|
done
|
2021-08-18 19:54:40 -05:00
|
|
|
local IFS='|'
|
|
|
|
printf "usage: %s (%s)\n" "$EXE" "${cmds[*]}"
|
2021-08-06 12:48:11 -05:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
# --------------------------- help command ---------------------------
|
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
HELP[help]='
|
2021-08-10 17:47:07 -05:00
|
|
|
|
|
|
|
```
|
2021-08-23 21:10:27 -05:00
|
|
|
'"$EXE"' help [COMMAND]
|
2021-08-10 17:47:07 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Displays specific help information. If no argument is passed displays
|
|
|
|
general help information (main). Otherwise, the documentation for the
|
|
|
|
specific argument keyword is displayed, which usually corresponds to
|
2021-08-23 21:10:27 -05:00
|
|
|
a COMMAND name (but not necessarily). All documentation is written in
|
2021-08-10 17:47:07 -05:00
|
|
|
GitHub Flavored Markdown and will displayed as a web page if `pandoc`
|
|
|
|
and `$HELP_BROWSER` are detected, otherwise, just the Markdown is sent
|
|
|
|
to `$PAGER` (default: more).
|
2021-08-10 13:18:33 -05:00
|
|
|
|
2021-08-10 17:47:07 -05:00
|
|
|
Also see `readme` and `usage` commands.
|
|
|
|
'
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.help() {
|
2021-08-18 19:54:40 -05:00
|
|
|
local name="${1:-main}" title body
|
2021-08-10 20:59:02 -05:00
|
|
|
title=$(_help_title "$name") || true
|
2021-08-10 13:18:33 -05:00
|
|
|
if [[ -z "$title" ]]; then
|
2021-08-23 21:10:27 -05:00
|
|
|
body="${HELP[$name]}"
|
2021-08-10 13:18:33 -05:00
|
|
|
title="$EXE $name"
|
|
|
|
[[ $name = main ]] && title="$EXE"
|
|
|
|
else
|
2021-08-23 21:10:27 -05:00
|
|
|
body="${HELP[$name]}"
|
2021-08-10 13:18:33 -05:00
|
|
|
body=${body#*$title}
|
|
|
|
fi
|
2021-08-06 12:48:11 -05:00
|
|
|
local file="/tmp/help-$EXE-$name.html"
|
|
|
|
if _have pandoc ; then
|
|
|
|
if _have "$HELP_BROWSER" && [[ -t 1 ]] ;then
|
2021-08-23 21:10:27 -05:00
|
|
|
pandoc -f gfm -s --metadata title="$title" \
|
2021-08-10 13:18:33 -05:00
|
|
|
-o "$file" <<< "$body"
|
2021-08-06 12:48:11 -05:00
|
|
|
[[ -z "$2" ]] && cd /tmp && exec "$HELP_BROWSER" "$file"
|
|
|
|
return 0
|
|
|
|
fi
|
2021-08-23 21:10:27 -05:00
|
|
|
pandoc -f gfm -s --metadata title="$title" \
|
2021-08-10 13:18:33 -05:00
|
|
|
-t plain <<< "$body" | "$PAGER"
|
2021-08-06 12:48:11 -05:00
|
|
|
return 0
|
|
|
|
fi
|
2021-08-10 13:18:33 -05:00
|
|
|
echo -e "$title\n\n$body" | "$PAGER"
|
2021-08-06 12:48:11 -05:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_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 --------------------------
|
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
HELP[readme]='
|
|
|
|
## Generate `README.md` File
|
2021-08-10 17:47:07 -05:00
|
|
|
|
|
|
|
```
|
2021-08-23 21:10:27 -05:00
|
|
|
'"$EXE"' readme > README.md
|
2021-08-10 17:47:07 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
The `readme` command will output the embedded help documentation in raw
|
|
|
|
GitHub Flavored Markdown suitable for use as a `README.md` file on
|
2021-08-23 21:10:27 -05:00
|
|
|
GitHub or similar hosting service.'
|
2021-08-10 17:47:07 -05:00
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.readme() {
|
2021-08-23 21:10:27 -05:00
|
|
|
_trim "${HELP[main]}"
|
|
|
|
echo
|
2021-08-10 17:47:07 -05:00
|
|
|
while IFS= read -r name; do
|
|
|
|
[[ $name = main ]] && continue
|
2021-08-23 21:10:27 -05:00
|
|
|
body=$(_trim "${HELP[$name]}")
|
|
|
|
[[ $body =~ ^\# ]] || body="## The \`$name\` Command"$'\n\n'$body
|
|
|
|
printf "%s\n\n" "$body"
|
|
|
|
done < <(printf "%s\n" "${!HELP[@]}" | LC_COLLATE=C sort)
|
2021-08-10 18:15:33 -05:00
|
|
|
echo -e "----\n\n*Autogenerated $(date)*\n"
|
2021-08-10 17:47:07 -05:00
|
|
|
}
|
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
# x.json() { _jsonstr "$@"; }
|
|
|
|
# x.urlencode() { _urlencode "$@"; }
|
2021-08-31 08:47:57 -05:00
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
# -------------------------- config command --------------------------
|
|
|
|
|
2021-08-23 21:10:27 -05:00
|
|
|
HELP[config]='
|
|
|
|
|
|
|
|
```
|
|
|
|
'"$EXE"' config
|
|
|
|
'"$EXE"' config KEY
|
|
|
|
'"$EXE"' config KEY VALUE
|
2021-08-24 23:50:39 -05:00
|
|
|
'"$EXE"' config KEY ""
|
2021-08-29 11:19:03 -05:00
|
|
|
'"$EXE"' config keys
|
|
|
|
'"$EXE"' config val[ues]
|
|
|
|
'"$EXE"' config dir[ectory]
|
2021-08-26 08:15:09 -05:00
|
|
|
'"$EXE"' config path [file]
|
|
|
|
'"$EXE"' config edit [file]
|
2021-08-29 11:19:03 -05:00
|
|
|
'"$EXE"' config del[ete]
|
2021-08-23 21:10:27 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
The `config` command is for reading, writing, and displaying standard
|
2021-08-24 23:50:39 -05:00
|
|
|
open desktop configuration properties. Pass an empty string to delete
|
|
|
|
a property.
|
2021-08-23 21:10:27 -05:00
|
|
|
|
|
|
|
### Arguments
|
|
|
|
|
|
|
|
With no arguments outputs all the currently cached configuration
|
2021-08-29 11:19:03 -05:00
|
|
|
settings.
|
2021-08-23 21:10:27 -05:00
|
|
|
|
|
|
|
With a single KEY argument fetches the value for that key and outputs
|
2021-08-26 08:15:09 -05:00
|
|
|
it unless it is one of the following special (reserved) key names:
|
|
|
|
|
2021-08-29 11:19:03 -05:00
|
|
|
* `dir*` full path to config directory
|
2021-08-26 08:15:09 -05:00
|
|
|
* `path` full path to specific config file (default: `values`)
|
|
|
|
* `edit` opens config file in editor (default: `editor` or `$EDITOR)
|
2021-08-29 11:19:03 -05:00
|
|
|
* `keys` output the configuration keys, one per line
|
|
|
|
* `val*` output the configuration values, one per line
|
|
|
|
* `del*` if key argument then delete a specific key, otherwise prompt
|
2021-08-23 21:10:27 -05:00
|
|
|
|
|
|
|
With more than one argument the remaining arguments after the KEY will
|
2021-08-23 21:21:28 -05:00
|
|
|
be combined into the VALUE and written to a `values` file in the
|
2021-08-24 23:50:39 -05:00
|
|
|
configuration directory.
|
|
|
|
|
|
|
|
### Configuration Directory
|
|
|
|
|
|
|
|
The configuration directory path relies on the following environment
|
|
|
|
variables:
|
|
|
|
|
|
|
|
* `EXE` - defaults to name of currently running command ('"$EXE"')
|
|
|
|
* `HOME` - checked for `$HOME/.config/$EXE/values`
|
|
|
|
* `XDG_CONFIG_HOME` - overrides `$HOME/.config`
|
|
|
|
* `CONFIG_DIR` - full path to directory containing `values` file
|
|
|
|
|
|
|
|
The `CONFIG_DIR` always takes priority over anything else if set, but is
|
|
|
|
never implied. If the directory does not exist it will be created the
|
|
|
|
first time a value is set.
|
2021-08-23 21:10:27 -05:00
|
|
|
|
2021-08-23 21:22:43 -05:00
|
|
|
### Configuration `values` File Format
|
2021-08-23 21:10:27 -05:00
|
|
|
|
2021-08-23 21:21:28 -05:00
|
|
|
The file (which is almost always located at
|
|
|
|
`~/.config/'"$EXE"'/values`) uses the simplest possible format to
|
2021-08-23 21:14:42 -05:00
|
|
|
facilitate standard UNIX parsing and filtering with any number of
|
|
|
|
existing tools (and no `jq` dependency).
|
2021-08-23 21:10:27 -05:00
|
|
|
|
|
|
|
* One KEY=VALUE per line
|
|
|
|
* KEYs may be anything but the equal sign (`=`)
|
|
|
|
* VALUEs may be anything but line returns must be escaped
|
|
|
|
|
|
|
|
Note that this is *not* the same as Java properties and other similar
|
|
|
|
format. It is designed for ultimate simplicity, efficiency, and
|
|
|
|
portability.'
|
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.config() {
|
2021-08-26 08:15:09 -05:00
|
|
|
case $1 in
|
2021-08-29 11:19:03 -05:00
|
|
|
dir*) shift; _config_dir "$@"; return $? ;;
|
|
|
|
path) shift; _config_path "$@"; return $? ;;
|
|
|
|
edit) shift; _config_edit "$@"; return $? ;;
|
|
|
|
del*) shift; _config_del "$@"; return $? ;;
|
|
|
|
keys) shift; _config_keys "$@"; return $? ;;
|
|
|
|
val*) shift; _config_vals "$@"; return $? ;;
|
2021-08-26 08:15:09 -05:00
|
|
|
esac
|
2021-08-23 21:10:27 -05:00
|
|
|
case $# in
|
2021-08-31 21:00:16 -05:00
|
|
|
0) _config_dump ;;
|
2021-08-26 08:15:09 -05:00
|
|
|
1) _config_get "$@" ;;
|
|
|
|
*) _config_set "$@" ;;
|
2021-08-23 21:10:27 -05:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_edit() {
|
2021-08-31 22:10:03 -05:00
|
|
|
: "${CONF[editor]:="${EDITOR:=vi}"}"
|
|
|
|
exec "${CONF[editor]}" "$(_config_path "${1:-values}")"
|
2021-08-23 21:10:27 -05:00
|
|
|
}
|
|
|
|
|
2021-08-29 11:19:03 -05:00
|
|
|
_config_del() {
|
|
|
|
if [[ -z "$1" ]];then
|
2021-08-31 22:10:03 -05:00
|
|
|
select key in "${!CONF[@]}"; do
|
2021-08-29 11:19:03 -05:00
|
|
|
_config_del "$key"
|
|
|
|
return $?
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
_config_set "$1" ''
|
|
|
|
}
|
|
|
|
|
2021-08-31 22:10:03 -05:00
|
|
|
_config_keys() { printf "%s\n" "${!CONF[@]}"; }
|
2021-08-29 11:19:03 -05:00
|
|
|
|
2021-08-31 22:10:03 -05:00
|
|
|
_config_vals() { printf "%s\n" "${CONF[@]}"; }
|
2021-08-29 11:19:03 -05:00
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_dir() {
|
2021-08-24 23:50:39 -05:00
|
|
|
local dir="$HOME/.config/$EXE"
|
|
|
|
[[ -n "$XDG_CONFIG_HOME" ]] && dir="$XDG_CONFIG_HOME/$EXE"
|
|
|
|
[[ -n "$CONFIG_DIR" ]] && dir="$CONFIG_DIR"
|
|
|
|
[[ -n "$1" ]] && echo "$dir/$1" && return 0
|
2021-08-26 09:00:16 -05:00
|
|
|
printf "%s" "$dir"
|
|
|
|
[[ -t 1 ]] && echo
|
2021-12-15 22:19:22 -06:00
|
|
|
return 0
|
2021-08-24 23:50:39 -05:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_path() {
|
|
|
|
local file=${1:-values}
|
2021-08-26 09:00:16 -05:00
|
|
|
printf "%s/%s" "$(_config_dir)" "$file"
|
|
|
|
[[ -t 1 ]] && echo
|
2021-12-15 22:19:22 -06:00
|
|
|
return 0
|
2021-08-26 08:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_config_set() {
|
2021-08-23 21:10:27 -05:00
|
|
|
local key="$1"; shift; local val="$*"
|
|
|
|
val="${val//$'\n'/\\n}"
|
2021-08-31 22:10:03 -05:00
|
|
|
CONF["$key"]="$val"
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_write
|
2021-08-23 21:10:27 -05:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:48:11 -05:00
|
|
|
_config_get() {
|
2021-12-15 22:19:22 -06:00
|
|
|
printf "%s" "${CONF[$1]}"
|
2021-08-26 08:48:11 -05:00
|
|
|
[[ -t 1 ]] && echo
|
2021-12-15 22:19:22 -06:00
|
|
|
return 0
|
2021-08-26 08:48:11 -05:00
|
|
|
}
|
2021-08-24 23:50:39 -05:00
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_read() {
|
|
|
|
local values="$(_config_path)"
|
2021-08-24 23:50:39 -05:00
|
|
|
[[ -r "$values" ]] || return 0
|
2021-08-23 21:10:27 -05:00
|
|
|
while IFS= read -r line; do
|
|
|
|
[[ $line =~ ^([^=]+)=(.+)$ ]] || continue
|
2021-08-31 22:10:03 -05:00
|
|
|
CONF["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
|
2021-08-24 23:50:39 -05:00
|
|
|
done < "$values"
|
2021-08-23 21:10:27 -05:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_write() {
|
|
|
|
local dir="$(_config_dir)"
|
2021-08-24 23:50:39 -05:00
|
|
|
mkdir -p "$dir"
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_dump > "$dir/values"
|
2021-08-23 21:10:27 -05:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_config_dump() {
|
2021-08-31 22:10:03 -05:00
|
|
|
(( ${#CONF[@]} == 0 )) && return 0
|
2021-08-23 21:10:27 -05:00
|
|
|
paste -d=\
|
2021-08-31 22:10:03 -05:00
|
|
|
<(printf "%s\n" "${!CONF[@]}") \
|
2021-08-31 22:26:27 -05:00
|
|
|
<(printf "%s\n" "${CONF[@]}") \
|
|
|
|
| sort
|
2021-08-10 13:18:33 -05:00
|
|
|
}
|
2021-08-06 12:48:11 -05:00
|
|
|
|
2021-08-31 08:47:57 -05:00
|
|
|
# ----------------------------- utilities ----------------------------
|
2021-08-10 17:47:07 -05:00
|
|
|
|
2021-08-10 20:32:51 -05:00
|
|
|
_jsonstr() {
|
2021-09-15 12:49:51 -05:00
|
|
|
_checkdep jq || return $?
|
2021-08-10 20:32:51 -05:00
|
|
|
_buffer "$@" && return $?
|
|
|
|
jq -MRsc <<< "$1"
|
|
|
|
}
|
|
|
|
|
2021-08-31 08:47:57 -05:00
|
|
|
_urlencode() {
|
|
|
|
_buffer "$@" && return $?
|
|
|
|
local string="$1"
|
|
|
|
local strlen=${#string}
|
|
|
|
local encoded=""
|
|
|
|
local pos c o
|
|
|
|
for ((pos = 0; pos < strlen; pos++)); do
|
|
|
|
c=${string:$pos:1}
|
|
|
|
case "$c" in
|
|
|
|
[-_.~a-zA-Z0-9]) o="$c" ;;
|
|
|
|
*) printf -v o '%%%02x' "'$c'" ;;
|
|
|
|
esac
|
|
|
|
encoded+="$o"
|
|
|
|
done
|
|
|
|
echo "$encoded"
|
|
|
|
}
|
2021-08-26 08:15:09 -05:00
|
|
|
|
|
|
|
_reduce() {
|
|
|
|
local -n name="${1:?"name of array required"}"
|
|
|
|
while IFS= read -r key; do
|
|
|
|
[[ $key =~ $2 ]] && echo "$key"
|
|
|
|
done < <(printf "%s\n" "${name[@]}")
|
|
|
|
}
|
|
|
|
|
2021-08-31 08:47:57 -05:00
|
|
|
_newest() {
|
|
|
|
IFS=$'\n'
|
|
|
|
local -a f=($(ls -1 --color=never -trd ${1:-.}/* 2>/dev/null))
|
|
|
|
[[ ${#f} > 0 ]] && echo "${f[-1]}"
|
|
|
|
}
|
|
|
|
|
2021-08-26 08:15:09 -05:00
|
|
|
_trim() {
|
|
|
|
local it="${1#"${1%%[![:space:]]*}"}"
|
|
|
|
echo -e "${it%"${it##*[![:space:]]}"}"
|
|
|
|
}
|
|
|
|
|
2021-08-06 12:48:11 -05:00
|
|
|
_have(){ type "$1" &>/dev/null; }
|
|
|
|
|
2021-08-31 08:47:57 -05:00
|
|
|
_checkdep() {
|
|
|
|
_have "$1" && return 0
|
|
|
|
echo "'$EXE' depends on '$1' for this, but not found"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-08-06 12:48:11 -05:00
|
|
|
_filter(){
|
|
|
|
[[ -n "$1" ]] && return 1
|
|
|
|
while IFS= read -ra args; do
|
|
|
|
"${FUNCNAME[1]}" "${args[@]}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2021-08-09 18:01:36 -05:00
|
|
|
_buffer() {
|
2021-08-06 12:48:11 -05:00
|
|
|
[[ -n "$1" ]] && return 1
|
|
|
|
"${FUNCNAME[1]}" "$(</dev/stdin)"
|
|
|
|
}
|
|
|
|
|
2021-08-10 17:59:04 -05:00
|
|
|
# --------------------- completion and delegation --------------------
|
2021-08-27 12:15:05 -05:00
|
|
|
# `complete -C foo foo` > `source <(foo bloated_completion)`
|
2021-08-10 17:59:04 -05:00
|
|
|
|
2021-12-14 16:42:27 -06:00
|
|
|
_config_read
|
|
|
|
_have _initialize && _initialize "$@"
|
|
|
|
|
2021-08-06 12:48:11 -05:00
|
|
|
while IFS= read -r line; do
|
2021-08-31 21:38:13 -05:00
|
|
|
[[ $line =~ ^declare\ -f\ x\. ]] || continue
|
|
|
|
COMMANDS+=( "${line##declare -f x.}" )
|
2021-08-06 12:48:11 -05:00
|
|
|
done < <(declare -F)
|
2021-08-18 19:54:40 -05:00
|
|
|
mapfile -t COMMANDS < \
|
|
|
|
<(LC_COLLATE=C sort < <(printf "%s\n" "${COMMANDS[@]}"))
|
2021-08-06 12:48:11 -05:00
|
|
|
|
|
|
|
if [[ -n $COMP_LINE ]]; then
|
|
|
|
line=${COMP_LINE#* }
|
|
|
|
for c in "${COMMANDS[@]}"; do
|
2021-08-31 21:00:16 -05:00
|
|
|
[[ ${c:0:${#line}} == "${line,,}" ]] && echo "$c"
|
2021-08-06 12:48:11 -05:00
|
|
|
done
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
for c in "${COMMANDS[@]}"; do
|
|
|
|
if [[ $c == "$EXE" ]]; then
|
2021-08-31 21:38:13 -05:00
|
|
|
"x.$EXE" "$@"
|
2021-08-06 12:48:11 -05:00
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -n "$1" ]]; then
|
2021-09-08 09:06:25 -05:00
|
|
|
declare CMD="$1"; shift
|
2021-08-06 12:48:11 -05:00
|
|
|
for c in "${COMMANDS[@]}"; do
|
2021-12-14 16:53:22 -06:00
|
|
|
declare cmd=$(command -v "x.$c")
|
|
|
|
if [[ $c == "$CMD" && -n "$cmd" ]]; then
|
2021-09-08 09:06:25 -05:00
|
|
|
"x.$CMD" "$@"
|
2021-08-06 12:48:11 -05:00
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2021-09-08 09:06:25 -05:00
|
|
|
if _have _alternatives; then
|
|
|
|
_alternatives "$@"
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|
2021-08-31 21:38:13 -05:00
|
|
|
x.usage "$@"
|