From 43c79e16823ccd42697678ff40d5789038175cbd Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 18 Dec 2022 01:42:39 +0300 Subject: [PATCH] v0.2.0 --- Makefile | 9 ++++++++ README.md | 54 ++++++++++++++++++++++++++++++++++++---------- completion.bash | 5 +++++ piglet | 57 +++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 Makefile create mode 100644 completion.bash diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..69f2b04 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +PREFIX ?= /usr/local + +install: + install -Dm755 piglet $(PREFIX)/bin/piglet + install -Dm644 completion.bash $(PREFIX)/share/bash-completion/completions/piglet + +uninstall: + rm -v $(PREFIX)/bin/piglet + rm -v $(PREFIX)/share/bash-completion/completions/piglet diff --git a/README.md b/README.md index b38b03d..2d9a64f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # piglet -piglet is a [Porkbun DNS API](https://porkbun.com/api/json/v3/documentation) CLI client. +**piglet** is a [Porkbun DNS API](https://porkbun.com/api/json/v3/documentation) +CLI client. -Currently piglet can: +Currently **piglet** can: - Create a DNS record - Edit record @@ -11,13 +12,44 @@ Currently piglet can: # Installation -Just copy `piglet` to your PATH. For example: +1. Clone this Git-repository. +2. Install files. + +Installation with Makefile: ```sh -install -Dm755 piglet /usr/local/bin/piglet +# System-wide installation +sudo make install +# Local installation (into user's home dir) +PREFIX=~/.local make install ``` -Install [jq](https://stedolan.github.io/jq/) to enable pretty output. +Manual installation: + +```sh +# Just copy files into your PATH, for example: +sudo install -Dm755 piglet /usr/local/bin/piglet +sudo install -Dm644 completion.bash /usr/share/bash-completion/completions/piglet +``` + +Make sure your shell loads files from `/usr/local/share/bash-completion/completions` +or `~/.local/share/bash-completion/completions` for Bash completion. + +Add to your **~/.bashrc**: + +```sh +for completion in {/usr/local,~/.local}/share/bash-completion/completions/*; do + [ -f "$completion" ] && . "$completion" +done +``` + +Create file `~/.config/piglet/domains.list` with your domains list (each domain on +new line) for complete domains names. For example: + +``` +example.com +another-domain.tld +``` # Getting started @@ -27,30 +59,30 @@ For first step setup the configuration file: piglet config ``` -piglet creates `~/.config/piglet.conf` file with API credentials. +piglet creates `~/.config/piglet/piglet.conf` file with API credentials. Retrieve DNS records: ```sh -piglet -d example.org retrieve +piglet retrieve example.com ``` Create A-record on subdomain `mail`: ```sh -piglet -d example.org create name=mail type=a content=127.0.0.1 ttl=3600 +piglet create mail.example.com type=a content=127.0.0.1 ttl=3600 ``` Edit A-record for `example.org` (change to 127.0.0.1): ```sh -piglet -d example.org edit id=220755500 type=a content=127.0.0.1 +piglet edit example.org id=220755500 type=a content=127.0.0.1 ``` Delete DNS record by id: ```sh -piglet -d example.org delete id=220755592 +piglet delete example.org id=220755592 ``` -See `piglet --help` for more info. +See **piglet --help** for more info. diff --git a/completion.bash b/completion.bash new file mode 100644 index 0000000..618a386 --- /dev/null +++ b/completion.bash @@ -0,0 +1,5 @@ +# Bash completion for piglet +# https://git.nxhs.cloud/ge/piglet + +complete -W "create edit delete retrieve \ + $(cat ~/.config/piglet/domains.list | tr '\n' ' ')" piglet diff --git a/piglet b/piglet index 5d54e55..7838b49 100755 --- a/piglet +++ b/piglet @@ -27,11 +27,15 @@ # # For more information, please refer to -piglet_version='0.1.3' +piglet_version='0.2.0' piglet_conf="${HOME}/.config/piglet.conf" +piglet_conf_dir="${HOME}/.config/piglet" +piglet_conf_alt="$piglet_conf_dir/piglet.conf" + +[ -f "$piglet_conf_alt" ] && piglet_conf="$piglet_conf_alt" print_help() { -cat <<- 'EOF' + cat <<- 'EOF' Porkbun DNS API client. /\ ____ /\ @@ -40,7 +44,7 @@ Porkbun DNS API client. \ /&" |_|--|_| -Usage: piglet [-cdjhv] command [arg=value ...] +Usage: piglet [-cdjhv] command [arg=value ...] Options: -c, --config path to configuration file [default: ~/.config/piglet.conf] @@ -237,6 +241,15 @@ _init_piglet() { fi } +split_name() { + if [ "$(echo "$1" | tr . ' ' | wc -w)" -gt 2 ]; then + name="$(echo "$1" | awk -F. '{NF-=2} 1' | sed 's/ /\./g')" + domain="$(echo "$1" | awk -F. 'END {print $(NF-1), $NF}' | sed 's/ /\./g')" + else + domain="$1" + fi +} + piglet_config() { mkdir -p "$HOME"/.config @@ -247,12 +260,13 @@ piglet_config() { printf 'API_Key: '; read -r api_key printf 'Secret_API_Key: '; read -r secret_api_key - cat > "$piglet_conf" <<- EOF + mkdir -p "$piglet_conf_dir" + cat > "$piglet_conf_alt" <<- EOF # Porkbun DNS API credentials API_Key=$api_key Secret_API_Key=$secret_api_key EOF - printf 'Config saved as %s\n' "$piglet_conf" + printf 'Config saved as %s\n' "$piglet_conf_alt" } piglet_create() { @@ -299,9 +313,13 @@ piglet_create() { echo "Unknown option: $1" >&2 exit 1 ;; - *) + *=*) echo "Unknown key: $1" >&2 exit 1 + ;; + *) + split_name "$1" + shift esac done @@ -359,9 +377,13 @@ piglet_edit() { echo "Unknown option: $1" >&2 exit 1 ;; - *) + *=*) echo "Unknown key: $1" >&2 exit 1 + ;; + *) + split_name "$1" + shift esac done @@ -399,9 +421,13 @@ piglet_delete() { echo "Unknown option: $1" >&2 exit 1 ;; - *) + *=*) echo "Unknown key: $1" >&2 exit 1 + ;; + *) + split_name "$1" + shift esac done @@ -444,9 +470,13 @@ piglet_retrieve() { echo "Unknown option: $1" >&2 exit 1 ;; - *) + *=*) echo "Unknown key: $1" >&2 exit 1 + ;; + *) + split_name "$1" + shift esac done @@ -574,7 +604,12 @@ while [ "$#" -ne 0 ]; do exit 1 ;; *) - echo "Unknown command: $1" >&2 - exit 1 + if echo "$1" | grep -E '.+\..+' >/dev/null 2>&1; then + split_name "$1" + shift + else + echo "Unknown command: $1" >&2 + exit 1 + fi esac done