Bump version to 0.1.3, fix wide table, add -l option

This commit is contained in:
ge 2022-12-13 01:48:38 +03:00
parent e2532bd133
commit 5a77aea485
2 changed files with 229 additions and 68 deletions

View File

@ -14,7 +14,7 @@ Currently piglet can:
Just copy `piglet` to your PATH. For example: Just copy `piglet` to your PATH. For example:
```sh ```sh
cp piglet /usr/local/bin/ install -Dm755 piglet /usr/local/bin/piglet
``` ```
Install [jq](https://stedolan.github.io/jq/) to enable pretty output. Install [jq](https://stedolan.github.io/jq/) to enable pretty output.

295
piglet
View File

@ -27,7 +27,7 @@
# #
# For more information, please refer to <http://unlicense.org/> # For more information, please refer to <http://unlicense.org/>
piglet_version='0.1.2' piglet_version='0.1.3'
piglet_conf="${HOME}/.config/piglet.conf" piglet_conf="${HOME}/.config/piglet.conf"
print_help() { print_help() {
@ -64,7 +64,6 @@ For more info, please refference to original API Documentation:
This software is not affiliated with Porkbun LLC <https://porkbun.com/> This software is not affiliated with Porkbun LLC <https://porkbun.com/>
License: The Unlicense <http://unlicense.org/> License: The Unlicense <http://unlicense.org/>
EOF EOF
exit 0
} }
print_help_create() { print_help_create() {
@ -88,7 +87,6 @@ Arguments:
prio (optional) [default: 0] prio (optional) [default: 0]
The priority of the record for those that support it. The priority of the record for those that support it.
EOF EOF
exit 0
} }
print_help_edit() { print_help_edit() {
@ -114,7 +112,6 @@ Arguments:
prio (optional) [default: 0] prio (optional) [default: 0]
The priority of the record for those that support it. The priority of the record for those that support it.
EOF EOF
exit 0
} }
print_help_delete() { print_help_delete() {
@ -127,7 +124,6 @@ Arguments:
id id
DNS record ID. DNS record ID.
EOF EOF
exit 0
} }
print_help_retrieve() { print_help_retrieve() {
@ -137,14 +133,13 @@ for a particular record ID.
Usage: piglet [options...] retrieve [arg=value ...] Usage: piglet [options...] retrieve [arg=value ...]
Retrieve options: Options:
-h, --help print this help message and exit. -l, --limit line length limit for table view [default: 36]
Arguments: Arguments:
id (optional) id (optional)
DNS record ID. DNS record ID.
EOF EOF
exit 0
} }
# ----------------------------------------- # # ----------------------------------------- #
@ -249,8 +244,8 @@ piglet_config() {
"Enter Porkbun DNS API keys. Press ^C to cancel." \ "Enter Porkbun DNS API keys. Press ^C to cancel." \
"Help: https://kb.porkbun.com/article/190-getting-started-with-the-porkbun-dns-api" "Help: https://kb.porkbun.com/article/190-getting-started-with-the-porkbun-dns-api"
printf '%s ' 'API_Key:'; read -r api_key printf 'API_Key: '; read -r api_key
printf '%s ' 'Secret_API_Key:'; read -r secret_api_key printf 'Secret_API_Key: '; read -r secret_api_key
cat > "$piglet_conf" <<- EOF cat > "$piglet_conf" <<- EOF
# Porkbun DNS API credentials # Porkbun DNS API credentials
@ -263,17 +258,50 @@ EOF
piglet_create() { piglet_create() {
while [ "$#" -ne 0 ]; do while [ "$#" -ne 0 ]; do
case "$1" in case "$1" in
-c|--config|--config=*) opts "$1" "$2"; piglet_conf="$val"; shift "$sft";; -c|--config|--config=*)
-d|--domain|--domain=*) opts "$1" "$2"; domain="$val"; shift "$sft";; opts "$1" "$2"
-j|--json) raw_json=1; shift;; piglet_conf="$val"
-h|--help) print_help_create;; shift "$sft"
name=*) name="${1#*=}"; shift;; ;;
type=*) type="${1#*=}"; shift;; -d|--domain|--domain=*)
content=*) content="${1#*=}"; shift;; opts "$1" "$2"
ttl=*) ttl="${1#*=}"; shift;; domain="$val"
prio=*) prio="${1#*=}"; shift;; shift "$sft"
-*) echo "Unknown option: $1" >&2; exit 1;; ;;
*) echo "Unknown key: $1" >&2; exit 1;; -j|--json)
raw_json=1
shift;;
-h|--help)
print_help_create
exit 0
;;
name=*)
name="${1#*=}"
shift
;;
type=*)
type="${1#*=}"
shift
;;
content=*)
content="${1#*=}"
shift
;;
ttl=*)
ttl="${1#*=}"
shift
;;
prio=*)
prio="${1#*=}"
shift
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
*)
echo "Unknown key: $1" >&2
exit 1
esac esac
done done
@ -285,18 +313,55 @@ piglet_create() {
piglet_edit() { piglet_edit() {
while [ "$#" -ne 0 ]; do while [ "$#" -ne 0 ]; do
case "$1" in case "$1" in
-c|--config|--config=*) opts "$1" "$2"; piglet_conf="$val"; shift "$sft";; -c|--config|--config=*)
-d|--domain|--domain=*) opts "$1" "$2"; domain="$val"; shift "$sft";; opts "$1" "$2"
-j|--json) raw_json=1; shift;; piglet_conf="$val"
-h|--help) print_help_edit;; shift "$sft"
id=*) record_id="${1#*=}"; shift;; ;;
name=*) name="${1#*=}"; shift;; -d|--domain|--domain=*)
type=*) type="${1#*=}"; shift;; opts "$1" "$2"
content=*) content="${1#*=}"; shift;; domain="$val"
ttl=*) ttl="${1#*=}"; shift;; shift "$sft"
prio=*) prio="${1#*=}"; shift;; ;;
-*) echo "Unknown option: $1" >&2; exit 1;; -j|--json)
*) echo "Unknown key: $1" >&2; exit 1;; raw_json=1
shift
;;
-h|--help)
print_help_edit
exit 0
;;
id=*)
record_id="${1#*=}"
shift
;;
name=*)
name="${1#*=}"
shift
;;
type=*)
type="${1#*=}"
shift
;;
content=*)
content="${1#*=}"
shift
;;
ttl=*)
ttl="${1#*=}"
shift
;;
prio=*)
prio="${1#*=}"
shift
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
*)
echo "Unknown key: $1" >&2
exit 1
esac esac
done done
@ -308,13 +373,35 @@ piglet_edit() {
piglet_delete() { piglet_delete() {
while [ "$#" -ne 0 ]; do while [ "$#" -ne 0 ]; do
case "$1" in case "$1" in
-c|--config|--config=*) opts "$1" "$2"; piglet_conf="$val"; shift "$sft";; -c|--config|--config=*)
-d|--domain|--domain=*) opts "$1" "$2"; domain="$val"; shift "$sft";; opts "$1" "$2"
-j|--json) raw_json=1; shift;; piglet_conf="$val"
-h|--help) print_help_delete;; shift "$sft"
id=*) record_id="${1#*=}"; shift;; ;;
-*) echo "Unknown option: $1" >&2; exit 1;; -d|--domain|--domain=*)
*) echo "Unknown key: $1" >&2; exit 1;; opts "$1" "$2"
domain="$val"
shift "$sft"
;;
-j|--json)
raw_json=1
shift
;;
-h|--help)
print_help_delete
exit 0
;;
id=*)
record_id="${1#*=}"
shift
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
*)
echo "Unknown key: $1" >&2
exit 1
esac esac
done done
@ -326,13 +413,40 @@ piglet_delete() {
piglet_retrieve() { piglet_retrieve() {
while [ "$#" -ne 0 ]; do while [ "$#" -ne 0 ]; do
case "$1" in case "$1" in
-c|--config|--config=*) opts "$1" "$2"; piglet_conf="$val"; shift "$sft";; -c|--config|--config=*)
-d|--domain|--domain=*) opts "$1" "$2"; domain="$val"; shift "$sft";; opts "$1" "$2"
-j|--json) raw_json=1; shift;; piglet_conf="$val"
-h|--help) print_help_retrieve;; shift "$sft"
id=*) record_id="${1#*=}"; shift;; ;;
-*) echo "Unknown option: $1" >&2; exit 1;; -d|--domain|--domain=*)
*) echo "Unknown key: $1" >&2; exit 1;; opts "$1" "$2"
domain="$val"
shift "$sft"
;;
-l|--limit|--limit=*)
opts "$1" "$2"
limit="$val"
shift "$sft"
;;
-j|--json)
raw_json=1
shift
;;
-h|--help)
print_help_retrieve
exit 0
;;
id=*)
record_id="${1#*=}"
shift
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
*)
echo "Unknown key: $1" >&2
exit 1
esac esac
done done
@ -345,13 +459,18 @@ piglet_retrieve() {
echo "Install jq to enable table view." >&2 echo "Install jq to enable table view." >&2
fi fi
if [ "$raw_json" ]; then if [ -n "$raw_json" ]; then
echo "$json_out" echo "$json_out"
else else
# Print table # Print table
echo "$json_out" | jq -r '.records[] | .id,.name,.type,.content,.ttl,.prio' | limit="${limit:-36}"
awk '{print;} NR%6==0 {print "|";}' | tr '\n' ' ' | tr '|' '\n' | echo "$json_out" |
awk 'BEGIN {print "ID NAME TYPE CONTENT TTL PRIO"} {print $0}' | column -t jq -r '.records[] | .id,.name,.type,.content,.ttl,.prio' |
awk -v len="$limit" '{if (length >= len) {print substr($0, 0, len) "..."}
else {print $0}} NR%6==0 {print "|"}' |
tr '\n' '#' | tr '|' '\n' | sed 's/^#//g' |
awk 'BEGIN {print "ID#NAME#TYPE#CONTENT#TTL#PRIO"} {print $0}' |
column -t -s '#'
fi fi
} }
@ -392,28 +511,70 @@ fi
for args in "$@"; do for args in "$@"; do
shift shift
case "$args" in case "$args" in
--*) set -- "$@" "$args";; # save long options --*)
-*) args="$(echo "${args#-}" | grep -o . | xargs -I {} echo -n '-{} ')" set -- "$@" "$args"
;; # save long options
-*)
args="$(echo "${args#-}" | grep -o . | xargs -I {} echo -n '-{} ')"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
set -- "$@" $args;; set -- "$@" $args
*) set -- "$@" "$args";; # save positional arguments ;;
*)
set -- "$@" "$args" # save positional arguments
esac esac
done done
# Final arguments parser # Final arguments parser
while [ "$#" -ne 0 ]; do while [ "$#" -ne 0 ]; do
case "$1" in case "$1" in
-c|--config|--config=*) opts "$1" "$2"; piglet_conf="$val"; shift "$sft";; -c|--config|--config=*)
-d|--domain|--domain=*) opts "$1" "$2"; domain="$val"; shift "$sft";; opts "$1" "$2"
-j|--json) raw_json=1; shift;; piglet_conf="$val"
-h|--help) print_help;; shift "$sft"
-v|--version) echo $piglet_version; exit 0;; ;;
create) shift; piglet_create "$@"; shift "$#";; -d|--domain|--domain=*)
edit) shift; piglet_edit "$@"; shift "$#";; opts "$1" "$2"
delete) shift; piglet_delete "$@"; shift "$#";; domain="$val"
retrieve) shift; piglet_retrieve "$@"; shift "$#";; shift "$sft"
config) piglet_config; exit "$?";; ;;
-*) echo "Unknown option: $1" >&2; exit 1;; -j|--json)
*) echo "Unknown command: $1" >&2; exit 1;; raw_json=1
shift
;;
-h|--help)
print_help
exit 0
;;
-v|--version)
echo $piglet_version
exit 0
;;
create)
shift; piglet_create "$@"
shift "$#"
;;
edit)
shift; piglet_edit "$@"
shift "$#"
;;
delete)
shift; piglet_delete "$@"
shift "$#"
;;
retrieve)
shift; piglet_retrieve "$@"
shift "$#"
;;
config)
piglet_config
exit "$?"
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
*)
echo "Unknown command: $1" >&2
exit 1
esac esac
done done