-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bdc825e
Showing
5 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
## | ||
## crdp argument parsing functions | ||
## | ||
|
||
function args-print-version { | ||
echo "$0 version $VERSION" | ||
exit 0 | ||
} | ||
|
||
function args-print-help { | ||
echo "usage: $0 <command> [<args>]" | ||
echo -e "\tCOMMAND\t\t\t\tDESCRIPTION" | ||
for cmd in "${!ACTIONS[@]}" | ||
do | ||
cmdhelp=${ACTIONS_HELP[$cmd]} | ||
echo -e "\t$cmd\t\t\t\t$cmdhelp" | ||
done | ||
} | ||
|
||
# register actions provided by this file | ||
ACTIONS[help]=args-print-help | ||
ACTIONS_HELP[help]="prints this help message" | ||
ACTIONS[version]=args-print-version | ||
ACTIONS_HELP[version]="prints the version string" | ||
|
||
# below is mostly a blatant copy of https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash | ||
|
||
getopt --test > /dev/null | ||
if [[ $? -ne 4 ]]; then | ||
echo "You need getopt installed to use crdp. `getopt --test` failed." | ||
exit 1 | ||
fi | ||
|
||
# using : means that option has a value (e.g. --verbosity 2) | ||
ARGS_OPTIONS= | ||
ARGS_LONGOPTIONS= | ||
|
||
# -temporarily store output to be able to check for errors | ||
# -e.g. use “--options” parameter by name to activate quoting/enhanced mode | ||
# -pass arguments only via -- "$@" to separate them correctly | ||
ARGS_PARSED=$(getopt --options=$ARGS_OPTIONS --longoptions=$ARGS_LONGOPTIONS --name "$0" -- "$@") | ||
if [[ $? -ne 0 ]]; then | ||
# e.g. $? == 1 | ||
# then getopt has complained about wrong arguments to stdout | ||
exit 2 | ||
fi | ||
|
||
# read getopt’s output this way to handle the quoting right: | ||
eval set -- "$ARGS_PARSED" | ||
|
||
# now enjoy the options in order and nicely split until we see -- | ||
while true; do | ||
case "$1" in | ||
# -d|--debug) | ||
# d=y | ||
# shift | ||
# ;; | ||
# -f|--force) | ||
# f=y | ||
# shift | ||
# ;; | ||
# -v|--verbose) | ||
# v=y | ||
# shift | ||
# ;; | ||
# -o|--output) | ||
# outFile="$2" | ||
# shift 2 | ||
# ;; | ||
--) | ||
shift | ||
break | ||
;; | ||
*) | ||
echo "Programming error" | ||
exit 3 | ||
;; | ||
esac | ||
done | ||
|
||
# handle non-option arguments | ||
if [[ $# -ne 1 ]]; then | ||
args-print-help | ||
exit 4 | ||
fi | ||
|
||
# read command into variable and shift $1 into first argument | ||
COMMAND="$1" | ||
shift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## | ||
## crp configuration related functions | ||
## | ||
|
||
function cfg-create-file { | ||
# creates config dir if not exists | ||
[[ -d $CONFIG_DIR ]] || mkdir -p $CONFIG_DIR | ||
|
||
# creates config file if not exists | ||
if [[ ! -f $CONFIG_PATH ]] | ||
then | ||
echo $1 | ||
cat > "$CONFIG_PATH" <<EOF | ||
[general] | ||
use_secret_tool=1 | ||
EOF | ||
fi | ||
} | ||
|
||
# cfg_get <section> <variable> prints the variable's value | ||
# cfg_get <secion> reads all variable lines under <section> and prefix them with cfg_<section>_ | ||
function cfg-get { | ||
# when you forget how you did this: | ||
# https://stackoverflow.com/questions/3717772/regex-grep-for-multi-line-search-needed | ||
# https://unix.stackexchange.com/questions/13466/can-grep-output-only-specified-groupings-that-match | ||
# because -z replaces $ with \0 and bash warns when it is on the string, delete it with tr | ||
[[ ! -z $2 ]] && grep -Poz '(?s).*?\['$1'\].*?'$2'=\K\N*' $CONFIG_PATH | tr -d '\0' \ | ||
|| grep -Poz '(?s).*?\['$1'\]\n\K.*?(?=$|\[)' $CONFIG_PATH | tr '\0' '\n' | grep '=' | sed 's/^\(.*\)$/cfg_'$1'_\1/' | ||
} | ||
|
||
# cfg_read_section <section> | ||
# populate variables cfg_<section>_<varname> with variable values | ||
function cfg-read-section { | ||
local vals=$(cfg-get $1) | ||
# eval will byte me later, this needs fixing | ||
eval "$vals" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
crdp.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#_CONFIG_PATH=~/.local/share/crdp.cfg | ||
_CONFIG_PATH=crdp.cfg | ||
CONFIG_PATH=$(realpath $_CONFIG_PATH) | ||
CONFIG_DIR=$(dirname $CONFIG_PATH) | ||
VERSION=0.0.1 | ||
|
||
# ACTIONS is a map between command and function that performs that action | ||
# ACTIONS_HELP is the help text printed when someone types help | ||
declare -A ACTIONS | ||
declare -A ACTIONS_HELP | ||
|
||
# source functions | ||
. cfg.sh | ||
|
||
# parse options and put command into $COMMAND and arguments into $* | ||
. args.sh | ||
|
||
# creates config file if not exists | ||
cfg-create-file | ||
|
||
# parses the general config section | ||
cfg-read-section general | ||
|
||
# grap function that operates on that command | ||
action=${ACTIONS[$COMMAND]} | ||
|
||
[[ -z "$action" ]] && { echo "unknown command $COMMAND"; echo "type '$0 help' for help"; exit 5; } || $action $* | ||
|
||
exit 0 |