Compare commits
No commits in common. "main" and "v0.8.0" have entirely different histories.
182
README.md
182
README.md
|
@ -1,11 +1,5 @@
|
||||||
# Bash Template Command
|
# Bash Template Command
|
||||||
|
|
||||||
*I no longer use this using [Bonzai][] instead. But it does have some good bash tricks to keep around including self-completion.*
|
|
||||||
|
|
||||||
[Bonzai]: <https://github.com/rwxrob/bonzai>
|
|
||||||
|
|
||||||
----
|
|
||||||
|
|
||||||
*This `README.md` is autogenerated.*
|
*This `README.md` is autogenerated.*
|
||||||
|
|
||||||
This is a GitHub template repo that will be copied instead of forked to
|
This is a GitHub template repo that will be copied instead of forked to
|
||||||
|
@ -17,102 +11,17 @@ gh repo create rwxrob/mycmd -p rwxrob/template-bash-command
|
||||||
|
|
||||||
This `cmd` inside can then be renamed and finished.
|
This `cmd` inside can then be renamed and finished.
|
||||||
|
|
||||||
|
Obviously, not all of this is needed for many Bash scripts, but anything
|
||||||
|
with more than two subcommands will benefit from the builtin tab
|
||||||
|
completion, embedded Markdown help documentation support, and included
|
||||||
|
functions (`usage`, `_filter`, `_buffer`, `_have`, etc.)
|
||||||
|
|
||||||
## Naming Conventions
|
## Naming Conventions
|
||||||
|
|
||||||
* Name repos containing single bash commands with `cmd-`
|
* Name repos containing single bash commands with `cmd-`
|
||||||
* Name template repos beginning with `template-`
|
* Name template repos beginning with `template-`
|
||||||
* Start command functions with `x.` to be completed
|
* Start command functions with `command_` to be completed
|
||||||
* Name `CONF` accessors with `x.` and full path
|
* Start command functions with `command__` to not be completed
|
||||||
* Use dot (`.`) pathing in `CONF` key names
|
|
||||||
|
|
||||||
Think of `x` as in "executable" command.
|
|
||||||
|
|
||||||
> ⚠️
|
|
||||||
> 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.
|
|
||||||
|
|
||||||
## 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
|
|
||||||
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`.
|
|
||||||
|
|
||||||
### `_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
|
|
||||||
converting back into an array with `IFS=$'\n'` or just as an in-memory
|
|
||||||
`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.
|
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
|
@ -127,7 +36,7 @@ Optional:
|
||||||
|
|
||||||
## Justification
|
## Justification
|
||||||
|
|
||||||
Bash is the dominant shell scripting language and the official default
|
Bash is the dominate shell scripting language and the official default
|
||||||
Linux interactive shell, which reduces cognitive overhead; every command
|
Linux interactive shell, which reduces cognitive overhead; every command
|
||||||
line *is* a line of code that could be put into script as is. Bash
|
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+
|
scripts are at the core of cloud, containers, and Kubernetes. Bash 4+
|
||||||
|
@ -151,17 +60,11 @@ more powerful, safer, flexible, and performant than POSIX shell or Zsh.
|
||||||
|
|
||||||
[`shellcheck`]: <https://www.shellcheck.net>
|
[`shellcheck`]: <https://www.shellcheck.net>
|
||||||
|
|
||||||
## 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.
|
|
||||||
|
|
||||||
## Legal
|
## Legal
|
||||||
|
|
||||||
Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
||||||
Released under Apache-2.0 License
|
Released under Apache-2.0 License
|
||||||
Please mention rwxrob.tv
|
Please mention <https://rwxrob.tv>
|
||||||
|
|
||||||
## The `bar` Command
|
## The `bar` Command
|
||||||
|
|
||||||
|
@ -177,58 +80,29 @@ Bar the things.
|
||||||
cmd config
|
cmd config
|
||||||
cmd config KEY
|
cmd config KEY
|
||||||
cmd config KEY VALUE
|
cmd config KEY VALUE
|
||||||
cmd config KEY ""
|
|
||||||
cmd config keys
|
|
||||||
cmd config val[ues]
|
|
||||||
cmd config dir[ectory]
|
|
||||||
cmd config path [file]
|
|
||||||
cmd config edit [file]
|
|
||||||
cmd config del[ete]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The `config` command is for reading, writing, and displaying standard
|
The `config` command is for reading, writing, and displaying standard
|
||||||
open desktop configuration properties. Pass an empty string to delete
|
open desktop configurations properties.
|
||||||
a property.
|
|
||||||
|
|
||||||
### Arguments
|
### Arguments
|
||||||
|
|
||||||
With no arguments outputs all the currently cached configuration
|
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 unless it is one of the following special (reserved) key names:
|
it.
|
||||||
|
|
||||||
* `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)
|
|
||||||
* `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
|
|
||||||
|
|
||||||
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 the `config` file in the
|
||||||
configuration directory.
|
standard configuration home location (Search for `XDG_CONFIG_HOME` for
|
||||||
|
more information).
|
||||||
|
|
||||||
### Configuration Directory
|
### Configuration `config` File Format
|
||||||
|
|
||||||
The configuration directory path relies on the following environment
|
The file (which is always named `config`) uses the simplest possible
|
||||||
variables:
|
format to facilitate standard UNIX parsing and filtering with any number
|
||||||
|
of existing tools (and no `jq` dependency).
|
||||||
* `EXE` - defaults to name of currently running command (cmd)
|
|
||||||
* `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.
|
|
||||||
|
|
||||||
### Configuration `values` File Format
|
|
||||||
|
|
||||||
The file (which is almost always located at
|
|
||||||
`~/.config/cmd/values`) uses the simplest possible format to
|
|
||||||
facilitate standard UNIX parsing and filtering with any number of
|
|
||||||
existing tools (and no `jq` dependency).
|
|
||||||
|
|
||||||
* One KEY=VALUE per line
|
* One KEY=VALUE per line
|
||||||
* KEYs may be anything but the equal sign (`=`)
|
* KEYs may be anything but the equal sign (`=`)
|
||||||
|
@ -258,6 +132,18 @@ to `$PAGER` (default: more).
|
||||||
|
|
||||||
Also see `readme` and `usage` commands.
|
Also see `readme` and `usage` commands.
|
||||||
|
|
||||||
|
## Convert to JSON String
|
||||||
|
|
||||||
|
```
|
||||||
|
cmd json STRING
|
||||||
|
cmd json <<< STRING
|
||||||
|
cmd json < FILE
|
||||||
|
cmd json < <(COMMAND)
|
||||||
|
```
|
||||||
|
|
||||||
|
Converts input into JSON string using `jq` (if found) containing only
|
||||||
|
escaped (`\n`) line returns.
|
||||||
|
|
||||||
## Generate `README.md` File
|
## Generate `README.md` File
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -268,15 +154,11 @@ The `readme` command will output the embedded help documentation in raw
|
||||||
GitHub Flavored Markdown suitable for use as a `README.md` file on
|
GitHub Flavored Markdown suitable for use as a `README.md` file on
|
||||||
GitHub or similar hosting service.
|
GitHub or similar hosting service.
|
||||||
|
|
||||||
## The `some.config.setting` Command
|
|
||||||
|
|
||||||
Get and set `some.config.setting`.
|
|
||||||
|
|
||||||
## The `usage` Command
|
## The `usage` Command
|
||||||
|
|
||||||
Displays a summary of usage.
|
Displays a summary of usage.
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
*Autogenerated Sat Dec 18 11:13:17 AM EST 2021*
|
*Autogenerated Mon Aug 23 10:09:44 PM EDT 2021*
|
||||||
|
|
||||||
|
|
455
cmd
455
cmd
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env bash
|
#!/bin/bash
|
||||||
# shellcheck disable=SC2016
|
# shellcheck disable=SC2016
|
||||||
set -e
|
set -e
|
||||||
# export PATH="/bin:/usr/bin:/usr/local/bin" # safer, if you can
|
# export PATH="/bin:/usr/bin:/usr/local/bin" # safer, if you can
|
||||||
|
@ -9,30 +9,10 @@ set -e
|
||||||
: "${EDITOR:=vi}"
|
: "${EDITOR:=vi}"
|
||||||
: "${HELP_BROWSER:=}"
|
: "${HELP_BROWSER:=}"
|
||||||
: "${EXE:="${0##*/}"}"
|
: "${EXE:="${0##*/}"}"
|
||||||
|
: "${XDG_CONFIG_HOME:="$HOME/.config"}"
|
||||||
|
|
||||||
declare -A HELP
|
declare -A HELP
|
||||||
declare -A CONF
|
declare -A CONFIG
|
||||||
|
|
||||||
# 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'
|
|
||||||
|
|
||||||
_initialize() {
|
|
||||||
: # put initialization code here
|
|
||||||
}
|
|
||||||
|
|
||||||
_alternatives() {
|
|
||||||
# put alternative argument possibilities here
|
|
||||||
if [[ $CMD = f ]];then
|
|
||||||
x.foo "$@"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
HELP[main]='
|
HELP[main]='
|
||||||
# Bash Template Command
|
# Bash Template Command
|
||||||
|
@ -48,102 +28,17 @@ gh repo create rwxrob/mycmd -p rwxrob/template-bash-command
|
||||||
|
|
||||||
This `cmd` inside can then be renamed and finished.
|
This `cmd` inside can then be renamed and finished.
|
||||||
|
|
||||||
|
Obviously, not all of this is needed for many Bash scripts, but anything
|
||||||
|
with more than two subcommands will benefit from the builtin tab
|
||||||
|
completion, embedded Markdown help documentation support, and included
|
||||||
|
functions (`usage`, `_filter`, `_buffer`, `_have`, etc.)
|
||||||
|
|
||||||
## Naming Conventions
|
## Naming Conventions
|
||||||
|
|
||||||
* Name repos containing single bash commands with `cmd-`
|
* Name repos containing single bash commands with `cmd-`
|
||||||
* Name template repos beginning with `template-`
|
* Name template repos beginning with `template-`
|
||||||
* Start command functions with `x.` to be completed
|
* Start command functions with `command_` to be completed
|
||||||
* Name `CONF` accessors with `x.` and full path
|
* Start command functions with `command__` to not be completed
|
||||||
* Use dot (`.`) pathing in `CONF` key names
|
|
||||||
|
|
||||||
Think of `x` as in "executable" command.
|
|
||||||
|
|
||||||
> ⚠️
|
|
||||||
> 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.
|
|
||||||
|
|
||||||
## 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
|
|
||||||
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`.
|
|
||||||
|
|
||||||
### `_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
|
|
||||||
converting back into an array with `IFS=$'"'\\\n'"'` or just as an in-memory
|
|
||||||
`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.
|
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
|
@ -158,7 +53,7 @@ Optional:
|
||||||
|
|
||||||
## Justification
|
## Justification
|
||||||
|
|
||||||
Bash is the dominant shell scripting language and the official default
|
Bash is the dominate shell scripting language and the official default
|
||||||
Linux interactive shell, which reduces cognitive overhead; every command
|
Linux interactive shell, which reduces cognitive overhead; every command
|
||||||
line *is* a line of code that could be put into script as is. Bash
|
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+
|
scripts are at the core of cloud, containers, and Kubernetes. Bash 4+
|
||||||
|
@ -182,21 +77,15 @@ more powerful, safer, flexible, and performant than POSIX shell or Zsh.
|
||||||
|
|
||||||
[`shellcheck`]: <https://www.shellcheck.net>
|
[`shellcheck`]: <https://www.shellcheck.net>
|
||||||
|
|
||||||
## 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.
|
|
||||||
|
|
||||||
## Legal
|
## Legal
|
||||||
|
|
||||||
Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
Copyright 2021 Rob Muhlestein <rob@rwx.gg>
|
||||||
Released under Apache-2.0 License
|
Released under Apache-2.0 License
|
||||||
Please mention rwxrob.tv'
|
Please mention <https://rwxrob.tv>'
|
||||||
|
|
||||||
HELP[foo]='Foos things.'
|
HELP[foo]='Foos things.'
|
||||||
|
|
||||||
x.foo () {
|
command_foo() {
|
||||||
_filter "$@" && return $?
|
_filter "$@" && return $?
|
||||||
echo "would foo: $*"
|
echo "would foo: $*"
|
||||||
}
|
}
|
||||||
|
@ -209,33 +98,37 @@ HELP[bar]='
|
||||||
|
|
||||||
Bar the things.'
|
Bar the things.'
|
||||||
|
|
||||||
x.bar() {
|
command_bar() {
|
||||||
_buffer "$@" && return $?
|
_buffer "$@" && return $?
|
||||||
echo "would bar: $*"
|
echo "would bar: $*"
|
||||||
}
|
}
|
||||||
|
|
||||||
HELP[some.config.setting]='Get and set `some.config.setting`.'
|
HELP[json]='
|
||||||
|
## Convert to JSON String
|
||||||
|
|
||||||
x.some.config.setting() {
|
```
|
||||||
x.config some.config.setting "$@"
|
'"$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: $*"
|
||||||
}
|
}
|
||||||
|
|
||||||
############################## BOILERPLATE ###########################
|
# ------------------ builtin commands and functions ------------------
|
||||||
## Everything from here to the end of file can be snipped and updated
|
# (https://github.com/rwxrob/template-bash-command)
|
||||||
## with latest from https://github.com/rwxrob/template-bash-command.
|
|
||||||
|
|
||||||
# ------------------------------- usage ------------------------------
|
HELP[usage]='Displays a summary of usage.'
|
||||||
|
|
||||||
HELP[usage]='
|
command_usage() {
|
||||||
|
|
||||||
```
|
|
||||||
'"$EXE"' usage
|
|
||||||
```
|
|
||||||
|
|
||||||
Display all possible commands. Note that this is usually easier by
|
|
||||||
simply using tab completion instead.'
|
|
||||||
|
|
||||||
x.usage() {
|
|
||||||
local -a cmds
|
local -a cmds
|
||||||
for c in "${COMMANDS[@]}"; do
|
for c in "${COMMANDS[@]}"; do
|
||||||
[[ ${c:0:1} =~ _ ]] && continue
|
[[ ${c:0:1} =~ _ ]] && continue
|
||||||
|
@ -245,8 +138,6 @@ x.usage() {
|
||||||
printf "usage: %s (%s)\n" "$EXE" "${cmds[*]}"
|
printf "usage: %s (%s)\n" "$EXE" "${cmds[*]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ------------------------------- help -------------------------------
|
|
||||||
|
|
||||||
HELP[help]='
|
HELP[help]='
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -264,20 +155,18 @@ to `$PAGER` (default: more).
|
||||||
Also see `readme` and `usage` commands.
|
Also see `readme` and `usage` commands.
|
||||||
'
|
'
|
||||||
|
|
||||||
x.help() {
|
command_help() {
|
||||||
local name="${1:-main}" title body file
|
local name="${1:-main}" title body
|
||||||
title=$(_help_title "$name") || true
|
title=$(_help_title "$name") || true
|
||||||
if [[ -z "$title" ]]; then
|
if [[ -z "$title" ]]; then
|
||||||
|
body="${HELP[$name]}"
|
||||||
title="$EXE $name"
|
title="$EXE $name"
|
||||||
[[ $name == main ]] && title="$EXE"
|
[[ $name = main ]] && title="$EXE"
|
||||||
fi
|
|
||||||
if [[ $name == main ]]; then
|
|
||||||
body=$(x.readme)
|
|
||||||
body=${body#*$title}
|
|
||||||
else
|
else
|
||||||
body="${HELP[$name]}"
|
body="${HELP[$name]}"
|
||||||
|
body=${body#*$title}
|
||||||
fi
|
fi
|
||||||
file="/tmp/help-$EXE-$name.html"
|
local file="/tmp/help-$EXE-$name.html"
|
||||||
if _have pandoc ; then
|
if _have pandoc ; then
|
||||||
if _have "$HELP_BROWSER" && [[ -t 1 ]] ;then
|
if _have "$HELP_BROWSER" && [[ -t 1 ]] ;then
|
||||||
pandoc -f gfm -s --metadata title="$title" \
|
pandoc -f gfm -s --metadata title="$title" \
|
||||||
|
@ -292,18 +181,6 @@ x.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 ------------------------------
|
|
||||||
|
|
||||||
HELP[readme]='
|
HELP[readme]='
|
||||||
## Generate `README.md` File
|
## Generate `README.md` File
|
||||||
|
|
||||||
|
@ -315,7 +192,7 @@ The `readme` command will output the embedded help documentation in raw
|
||||||
GitHub Flavored Markdown suitable for use as a `README.md` file on
|
GitHub Flavored Markdown suitable for use as a `README.md` file on
|
||||||
GitHub or similar hosting service.'
|
GitHub or similar hosting service.'
|
||||||
|
|
||||||
x.readme() {
|
command_readme() {
|
||||||
_trim "${HELP[main]}"
|
_trim "${HELP[main]}"
|
||||||
echo
|
echo
|
||||||
while IFS= read -r name; do
|
while IFS= read -r name; do
|
||||||
|
@ -327,196 +204,87 @@ x.readme() {
|
||||||
echo -e "----\n\n*Autogenerated $(date)*\n"
|
echo -e "----\n\n*Autogenerated $(date)*\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ------------------------------ config ------------------------------
|
|
||||||
|
|
||||||
HELP[config]='
|
HELP[config]='
|
||||||
|
|
||||||
```
|
```
|
||||||
'"$EXE"' config
|
'"$EXE"' config
|
||||||
'"$EXE"' config KEY
|
'"$EXE"' config KEY
|
||||||
'"$EXE"' config.set KEY VALUE
|
'"$EXE"' config KEY VALUE
|
||||||
'"$EXE"' config.set KEY ""
|
|
||||||
'"$EXE"' config.keys
|
|
||||||
'"$EXE"' config.values
|
|
||||||
'"$EXE"' config.directory
|
|
||||||
'"$EXE"' config.path [file]
|
|
||||||
'"$EXE"' config.edit [file]
|
|
||||||
'"$EXE"' config.delete
|
|
||||||
'"$EXE"' config.read
|
|
||||||
'"$EXE"' config.write
|
|
||||||
'"$EXE"' config.dump
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The `config` command is for reading, writing, and displaying standard
|
The `config` command is for reading, writing, and displaying standard
|
||||||
open desktop configuration properties.
|
open desktop configurations properties.
|
||||||
|
|
||||||
### Arguments
|
### Arguments
|
||||||
|
|
||||||
With no arguments calls `dump` and outputs all the currently cached
|
With no arguments outputs all the currently cached configuration
|
||||||
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 unless it is one of the following special (reserved) key names:
|
it.
|
||||||
|
|
||||||
* `directory` full path to config directory
|
|
||||||
* `path` full path to specific config file (default: `values`)
|
|
||||||
* `edit` opens config file in editor (default: `editor` or `$EDITOR)
|
|
||||||
* `keys` output the configuration keys, one per line
|
|
||||||
* `values` output the configuration values, one per line
|
|
||||||
* `delete` if key argument then delete a specific key, otherwise prompt
|
|
||||||
* `read` reads the configuration file into CONF associative array
|
|
||||||
* `write` write the CONF associative array to the configuration file
|
|
||||||
* `dump` write the flattened CONF associative array to standard output
|
|
||||||
|
|
||||||
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 the `config` file in the
|
||||||
configuration directory.
|
standard configuration home location (Search for `XDG_CONFIG_HOME` for
|
||||||
|
more information).
|
||||||
|
|
||||||
### Configuration Directory
|
### Configuration `config` File Format
|
||||||
|
|
||||||
The configuration directory path relies on the following environment
|
The file (which is always named `config`) uses the simplest possible
|
||||||
variables:
|
format to facilitate standard UNIX parsing and filtering with any number
|
||||||
|
of existing tools (and no `jq` dependency).
|
||||||
* `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.
|
|
||||||
|
|
||||||
### Configuration `values` File Format
|
|
||||||
|
|
||||||
The file (which is almost always located at
|
|
||||||
`~/.config/'"$EXE"'/values`) uses the simplest possible format to
|
|
||||||
facilitate standard UNIX parsing and filtering with any number of
|
|
||||||
existing tools (and no `jq` dependency).
|
|
||||||
|
|
||||||
* One KEY=VALUE per line
|
* One KEY=VALUE per line
|
||||||
* KEYs may be anything but the equal sign (`=`)
|
* KEYs may be anything but the equal sign (`=`)
|
||||||
* VALUEs may be anything but line returns must be escaped
|
* VALUEs may be anything but line returns must be escaped
|
||||||
|
|
||||||
Note that, although similar, this is *not* the same as Java properties
|
Note that this is *not* the same as Java properties and other similar
|
||||||
and other similar format. It is designed for ultimate simplicity,
|
format. It is designed for ultimate simplicity, efficiency, and
|
||||||
efficiency, and portability.'
|
portability.'
|
||||||
|
|
||||||
x.config() {
|
command_config() {
|
||||||
case $# in
|
case $# in
|
||||||
0) x.config.dump ;;
|
0) _dump_config ;;
|
||||||
1) x.config.get "$@" ;;
|
1) printf "${CONFIG[$1]}" ;;
|
||||||
*) x.config.set "$@" ;;
|
*) _set_config "$@" ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
x.config.edit() {
|
_help_title() {
|
||||||
: "${CONF[editor]:="${EDITOR:=vi}"}"
|
_filter "$@" && return $?;
|
||||||
exec "${CONF[editor]}" "$(x.config.path "${1:-values}")"
|
local name="$1"
|
||||||
|
while IFS= read -r line; do
|
||||||
|
[[ $line =~ ^[:space]*$ ]] && continue
|
||||||
|
[[ $line =~ ^#\ (.+) ]] && echo "${BASH_REMATCH[1]}" && return 0
|
||||||
|
return 1
|
||||||
|
done <<< "${HELP[$name]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
x.config.delete() {
|
_set_config() {
|
||||||
if [[ -z "$1" ]];then
|
|
||||||
select key in "${!CONF[@]}"; do
|
|
||||||
x.config.delete "$key"
|
|
||||||
return $?
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
x.config.set "$1" ''
|
|
||||||
}
|
|
||||||
|
|
||||||
x.config.keys() { printf "%s\n" "${!CONF[@]}"; }
|
|
||||||
|
|
||||||
x.config.values() { printf "%s\n" "${CONF[@]}"; }
|
|
||||||
|
|
||||||
x.config.dir() {
|
|
||||||
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
|
|
||||||
printf "%s" "$dir"
|
|
||||||
[[ -t 1 ]] && echo
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
x.config.path() {
|
|
||||||
local file=${1:-values}
|
|
||||||
printf "%s/%s" "$(x.config.dir)" "$file"
|
|
||||||
[[ -t 1 ]] && echo
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
x.config.set() {
|
|
||||||
local key="$1"; shift; local val="$*"
|
local key="$1"; shift; local val="$*"
|
||||||
val="${val//$'\n'/\\n}"
|
val="${val//$'\n'/\\n}"
|
||||||
CONF["$key"]="$val"
|
CONFIG["$key"]="$val"
|
||||||
x.config.write
|
_write_config
|
||||||
}
|
}
|
||||||
|
|
||||||
x.config.get() {
|
_read_config() {
|
||||||
printf "%s" "${CONF[$1]}"
|
local path="$XDG_CONFIG_HOME/$EXE/config"
|
||||||
[[ -t 1 ]] && echo
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
x.config.read() {
|
|
||||||
local values="$(x.config.path)"
|
|
||||||
[[ -r "$values" ]] || return 0
|
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
[[ $line =~ ^([^=]+)=(.+)$ ]] || continue
|
[[ $line =~ ^([^=]+)=(.+)$ ]] || continue
|
||||||
CONF["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
|
CONFIG["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
|
||||||
done < "$values"
|
done < "$path"
|
||||||
}
|
}
|
||||||
|
|
||||||
x.config.write() {
|
_write_config() {
|
||||||
local dir="$(x.config.dir)"
|
local path="$XDG_CONFIG_HOME/$EXE/config"
|
||||||
mkdir -p "$dir"
|
mkdir -p "${path%/config}"
|
||||||
x.config.dump > "$dir/values"
|
_dump_config > "$path"
|
||||||
}
|
}
|
||||||
|
|
||||||
x.config.dump() {
|
_dump_config() {
|
||||||
(( ${#CONF[@]} == 0 )) && return 0
|
|
||||||
paste -d=\
|
paste -d=\
|
||||||
<(printf "%s\n" "${!CONF[@]}") \
|
<(printf "%s\n" "${!CONFIG[@]}") \
|
||||||
<(printf "%s\n" "${CONF[@]}") \
|
<(printf "%s\n" "${CONFIG[@]}")
|
||||||
| sort
|
|
||||||
}
|
|
||||||
|
|
||||||
# ----------------------------- utilities ----------------------------
|
|
||||||
|
|
||||||
_jsonstr() {
|
|
||||||
_checkdep jq || return $?
|
|
||||||
_buffer "$@" && return $?
|
|
||||||
jq -MRsc <<< "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
_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"
|
|
||||||
}
|
|
||||||
|
|
||||||
_reduce() {
|
|
||||||
local -n name="${1:?"name of array required"}"
|
|
||||||
while IFS= read -r key; do
|
|
||||||
[[ $key =~ $2 ]] && echo "$key"
|
|
||||||
done < <(printf "%s\n" "${name[@]}")
|
|
||||||
}
|
|
||||||
|
|
||||||
_newest() {
|
|
||||||
IFS=$'\n'
|
|
||||||
mapfile -t f < <(ls -1 --color=never -trd "${1:-.}"/* 2>/dev/null)
|
|
||||||
[[ ${#f} > 0 ]] && echo "${f[-1]}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_trim() {
|
_trim() {
|
||||||
|
@ -524,16 +292,13 @@ _trim() {
|
||||||
echo -e "${it%"${it##*[![:space:]]}"}"
|
echo -e "${it%"${it##*[![:space:]]}"}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_join() { local IFS="$1"; shift; echo "$*"; }
|
_jsonstr() {
|
||||||
|
_buffer "$@" && return $?
|
||||||
|
jq -MRsc <<< "$1"
|
||||||
|
}
|
||||||
|
|
||||||
_have(){ type "$1" &>/dev/null; }
|
_have(){ type "$1" &>/dev/null; }
|
||||||
|
|
||||||
_checkdep() {
|
|
||||||
_have "$1" && return 0
|
|
||||||
echo "'$EXE' depends on '$1' for this, but not found"
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
_filter(){
|
_filter(){
|
||||||
[[ -n "$1" ]] && return 1
|
[[ -n "$1" ]] && return 1
|
||||||
while IFS= read -ra args; do
|
while IFS= read -ra args; do
|
||||||
|
@ -546,32 +311,12 @@ _buffer() {
|
||||||
"${FUNCNAME[1]}" "$(</dev/stdin)"
|
"${FUNCNAME[1]}" "$(</dev/stdin)"
|
||||||
}
|
}
|
||||||
|
|
||||||
_prompt() {
|
|
||||||
local key="$1" def="$2" regx="$3" value first=yes
|
|
||||||
shift 3
|
|
||||||
local text="${*:-Enter value for %s [%s]: }"
|
|
||||||
[[ -z "$key" ]] && echo "Missing prompt key" >&2 && return 1
|
|
||||||
[[ -z "$regx" ]] && echo "Missing valid regx" >&2 && return 1
|
|
||||||
while [[ ! $value =~ $regx ]];do
|
|
||||||
printf "$text" "$key" "$def" >&2
|
|
||||||
IFS= read -r value
|
|
||||||
[[ -z "$value" ]] && value="$def"
|
|
||||||
[[ $value =~ ^\ +$ ]] && value=""
|
|
||||||
[[ -n "$first" ]] && unset first && continue
|
|
||||||
echo "Must match /$regx/" >&2
|
|
||||||
done
|
|
||||||
_trim "$value"
|
|
||||||
}
|
|
||||||
|
|
||||||
# --------------------- completion and delegation --------------------
|
# --------------------- completion and delegation --------------------
|
||||||
# `complete -C foo foo` > `source <(foo bloated_completion)`
|
# (better than . <(foo bloated_completion) in .bashrc)
|
||||||
|
|
||||||
x.config.read
|
|
||||||
_have _initialize && _initialize "$@"
|
|
||||||
|
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
[[ $line =~ ^declare\ -f\ x\. ]] || continue
|
[[ $line =~ ^declare\ -f\ command_ ]] || continue
|
||||||
COMMANDS+=( "${line##declare -f x.}" )
|
COMMANDS+=( "${line##declare -f command_}" )
|
||||||
done < <(declare -F)
|
done < <(declare -F)
|
||||||
mapfile -t COMMANDS < \
|
mapfile -t COMMANDS < \
|
||||||
<(LC_COLLATE=C sort < <(printf "%s\n" "${COMMANDS[@]}"))
|
<(LC_COLLATE=C sort < <(printf "%s\n" "${COMMANDS[@]}"))
|
||||||
|
@ -579,32 +324,28 @@ mapfile -t COMMANDS < \
|
||||||
if [[ -n $COMP_LINE ]]; then
|
if [[ -n $COMP_LINE ]]; then
|
||||||
line=${COMP_LINE#* }
|
line=${COMP_LINE#* }
|
||||||
for c in "${COMMANDS[@]}"; do
|
for c in "${COMMANDS[@]}"; do
|
||||||
[[ ${c:0:${#line}} == "${line,,}" ]] && echo "$c"
|
[[ ${c:0:${#line}} == "${line,,}" && ${c:0:1} != _ ]] && echo "$c"
|
||||||
done
|
done
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for c in "${COMMANDS[@]}"; do
|
for c in "${COMMANDS[@]}"; do
|
||||||
if [[ $c == "$EXE" ]]; then
|
if [[ $c == "$EXE" ]]; then
|
||||||
"x.$EXE" "$@"
|
"command_$EXE" "$@"
|
||||||
exit $?
|
exit $?
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
_read_config
|
||||||
|
|
||||||
if [[ -n "$1" ]]; then
|
if [[ -n "$1" ]]; then
|
||||||
declare CMD="$1"; shift
|
declare cmd="$1"; shift
|
||||||
for c in "${COMMANDS[@]}"; do
|
for c in "${COMMANDS[@]}"; do
|
||||||
declare cmd=$(command -v "x.$c")
|
if [[ $c == "$cmd" ]]; then
|
||||||
if [[ $c == "$CMD" && -n "$cmd" ]]; then
|
"command_$cmd" "$@"
|
||||||
"x.$CMD" "$@"
|
|
||||||
exit $?
|
exit $?
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if _have _alternatives; then
|
command_usage "$@"
|
||||||
_alternatives "$@"
|
|
||||||
exit $?
|
|
||||||
fi
|
|
||||||
|
|
||||||
x.usage "$@"
|
|
||||||
|
|
Loading…
Reference in New Issue