diff options
author | Tyler Davis <tyler@gluecode.net> | 2024-04-19 22:13:38 +0000 |
---|---|---|
committer | Tyler Davis <tyler@gluecode.net> | 2024-04-19 22:13:38 +0000 |
commit | c93221d83e9ea07c7e69ebf7f15ec42ea81fe289 (patch) | |
tree | beb73d88f9ccf1093ab61865a43cad22f2e03a1e | |
parent | 44afca8dabac92c1b9358da59a9759465566c147 (diff) | |
download | dotfiles-c93221d83e9ea07c7e69ebf7f15ec42ea81fe289.tar.gz dotfiles-c93221d83e9ea07c7e69ebf7f15ec42ea81fe289.zip |
bin: shfmt all scripts
-rwxr-xr-x | .local/bin/alacritty-setappearance.sh | 6 | ||||
-rwxr-xr-x | .local/bin/allupdate | 44 | ||||
-rwxr-xr-x | .local/bin/backup_sh | 113 | ||||
-rwxr-xr-x | .local/bin/buildgo_sh | 44 | ||||
-rwxr-xr-x | .local/bin/confgit.sh | 140 | ||||
-rwxr-xr-x | .local/bin/fetchgit | 50 | ||||
-rwxr-xr-x | .local/bin/gad | 543 | ||||
-rwxr-xr-x | .local/bin/gitrect-clean | 134 | ||||
-rwxr-xr-x | .local/bin/gitrect-fix | 210 | ||||
-rwxr-xr-x | .local/bin/gitrect-update | 214 | ||||
-rwxr-xr-x | .local/bin/gotools.sh | 50 | ||||
-rwxr-xr-x | .local/bin/ssg | 126 | ||||
-rwxr-xr-x | .local/bin/wat | 82 |
13 files changed, 875 insertions, 881 deletions
diff --git a/.local/bin/alacritty-setappearance.sh b/.local/bin/alacritty-setappearance.sh index 3ad5b0d..6dc8e93 100755 --- a/.local/bin/alacritty-setappearance.sh +++ b/.local/bin/alacritty-setappearance.sh @@ -1,7 +1,7 @@ #!/bin/bash CONFIG_PATH=~/.config/alacritty/alacritty.toml grep -q github_light $CONFIG_PATH && - sed -i -e "s/github_light/github_dark/" $CONFIG_PATH && - exit + sed -i -e "s/github_light/github_dark/" $CONFIG_PATH && + exit grep -q github_dark $CONFIG_PATH && - sed -i -e "s/github_dark/github_light/" $CONFIG_PATH + sed -i -e "s/github_dark/github_light/" $CONFIG_PATH diff --git a/.local/bin/allupdate b/.local/bin/allupdate index 83852bd..f439adc 100755 --- a/.local/bin/allupdate +++ b/.local/bin/allupdate @@ -4,40 +4,40 @@ $HOME/.local/bin/confgit.sh ## Rust ## if [ "$(command -v rustup)" ]; then - rustup update + rustup update fi if [ "$(command -v cargo)" ]; then - cargo --list | grep install-update &>/dev/null - if [ $? -eq 0 ]; then - cargo install-update -a - fi + cargo --list | grep install-update &>/dev/null + if [ $? -eq 0 ]; then + cargo install-update -a + fi fi if [ "$(command -v gcloud)" ]; then - gcloud components update -q + gcloud components update -q fi case $(uname) in - Darwin) - # Brew specifics, add installed formula capture later. - if [ "$(command -v brew)" ]; then - brew upgrade -q - fi - ;; - Linux) - # Store APK list to a file - apk list -I | cut -d ' ' -f 1|rev| cut -d '-' -f 3-|rev|sort -u > ~/.local/share/apklist +Darwin) + # Brew specifics, add installed formula capture later. + if [ "$(command -v brew)" ]; then + brew upgrade -q + fi + ;; +Linux) + # Store APK list to a file + apk list -I | cut -d ' ' -f 1 | rev | cut -d '-' -f 3- | rev | sort -u >~/.local/share/apklist - ;; + ;; esac if [ "$(command -v fetchgit)" ] && [ -d $HOME/.code ]; then - echo "Updating git repositories $(date -u +"%Y-%m-%d %H:%M:%SZ")" - if [ -d "$HOME/.golang/go/.git" ]; then - cd $HOME/.golang/go - fetchgit - fi - if [ "$(command -v gitrect-update)" ] && [ "$(command -v gitrect-fix)" ]; then gitrect-fix && gitrect-update -y; fi + echo "Updating git repositories $(date -u +"%Y-%m-%d %H:%M:%SZ")" + if [ -d "$HOME/.golang/go/.git" ]; then + cd $HOME/.golang/go + fetchgit + fi + if [ "$(command -v gitrect-update)" ] && [ "$(command -v gitrect-fix)" ]; then gitrect-fix && gitrect-update -y; fi fi echo "" # Newline diff --git a/.local/bin/backup_sh b/.local/bin/backup_sh index efccad4..eb44107 100755 --- a/.local/bin/backup_sh +++ b/.local/bin/backup_sh @@ -5,7 +5,7 @@ UPDATED=0 TARGET_DIR="/media/hdd/tdavis-archives/tech/backup" SOURCE_DIR="$HOME" -TODAY=`date +"%Y%m%d"` +TODAY=$(date +"%Y%m%d") TF="home-${TODAY}.tar" usage="$(basename "$0") [-h] [-t TARGET_DIR] [-s SOURCE_DIR] @@ -18,58 +18,58 @@ where: " while :; do - case $1 in - -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. - echo "$usage" - exit - ;; - -s) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - SOURCE_DIR=$2 - shift - fi - ;; - -s=?*) - SOURCE_DIR=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -s=) # Handle the case of an empty --file= - printf 'ERROR: "-s" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - -t) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - TARGET_DIR=$2 - shift - fi - ;; - -t=?*) - TARGET_DIR=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -t=) # Handle the case of an empty --t= - printf 'ERROR: "-t" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - --) # End of all options. - shift - break - ;; - -?*) - printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 - ;; - *) # Default case: If no more options then break out of the loop. - break ;; - esac - shift + case $1 in + -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. + echo "$usage" + exit + ;; + -s) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + SOURCE_DIR=$2 + shift + fi + ;; + -s=?*) + SOURCE_DIR=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -s=) # Handle the case of an empty --file= + printf 'ERROR: "-s" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + -t) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + TARGET_DIR=$2 + shift + fi + ;; + -t=?*) + TARGET_DIR=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -t=) # Handle the case of an empty --t= + printf 'ERROR: "-t" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + --) # End of all options. + shift + break + ;; + -?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + *) # Default case: If no more options then break out of the loop. + break ;; + esac + shift done # No posix-compliant way of getting processor count, so leverage OS-specifics if [ "$(uname -s)" = "Darwin" ]; then - FS_SEPARATOR="/" + FS_SEPARATOR="/" elif [ "$(uname -s)" = "Linux" ]; then - FS_SEPARATOR="/" + FS_SEPARATOR="/" else - # Windows uses backslash - FS_SEPARATOR="\\" + # Windows uses backslash + FS_SEPARATOR="\\" fi # Verify free space @@ -77,29 +77,28 @@ SOURCE_SIZE=$(du -sk ${SOURCE_DIR} | cut -f 1) DOUBLE_SOURCE_SIZE=$(echo "${SOURCE_SIZE}*2" | bc -q) TARGET_FREE=$(df -Pk ${TARGET_DIR} | grep -vE "^Filesystem" | awk '{ print $4 }') if [ $DOUBLE_SOURCE_SIZE -gt $TARGET_FREE ]; then - echo "insufficient space to perform backup. 2x Source Directory usage is ${DOUBLE_SOURCE_SIZE}, Target Directory free space is: ${TARGET_FREE}" - exit 1 + echo "insufficient space to perform backup. 2x Source Directory usage is ${DOUBLE_SOURCE_SIZE}, Target Directory free space is: ${TARGET_FREE}" + exit 1 fi COMPRESSION="gzip" # Gzip default SUFFIX=".gz" if [ $(command -v zstd) ]; then - COMPRESSION="zstd -T0 -qq" - SUFFIX=".zstd" + COMPRESSION="zstd -T0 -qq" + SUFFIX=".zstd" elif [ $(command -v pigz) ]; then - COMPRESSION="pigz -p" + COMPRESSION="pigz -p" fi FULL_TF="${TARGET_DIR}${FS_SEPARATOR}${TF}" # Updating existing archives almost always fails. Build and zip in one shot. if [ -e ${FULL_TF}${SUFFIX} ]; then - rm -f ${FULL_TF}${SUFFIX}; + rm -f ${FULL_TF}${SUFFIX} fi # Do the backup -if tar -C ${SOURCE_DIR} -c -f ${FULL_TF} ${SOURCE_DIR} >/dev/null 2>&1 ; then - ${COMPRESSION} ${FULL_TF} - rm -f ${FULL_TF} # Delete the original tar file just in case +if tar -C ${SOURCE_DIR} -c -f ${FULL_TF} ${SOURCE_DIR} >/dev/null 2>&1; then + ${COMPRESSION} ${FULL_TF} + rm -f ${FULL_TF} # Delete the original tar file just in case fi - diff --git a/.local/bin/buildgo_sh b/.local/bin/buildgo_sh index ea54801..e1c6432 100755 --- a/.local/bin/buildgo_sh +++ b/.local/bin/buildgo_sh @@ -1,26 +1,26 @@ #!/usr/bin/env sh if [ -d "$HOME/.golang/go" ] && [ $(command -v go) ]; then - CURR=$PWD - cd $HOME/.golang/go/src; - if [ "$(go version | grep devel)" ]; then - # Check that the go version is dev-build - # then verify that the displayed hash doesn't match current revision - if [ ! "$(command -v git)" ]; then - echo "git not found" - exit 1 - fi - fetchgit - rev=$(git rev-parse --short HEAD) - currentRevision=$(go version|cut -d ' ' -f 4|cut -d- -f 2) - if [ $rev != $currentRevision ]; then - echo "Upgrading ${currentRevision} -> ${rev}" - go clean -cache; - go clean -modcache; - ./all.bash # Make always finishes, tests may just fail - cd $HOME - gotools.sh - fi - fi - cd $CURR + CURR=$PWD + cd $HOME/.golang/go/src + if [ "$(go version | grep devel)" ]; then + # Check that the go version is dev-build + # then verify that the displayed hash doesn't match current revision + if [ ! "$(command -v git)" ]; then + echo "git not found" + exit 1 + fi + fetchgit + rev=$(git rev-parse --short HEAD) + currentRevision=$(go version | cut -d ' ' -f 4 | cut -d- -f 2) + if [ $rev != $currentRevision ]; then + echo "Upgrading ${currentRevision} -> ${rev}" + go clean -cache + go clean -modcache + ./all.bash # Make always finishes, tests may just fail + cd $HOME + gotools.sh + fi + fi + cd $CURR fi diff --git a/.local/bin/confgit.sh b/.local/bin/confgit.sh index 64c7927..8fbf0ed 100755 --- a/.local/bin/confgit.sh +++ b/.local/bin/confgit.sh @@ -4,26 +4,26 @@ OS="$(uname -s)" NPROCS="" if [ "$OS" = "Linux" ]; then - DETPROCS="$(nproc --all)" + DETPROCS="$(nproc --all)" elif [ "$OS" = "Darwin" ] || [ "$(echo "$OS" | grep -q BSD)" = "BSD" ]; then - DETPROCS="$(sysctl -n hw.ncpu)" + DETPROCS="$(sysctl -n hw.ncpu)" else - DETPROCS="$(getconf _NPROCESSORS_ONLN)" # glibc/coreutils fallback + DETPROCS="$(getconf _NPROCESSORS_ONLN)" # glibc/coreutils fallback fi if [ $DETPROCS -gt 16 ]; then - NPROCS="$(($DETPROCS / 2))" + NPROCS="$(($DETPROCS / 2))" else - NPROCS="$DETPROCS" + NPROCS="$DETPROCS" fi if [ ! $(command -v git) ]; then - echo "git not installed"; - exit 1 + echo "git not installed" + exit 1 fi if [ -e $HOME/.gitconfig ]; then - rm $HOME/.gitconfig # Purge existing gitconf + rm $HOME/.gitconfig # Purge existing gitconf fi # Dotfiles config @@ -37,9 +37,9 @@ git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME config status.showUntrackedFile # Main Git Config git config --global am.messageid true git config --global branch.sort -committerdate -git config --global checkout.defaultRemote origin # Default to using the "origin" remote +git config --global checkout.defaultRemote origin # Default to using the "origin" remote git config --global checkout.thresholdForParallelism 50 # Default 100 files -git config --global checkout.workers "$NPROCS" # Auto-detect threads from CPU +git config --global checkout.workers "$NPROCS" # Auto-detect threads from CPU git config --global commit.verbose true git config --global core.commitgraph true git config --global core.excludesfile "$HOME/.local/share/git/ignore_global" @@ -92,83 +92,83 @@ git config --global mergetool.vscode.cmd "code --wait \$MERGED" # Editors if [ "$(command -v code)" ] && [ "$(env | grep VSCODE)" ]; then - export EDITOR="code" - export VISUAL="code" - git config --global core.editor "code --wait" - git config --global diff.tool vscode - git config --global merge.tool vscode + export EDITOR="code" + export VISUAL="code" + git config --global core.editor "code --wait" + git config --global diff.tool vscode + git config --global merge.tool vscode elif [ -d "$HOME/.config/doom" ] && [ "$(command -v emacs)" ]; then - export EDITOR="emacs" - export VISUAL="emacs" - export PATH=$PATH:$HOME/.config/emacs/bin + export EDITOR="emacs" + export VISUAL="emacs" + export PATH=$PATH:$HOME/.config/emacs/bin elif [ "$(command -v hx)" ]; then - export EDITOR="hx" - export VISUAL="hx" - git config --global core.editor hx - # Still can't find whether helix supports merge contents - git config --global diff.tool vim - git config --global merge.tool vim + export EDITOR="hx" + export VISUAL="hx" + git config --global core.editor hx + # Still can't find whether helix supports merge contents + git config --global diff.tool vim + git config --global merge.tool vim else - export EDITOR="vim" - export VISUAL="vim" - git config --global core.editor vim - git config --global diff.tool vim - git config --global merge.tool vim + export EDITOR="vim" + export VISUAL="vim" + git config --global core.editor vim + git config --global diff.tool vim + git config --global merge.tool vim fi # Use pager for large diffs git config --global pager.difftool true if [ "$(command -v difft)" ]; then - git config --global alias.dft difftool - git config --global diff.external difft + git config --global alias.dft difftool + git config --global diff.external difft fi # Git configs applied to folder groups if [ "$(command -v fd)" ]; then - if [ -d $HOME/.code/hpe ]; then - fd -H -t d -E mod -E vendor '.git$' $HOME/.code/hpe -x bash -c "cd {//}; git config user.email tdavis@hpe.com; git config http.cookiefile $HOME/.local/share/git/cookies-work;" - fi - if [ -d $HOME/.code/gluecode ]; then - fd -H -t d -E mod -E vendor '.git$' $HOME/.code/gluecode -x bash -c "cd {//}; git config user.email tyler@gluecode.com;" - fi - if [ -d $HOME/.code/phoenix ]; then - fd -H -t d -E mod -E vendor '.git$' $HOME/.code/phoenix -x bash -c "cd {//}; git config user.email tyler@gluecode.com;" - fi + if [ -d $HOME/.code/hpe ]; then + fd -H -t d -E mod -E vendor '.git$' $HOME/.code/hpe -x bash -c "cd {//}; git config user.email tdavis@hpe.com; git config http.cookiefile $HOME/.local/share/git/cookies-work;" + fi + if [ -d $HOME/.code/gluecode ]; then + fd -H -t d -E mod -E vendor '.git$' $HOME/.code/gluecode -x bash -c "cd {//}; git config user.email tyler@gluecode.com;" + fi + if [ -d $HOME/.code/phoenix ]; then + fd -H -t d -E mod -E vendor '.git$' $HOME/.code/phoenix -x bash -c "cd {//}; git config user.email tyler@gluecode.com;" + fi else - if [ -d $HOME/.code/hpe ]; then - find $HOME/.code/hpe -name ".git" -exec bash -c "cd {}; git config user.email tdavis@hpe.com; git config http.cookiefile $HOME/.local/share/git/cookies-work;" \; - fi - if [ -d $HOME/.code/gluecode ]; then - find $HOME/.code/gluecode -name ".git" -exec bash -c "cd {}; git config user.email tyler@gluecode.net;" \; - fi - if [ -d $HOME/.code/phoenix ]; then - find $HOME/.code/phoenix -name ".git" -exec bash -c "cd {}; git config user.email tyler@gluecode.net;" \; - fi + if [ -d $HOME/.code/hpe ]; then + find $HOME/.code/hpe -name ".git" -exec bash -c "cd {}; git config user.email tdavis@hpe.com; git config http.cookiefile $HOME/.local/share/git/cookies-work;" \; + fi + if [ -d $HOME/.code/gluecode ]; then + find $HOME/.code/gluecode -name ".git" -exec bash -c "cd {}; git config user.email tyler@gluecode.net;" \; + fi + if [ -d $HOME/.code/phoenix ]; then + find $HOME/.code/phoenix -name ".git" -exec bash -c "cd {}; git config user.email tyler@gluecode.net;" \; + fi fi # Git LFS support if [ $(command -v git-lfs) ]; then - # Only install if we don't detect existing LFS entries, which should - # always be true on a fresh run - if [ $(git config -l|grep -c lfs) -lt 1 ]; then - # Redirect output to null - silence "Git LFS installed" messages - #git lfs install > /dev/null - git config --global --includes --replace-all filter.lfs.clean "git-lfs clean -- %f" - fi + # Only install if we don't detect existing LFS entries, which should + # always be true on a fresh run + if [ $(git config -l | grep -c lfs) -lt 1 ]; then + # Redirect output to null - silence "Git LFS installed" messages + #git lfs install > /dev/null + git config --global --includes --replace-all filter.lfs.clean "git-lfs clean -- %f" + fi fi -case `uname` in - Darwin) - git config --global credential.helper osxkeychain - ;; - Linux) - git config --global credential.helper "store --file=/home/tydavis/.config/gitcreds" - # Email configuration for `git send-email` command - default Gluecode - git config --global sendemail.annotate yes # Review and edit each patch - git config --global sendemail.confirm auto - git config --global sendemail.smtpserver /usr/bin/msmtp - ;; - FreeBSD) - # commands for FreeBSD go here - ;; +case $(uname) in +Darwin) + git config --global credential.helper osxkeychain + ;; +Linux) + git config --global credential.helper "store --file=/home/tydavis/.config/gitcreds" + # Email configuration for `git send-email` command - default Gluecode + git config --global sendemail.annotate yes # Review and edit each patch + git config --global sendemail.confirm auto + git config --global sendemail.smtpserver /usr/bin/msmtp + ;; +FreeBSD) + # commands for FreeBSD go here + ;; esac diff --git a/.local/bin/fetchgit b/.local/bin/fetchgit index 7e227f7..bca5a39 100755 --- a/.local/bin/fetchgit +++ b/.local/bin/fetchgit @@ -1,31 +1,31 @@ #!/usr/bin/env sh if [ "$#" -gt 0 ] && [ -d "$1" ]; then - cd ${1} || exit + cd ${1} || exit fi if [ ! -d .git ]; then # If $PWD/.git doesn't exist - # then are we in a ".git" dir? jump up one dir - if [ ${PWD##*/} = ".git" ]; then - cd .. - else #Finally, are we at least in a work tree? - if [ ! $(git rev-parse --is-inside-work-tree) ]; then - echo "Not in a git directory" - exit - fi - fi + # then are we in a ".git" dir? jump up one dir + if [ ${PWD##*/} = ".git" ]; then + cd .. + else #Finally, are we at least in a work tree? + if [ ! $(git rev-parse --is-inside-work-tree) ]; then + echo "Not in a git directory" + exit + fi + fi fi rems=$(eval "git remote") # list all remotes len_rems=${#rems} if [ $len_rems -lt 1 ]; then - # If the list of remotes is zero, we don't do any remote ops - exit 0; + # If the list of remotes is zero, we don't do any remote ops + exit 0 fi -CURRENT_BRANCH=`git symbolic-ref --short HEAD` +CURRENT_BRANCH=$(git symbolic-ref --short HEAD) # There should always be an origin remote if there is at least one remote -DEFAULT_BRANCH=`git remote show origin | sed -n '/HEAD branch/s/.*: //p'` +DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') # -- I think we don't need this due to `--autostash` in pull commands # @@ -43,18 +43,18 @@ FETCH_FAILED=$? git switch --quiet $DEFAULT_BRANCH if [ $FETCH_FAILED -ne 0 ]; then - # We should almost always be able to connect to origin - git pull -q --ff-only --autostash origin $DEFAULT_BRANCH + # We should almost always be able to connect to origin + git pull -q --ff-only --autostash origin $DEFAULT_BRANCH else - if git config remote.upstream.url >/dev/null; then - git pull -q --ff-only --autostash upstream $DEFAULT_BRANCH - git push -q origin $DEFAULT_BRANCH - elif git config remote.upstream.tydavis >/dev/null; then - git pull -q --ff-only --autostash tydavis $DEFAULT_BRANCH - git push -q origin $DEFAULT_BRANCH - else - git pull -q --ff-only --autostash origin $DEFAULT_BRANCH - fi + if git config remote.upstream.url >/dev/null; then + git pull -q --ff-only --autostash upstream $DEFAULT_BRANCH + git push -q origin $DEFAULT_BRANCH + elif git config remote.upstream.tydavis >/dev/null; then + git pull -q --ff-only --autostash tydavis $DEFAULT_BRANCH + git push -q origin $DEFAULT_BRANCH + else + git pull -q --ff-only --autostash origin $DEFAULT_BRANCH + fi fi git switch --quiet $CURRENT_BRANCH diff --git a/.local/bin/gad b/.local/bin/gad index 75e1341..d104c4d 100755 --- a/.local/bin/gad +++ b/.local/bin/gad @@ -1,7 +1,7 @@ #!/bin/sh usage() { - printf "\\nUsage: %s [-5] [-6] [-l TTL] [-f] [-t] [-e] [-v] [-s] [-i EXT_IF] [-a APIKEY] -d EXAMPLE.COM -r \"RECORD-NAMES\" + printf "\\nUsage: %s [-5] [-6] [-l TTL] [-f] [-t] [-e] [-v] [-s] [-i EXT_IF] [-a APIKEY] -d EXAMPLE.COM -r \"RECORD-NAMES\" -5: Use Gandi's Legacy LiveDNS platform -6: Update AAAA record(s) instead of A record(s) @@ -18,7 +18,7 @@ EXT_IF: The name of your external network interface APIKEY: Your API key provided by Gandi (loaded from the file ~/.gandiapi if not specified) EXAMPLE.COM: The domain name whose active zonefile will be updated RECORD-NAMES: A space-separated list of the name(s) of the A or AAAA record(s) to update or create\\n\\n" "$0" - exit 1 + exit 1 } # @@ -26,48 +26,66 @@ RECORD-NAMES: A space-separated list of the name(s) of the A or AAAA record(s) t # while [ $# -gt 0 ]; do - case "$1" in - -5) v5="yes";; - -6) ipv6="yes";; - -l) ttl="$2"; shift;; - -f) force="yes";; - -t) testing="yes";; - -e) debug="yes";; - -v) verbose="yes";; - -s) stdin_ip="yes";; - -i) ext_if="$2"; shift;; - -a) apikey="$2"; shift;; - -d) domain="$2"; shift;; - -r) records="$2"; shift;; - *) usage; break - esac - shift + case "$1" in + -5) v5="yes" ;; + -6) ipv6="yes" ;; + -l) + ttl="$2" + shift + ;; + -f) force="yes" ;; + -t) testing="yes" ;; + -e) debug="yes" ;; + -v) verbose="yes" ;; + -s) stdin_ip="yes" ;; + -i) + ext_if="$2" + shift + ;; + -a) + apikey="$2" + shift + ;; + -d) + domain="$2" + shift + ;; + -r) + records="$2" + shift + ;; + *) + usage + break + ;; + esac + shift done if [ -z "$domain" -o -z "$records" ]; then - usage + usage fi if [ -z "$apikey" ]; then - if [ -f "${HOME}/.gandiapi" ]; then - apikey=$(cat "${HOME}/.gandiapi") - else - usage - fi + if [ -f "${HOME}/.gandiapi" ]; then + apikey=$(cat "${HOME}/.gandiapi") + else + usage + fi fi if [ ! -z "$ttl" -a "$v5" != "yes" ]; then - printf "Setting a custom TTL on records is not supported on Gandi's legacy DNS platform.\\n" - exit 1 + printf "Setting a custom TTL on records is not supported on Gandi's legacy DNS platform.\\n" + exit 1 fi if [ "$ipv6" = "yes" ]; then - record_type="AAAA" - ip_regex="\([0-9A-Fa-f:]*\)" - inet="inet6" + record_type="AAAA" + ip_regex="\([0-9A-Fa-f:]*\)" + inet="inet6" else - record_type="A" - ip_regex="\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)" - inet="inet" + record_type="A" + ip_regex="\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)" + inet="inet" fi if [ "$debug" = "yes" ]; then - printf "Initial variables:\\n---\\napikey = %s\\ndomain = %s\\nrecords = %s\\nttl (only relevant with LiveDNS) = %s\\nrecord_type = %s\\nip_regex = %s\\n---\\n\\n" "$apikey" "$domain" "$records" "$ttl" "$record_type" "$ip_regex" + printf "Initial variables:\\n---\\napikey = %s\\ndomain = %s\\nrecords = %s\\nttl (only relevant with LiveDNS) = %s\\nrecord_type = %s\\nip_regex = %s\\n---\\n\\n" "$apikey" "$domain" "$records" "$ttl" "$record_type" "$ip_regex" fi # @@ -75,9 +93,9 @@ fi # if [ "$v5" = "yes" ]; then # Swapping defaults - gandi="rpc.gandi.net:443" + gandi="rpc.gandi.net:443" else - gandi="dns.api.gandi.net:443" + gandi="dns.api.gandi.net:443" fi gad_version="1.4.2" @@ -91,14 +109,14 @@ gad_version="1.4.2" # rest() { - if [ "$debug" = "yes" ]; then - printf "REST call to endpoint:\\n---\\n%s\\n---\\n\\n" "$2" 1>&2 - fi - # Throw away third argument to function if verb is GET - if [ "$1" != "GET" ]; then - tmp_json="$3" - fi - tmp_request="${1} /api/v5/${2} HTTP/1.1 + if [ "$debug" = "yes" ]; then + printf "REST call to endpoint:\\n---\\n%s\\n---\\n\\n" "$2" 1>&2 + fi + # Throw away third argument to function if verb is GET + if [ "$1" != "GET" ]; then + tmp_json="$3" + fi + tmp_request="${1} /api/v5/${2} HTTP/1.1 User-Agent: Gandi Automatic DNS shell script/${gad_version} Host: $(printf "%s" "$gandi" | cut -d ':' -f 1 -) Content-Type: application/json @@ -106,18 +124,18 @@ Content-Length: $(printf "%s" "$tmp_json" | wc -c | tr -d "[:space:]") X-Api-Key: ${apikey} " - if [ "$1" != "GET" ]; then - tmp_message="${tmp_request}${tmp_json}" - else - tmp_message="$tmp_request" - fi - if [ "$debug" = "yes" ]; then - printf "Sending REST message tmp_message:\\n---\\n%s\\n---\\n\\n" "$tmp_message" 1>&2 - fi - printf "%s" "$tmp_message" | openssl s_client -quiet -connect "$gandi" 2> /dev/null | tail -1 - unset tmp_json - unset tmp_request - unset tmp_message + if [ "$1" != "GET" ]; then + tmp_message="${tmp_request}${tmp_json}" + else + tmp_message="$tmp_request" + fi + if [ "$debug" = "yes" ]; then + printf "Sending REST message tmp_message:\\n---\\n%s\\n---\\n\\n" "$tmp_message" 1>&2 + fi + printf "%s" "$tmp_message" | openssl s_client -quiet -connect "$gandi" 2>/dev/null | tail -1 + unset tmp_json + unset tmp_request + unset tmp_message } # @@ -132,43 +150,43 @@ X-Api-Key: ${apikey} # get_json_field() { - # Set $updated_json to the provided JSON content - updated_json="$2" - # This helps handle JSON lists by editing list values to be separated by - # spaces instead of commas, so we can split JSON fields on commas with awk - # later, and loop over them to find the value we want - while true; do - # Find the first instance of [ and replace content up to the first comma - # with the content followed by a space - updated_json=$(printf "%s" "$updated_json" | sed 's/\(\[[^,]*"\),/\1 /g') - # Check for the pattern again, and restart the loop if grep returns 0 - # indicating the pattern was found. Otherwise break out of this loop. - if printf "%s" "$updated_json" | grep -e '\[[^,]*",' > /dev/null; then - continue - else - break - fi - done - # Set internal field separator to a new line, so our for loop loops over the - # records returned on separate lines by the awk command below - IFS=' + # Set $updated_json to the provided JSON content + updated_json="$2" + # This helps handle JSON lists by editing list values to be separated by + # spaces instead of commas, so we can split JSON fields on commas with awk + # later, and loop over them to find the value we want + while true; do + # Find the first instance of [ and replace content up to the first comma + # with the content followed by a space + updated_json=$(printf "%s" "$updated_json" | sed 's/\(\[[^,]*"\),/\1 /g') + # Check for the pattern again, and restart the loop if grep returns 0 + # indicating the pattern was found. Otherwise break out of this loop. + if printf "%s" "$updated_json" | grep -e '\[[^,]*",' >/dev/null; then + continue + else + break + fi + done + # Set internal field separator to a new line, so our for loop loops over the + # records returned on separate lines by the awk command below + IFS=' ' - # Trim out curly braces, anything like a newline, split records on ", " with - # awk, replace any leading whitespace with sed, and loop over records. - for i in $(printf "%s" "$updated_json" | tr -d '{}\t\n\r\f' | awk 'BEGIN{RS=", "}{print $0}' | sed 's/^ //'); do - # Find the current field name and value by using ": " as the awk field - # separator, and treating field 1 as the name and field 2 as the value. - # Also trim out any double quotes and brackets. - field_name=$(printf "%s" "$i" | awk -F ': ' '{print $1}' | tr -d '"' | tr -d '[]') - field_value=$(printf "%s" "$i" | awk -F ': ' '{print $2}' | tr -d '"' | tr -d '[]') - # If the current field name matches the one we're looking for, print the - # value and break out of this loop - if [ "$field_name" = "$1" ]; then - printf "%s" "$field_value" - break - fi - done - unset IFS + # Trim out curly braces, anything like a newline, split records on ", " with + # awk, replace any leading whitespace with sed, and loop over records. + for i in $(printf "%s" "$updated_json" | tr -d '{}\t\n\r\f' | awk 'BEGIN{RS=", "}{print $0}' | sed 's/^ //'); do + # Find the current field name and value by using ": " as the awk field + # separator, and treating field 1 as the name and field 2 as the value. + # Also trim out any double quotes and brackets. + field_name=$(printf "%s" "$i" | awk -F ': ' '{print $1}' | tr -d '"' | tr -d '[]') + field_value=$(printf "%s" "$i" | awk -F ': ' '{print $2}' | tr -d '"' | tr -d '[]') + # If the current field name matches the one we're looking for, print the + # value and break out of this loop + if [ "$field_name" = "$1" ]; then + printf "%s" "$field_value" + break + fi + done + unset IFS } # @@ -183,66 +201,69 @@ get_json_field() { # rpc() { - if [ "$debug" = "yes" ]; then - printf "RPC call to methodName:\\n---\\n%s\\n---\\n\\n" "$1" 1>&2 - fi - tmp_xml="<?xml version=\"1.0\"?> + if [ "$debug" = "yes" ]; then + printf "RPC call to methodName:\\n---\\n%s\\n---\\n\\n" "$1" 1>&2 + fi + tmp_xml="<?xml version=\"1.0\"?> <methodCall> <methodName>${1}</methodName> <params> <param> <value><string>${apikey}</string></value> </param>" - shift - while [ ! -z "$1" ]; do - if [ "$1" != "struct" ]; then - tmp_xml="${tmp_xml} + shift + while [ ! -z "$1" ]; do + if [ "$1" != "struct" ]; then + tmp_xml="${tmp_xml} <param> <value><${1}>${2}</${1}></value> </param>" - shift; shift - else - tmp_xml="${tmp_xml} + shift + shift + else + tmp_xml="${tmp_xml} <param> <value> <struct>" - shift; - while [ ! -z "$1" ]; do - if [ "$1" != "struct" ]; then - tmp_xml="${tmp_xml} + shift + while [ ! -z "$1" ]; do + if [ "$1" != "struct" ]; then + tmp_xml="${tmp_xml} <member> <name>${1}</name> <value><${2}>${3}</${2}></value> </member>" - shift; shift; shift; - else - break - fi - done - tmp_xml="${tmp_xml} + shift + shift + shift + else + break + fi + done + tmp_xml="${tmp_xml} </struct> </value> </param>" - fi - done - tmp_xml="${tmp_xml} + fi + done + tmp_xml="${tmp_xml} </params> </methodCall>" - tmp_post="POST /xmlrpc/ HTTP/1.1 + tmp_post="POST /xmlrpc/ HTTP/1.1 User-Agent: Gandi Automatic DNS shell script/${gad_version} Host: $(printf "%s" "$gandi" | cut -d ':' -f 1 -) Content-Type: text/xml Content-Length: $(printf "%s" "$tmp_xml" | wc -c | tr -d "[:space:]") " - tmp_message="${tmp_post}${tmp_xml}" - if [ "$debug" = "yes" ]; then - printf "Sending XML-RPC message tmp_message:\\n---\\n%s\\n---\\n\\n" "$tmp_message" 1>&2 - fi - printf "%s" "$tmp_message" | openssl s_client -quiet -connect "$gandi" 2> /dev/null - unset tmp_xml - unset tmp_post - unset tmp_message + tmp_message="${tmp_post}${tmp_xml}" + if [ "$debug" = "yes" ]; then + printf "Sending XML-RPC message tmp_message:\\n---\\n%s\\n---\\n\\n" "$tmp_message" 1>&2 + fi + printf "%s" "$tmp_message" | openssl s_client -quiet -connect "$gandi" 2>/dev/null + unset tmp_xml + unset tmp_post + unset tmp_message } # @@ -252,23 +273,23 @@ Content-Length: $(printf "%s" "$tmp_xml" | wc -c | tr -d "[:space:]") # update() { - while [ ! -z "$1" ]; do - if [ "$v5" = "yes" ]; then - new_record_id=$(rpc "domain.zone.record.list" "int" "$zone_id" "int" "$new_version_id" "struct" "name" "string" "$1" "type" "string" "$record_type" | grep -A 1 ">id<" | sed -n 's/.*<string>\([0-9]*\).*/\1/p') - if [ "$debug" = "yes" ]; then - printf "new_record_id:\\n---\\n%s\\n---\\n\\n" "$new_record_id" - fi - rpc "domain.zone.record.update" "int" "$zone_id" "int" "$new_version_id" "struct" "id" "int" "$new_record_id" "struct" "name" "string" "$1" "type" "string" "$record_type" "value" "string" "$ext_ip" - else - new_record_json=$(rest "PUT" "zones/${zone_id}/records/${1}/${record_type}" "{\"rrset_ttl\": \"${new_ttl}\", \"rrset_values\": [\"${ext_ip}\"]}") - new_record_message=$(get_json_field "message" "$new_record_json") - if [ "$debug" = "yes" ]; then - printf "new_record_json:\\n---\\n%s\\n---\\n\\n" "$new_record_json" - printf "new_record_message:\\n---\\n%s\\n---\\n\\n" "$new_record_message" - fi - fi - shift - done + while [ ! -z "$1" ]; do + if [ "$v5" = "yes" ]; then + new_record_id=$(rpc "domain.zone.record.list" "int" "$zone_id" "int" "$new_version_id" "struct" "name" "string" "$1" "type" "string" "$record_type" | grep -A 1 ">id<" | sed -n 's/.*<string>\([0-9]*\).*/\1/p') + if [ "$debug" = "yes" ]; then + printf "new_record_id:\\n---\\n%s\\n---\\n\\n" "$new_record_id" + fi + rpc "domain.zone.record.update" "int" "$zone_id" "int" "$new_version_id" "struct" "id" "int" "$new_record_id" "struct" "name" "string" "$1" "type" "string" "$record_type" "value" "string" "$ext_ip" + else + new_record_json=$(rest "PUT" "zones/${zone_id}/records/${1}/${record_type}" "{\"rrset_ttl\": \"${new_ttl}\", \"rrset_values\": [\"${ext_ip}\"]}") + new_record_message=$(get_json_field "message" "$new_record_json") + if [ "$debug" = "yes" ]; then + printf "new_record_json:\\n---\\n%s\\n---\\n\\n" "$new_record_json" + printf "new_record_message:\\n---\\n%s\\n---\\n\\n" "$new_record_message" + fi + fi + shift + done } # @@ -278,19 +299,19 @@ update() { # create() { - while [ ! -z "$1" ]; do - if [ "$v5" = "yes" ]; then - rpc "domain.zone.record.add" "int" "$zone_id" "int" "$new_version_id" "struct" "name" "string" "$1" "type" "string" "$record_type" "value" "string" "$ext_ip" - else - new_record_json=$(rest "POST" "zones/${zone_id}/records/${1}/${record_type}" "{\"rrset_ttl\": \"${new_ttl}\", \"rrset_values\": [\"${ext_ip}\"]}") - new_record_message=$(get_json_field "message" "$new_record_json") - if [ "$debug" = "yes" ]; then - printf "new_record_json:\\n---\\n%s\\n---\\n\\n" "$new_record_json" - printf "new_record_message:\\n---\\n%s\\n---\\n\\n" "$new_record_message" - fi - fi - shift - done + while [ ! -z "$1" ]; do + if [ "$v5" = "yes" ]; then + rpc "domain.zone.record.add" "int" "$zone_id" "int" "$new_version_id" "struct" "name" "string" "$1" "type" "string" "$record_type" "value" "string" "$ext_ip" + else + new_record_json=$(rest "POST" "zones/${zone_id}/records/${1}/${record_type}" "{\"rrset_ttl\": \"${new_ttl}\", \"rrset_values\": [\"${ext_ip}\"]}") + new_record_message=$(get_json_field "message" "$new_record_json") + if [ "$debug" = "yes" ]; then + printf "new_record_json:\\n---\\n%s\\n---\\n\\n" "$new_record_json" + printf "new_record_message:\\n---\\n%s\\n---\\n\\n" "$new_record_message" + fi + fi + shift + done } # @@ -301,53 +322,53 @@ create() { # check() { - while [ ! -z "$1" ]; do - if [ "$v5" = "yes" ]; then - record_value=$(rpc "domain.zone.record.list" "int" "$zone_id" "int" "0" "struct" "name" "string" "$1" "type" "string" "$record_type" | grep -A 1 ">value<" | sed -n "s/.*<string>${ip_regex}.*/\1/p") - record_count=$(printf "%s" "$record_value" | wc -w) - else - record_json=$(rest "GET" "zones/${zone_id}/records/${1}/${record_type}") - if [ "$debug" = "yes" ]; then - printf "record_json:\\n---\\n%s\\n---\\n\\n" "$record_json" - fi - record_value=$(get_json_field "rrset_values" "$record_json") - if [ "$debug" = "yes" ]; then - printf "record_value:\\n---\\n%s\\n---\\n\\n" "$record_value" - fi - record_ttl=$(get_json_field "rrset_ttl" "$record_json") - record_count=$(printf "%s" "$record_value" | wc -w) - # If a custom TTL wasn't provided, just set it to the existing one. - # If the record TTL is empty (because the record doesn't exist) and - # no custom TTL was provided, set a default. - if [ -z "$record_ttl" -a -z "$ttl" ]; then - new_ttl="10800" - elif [ -z "$ttl" ]; then - new_ttl="$record_ttl" - else - new_ttl="$ttl" - fi - fi - if [ "$record_count" -gt "1" ]; then - printf "Sorry, but gad does not support updating multiple records with the same name.\\n" - exit 1 - elif [ -z "$record_value" ]; then - if [ -z "$records_to_create" ]; then - records_to_create="$1" - else - records_to_create="${records_to_create} ${1}" - fi - elif [ "$ext_ip" != "$record_value" -o "$new_ttl" != "$record_ttl" -o "$force" = "yes" ]; then - if [ -z "$records_to_update" ]; then - records_to_update="$1" - else - records_to_update="${records_to_update} ${1}" - fi - fi - if [ "$debug" = "yes" ]; then - printf "Results after checking record:\\n---\\nrecord: %s\\nrecord_value: %s\\nrecords_to_create: %s\\nrecords_to_update: %s\\n---\\n\\n" "$1" "$record_value" "$records_to_create" "$records_to_update" - fi - shift - done + while [ ! -z "$1" ]; do + if [ "$v5" = "yes" ]; then + record_value=$(rpc "domain.zone.record.list" "int" "$zone_id" "int" "0" "struct" "name" "string" "$1" "type" "string" "$record_type" | grep -A 1 ">value<" | sed -n "s/.*<string>${ip_regex}.*/\1/p") + record_count=$(printf "%s" "$record_value" | wc -w) + else + record_json=$(rest "GET" "zones/${zone_id}/records/${1}/${record_type}") + if [ "$debug" = "yes" ]; then + printf "record_json:\\n---\\n%s\\n---\\n\\n" "$record_json" + fi + record_value=$(get_json_field "rrset_values" "$record_json") + if [ "$debug" = "yes" ]; then + printf "record_value:\\n---\\n%s\\n---\\n\\n" "$record_value" + fi + record_ttl=$(get_json_field "rrset_ttl" "$record_json") + record_count=$(printf "%s" "$record_value" | wc -w) + # If a custom TTL wasn't provided, just set it to the existing one. + # If the record TTL is empty (because the record doesn't exist) and + # no custom TTL was provided, set a default. + if [ -z "$record_ttl" -a -z "$ttl" ]; then + new_ttl="10800" + elif [ -z "$ttl" ]; then + new_ttl="$record_ttl" + else + new_ttl="$ttl" + fi + fi + if [ "$record_count" -gt "1" ]; then + printf "Sorry, but gad does not support updating multiple records with the same name.\\n" + exit 1 + elif [ -z "$record_value" ]; then + if [ -z "$records_to_create" ]; then + records_to_create="$1" + else + records_to_create="${records_to_create} ${1}" + fi + elif [ "$ext_ip" != "$record_value" -o "$new_ttl" != "$record_ttl" -o "$force" = "yes" ]; then + if [ -z "$records_to_update" ]; then + records_to_update="$1" + else + records_to_update="${records_to_update} ${1}" + fi + fi + if [ "$debug" = "yes" ]; then + printf "Results after checking record:\\n---\\nrecord: %s\\nrecord_value: %s\\nrecords_to_create: %s\\nrecords_to_update: %s\\n---\\n\\n" "$1" "$record_value" "$records_to_create" "$records_to_update" + fi + shift + done } # @@ -355,25 +376,25 @@ check() { # if [ "$stdin_ip" = "yes" ]; then - ext_ip_method="standard input" - read ext_ip + ext_ip_method="standard input" + read ext_ip elif [ ! -z "$ext_if" ]; then - ext_ip_method="ifconfig ${ext_if}" - ext_ip=$(ifconfig "$ext_if" | sed -n "s/.*${inet} \(addr:\)* *${ip_regex}.*/\2/p" | head -1) + ext_ip_method="ifconfig ${ext_if}" + ext_ip=$(ifconfig "$ext_if" | sed -n "s/.*${inet} \(addr:\)* *${ip_regex}.*/\2/p" | head -1) else - ext_ip_method="ICanHazIP.com" - if [ "$record_type" = "A" ]; then - ext_ip=$(curl -s -4 https://ipv4.icanhazip.com/) - else - ext_ip=$(curl -s -6 https://ipv6.icanhazip.com/) - fi + ext_ip_method="ICanHazIP.com" + if [ "$record_type" = "A" ]; then + ext_ip=$(curl -s -4 https://ipv4.icanhazip.com/) + else + ext_ip=$(curl -s -6 https://ipv6.icanhazip.com/) + fi fi if [ -z "$ext_ip" ]; then - printf "Failed to determine external IP address with %s. See above error.\\n" "$ext_ip_method" - exit 1 + printf "Failed to determine external IP address with %s. See above error.\\n" "$ext_ip_method" + exit 1 fi if [ "$debug" = "yes" ]; then - printf "IP information:\\n---\\next_ip_method: %s\\next_ip: %s\\n---\\n\\n" "$ext_ip_method" "$ext_ip" + printf "IP information:\\n---\\next_ip_method: %s\\next_ip: %s\\n---\\n\\n" "$ext_ip_method" "$ext_ip" fi # @@ -381,20 +402,20 @@ fi # if [ "$v5" = "yes" ]; then - zone_id=$(rpc "domain.info" "string" "$domain" | grep -A 1 zone_id | sed -n 's/.*<int>\([0-9]*\).*/\1/p') + zone_id=$(rpc "domain.info" "string" "$domain" | grep -A 1 zone_id | sed -n 's/.*<int>\([0-9]*\).*/\1/p') else - domain_json=$(rest "GET" "domains/${domain}") - if [ "$debug" = "yes" ]; then - printf "domain_json:\\n---\\n%s\\n---\\n\\n" "$domain_json" - fi - zone_id=$(get_json_field "zone_uuid" "$domain_json") + domain_json=$(rest "GET" "domains/${domain}") + if [ "$debug" = "yes" ]; then + printf "domain_json:\\n---\\n%s\\n---\\n\\n" "$domain_json" + fi + zone_id=$(get_json_field "zone_uuid" "$domain_json") fi if [ -z "$zone_id" ]; then - printf "No zone_id returned. This is expected with Gandi's test API or if you send a LiveDNS API key to Gandi's legacy API. Use gad's -t flag for testing or the -5 flag for LiveDNS.\\n" - exit 1 + printf "No zone_id returned. This is expected with Gandi's test API or if you send a LiveDNS API key to Gandi's legacy API. Use gad's -t flag for testing or the -5 flag for LiveDNS.\\n" + exit 1 fi if [ "$debug" = "yes" ]; then - printf "zone_id:\\n---\\n%s\\n---\\n\\n" "$zone_id" + printf "zone_id:\\n---\\n%s\\n---\\n\\n" "$zone_id" fi # @@ -410,43 +431,43 @@ set +f # if [ ! -z "$records_to_update" -o ! -z "$records_to_create" ]; then - if [ "$v5" = "yes" ]; then - new_version_id=$(rpc "domain.zone.version.new" "int" "$zone_id" | sed -n 's/.*<int>\([0-9]*\).*/\1/p') - if [ "$debug" = "yes" ]; then - printf "new_version_id:\\n---\\n%s\\n---\\n\\n" "$new_version_id" - fi - set -f - update $records_to_update - create $records_to_create - set +f - if [ "$testing" != "yes" ]; then - printf "Activating version %s of the zonefile for domain %s...\\n\\nopenssl s_client output and domain.zone.version.set() method response:\\n\\n" "$new_version_id" "$domain" - rpc "domain.zone.version.set" "int" "$zone_id" "int" "$new_version_id" - printf "\\nTried to update the following %s records to %s: %s %s\\n\\nThere is no error checking on the RPCs so check the web interface if you want to be sure the update was successful, or look at the methodResponse from domain.zone.version.set() above (a response of "1" means success).\\n" "$record_type" "$ext_ip" "$records_to_update" "$records_to_create" - else - printf "Created version %s of the zonefile for domain %s.\\n\\nTried to update the following %s records to %s: %s %s\\n\\nThere is no error checking on the RPCs so check the web interface if you want to be sure the update was successful.\\n" "$new_version_id" "$domain" "$record_type" "$ext_ip" "$records_to_update" "$records_to_create" - fi - exit - else - new_snapshot_json=$(rest "POST" "zones/${zone_id}/snapshots" "") - new_snapshot_id=$(get_json_field "uuid" "$new_snapshot_json") - if [ "$debug" = "yes" ]; then - printf "new_snapshot_json:\\n---\\n%s\\n---\\n\\n" "$new_snapshot_json" - printf "new_snapshot_id:\\n---\\n%s\\n---\\n\\n" "$new_snapshot_id" - fi - if [ "$testing" != "yes" ]; then - set -f - update $records_to_update - create $records_to_create - set +f - printf "Created a new snapshot and tried to update the following live %s records to %s with TTL of %s seconds: %s %s\\n" "$record_type" "$ext_ip" "$new_ttl" "$records_to_update" "$records_to_create" - else - printf "Testing mode! Not sending any updates to the LiveDNS API.\\nIn non-testing mode, gad would have tried to update the following live %s records to %s with TTL of %s seconds: %s %s\\n" "$record_type" "$ext_ip" "$new_ttl" "$records_to_update" "$records_to_create" - fi - fi + if [ "$v5" = "yes" ]; then + new_version_id=$(rpc "domain.zone.version.new" "int" "$zone_id" | sed -n 's/.*<int>\([0-9]*\).*/\1/p') + if [ "$debug" = "yes" ]; then + printf "new_version_id:\\n---\\n%s\\n---\\n\\n" "$new_version_id" + fi + set -f + update $records_to_update + create $records_to_create + set +f + if [ "$testing" != "yes" ]; then + printf "Activating version %s of the zonefile for domain %s...\\n\\nopenssl s_client output and domain.zone.version.set() method response:\\n\\n" "$new_version_id" "$domain" + rpc "domain.zone.version.set" "int" "$zone_id" "int" "$new_version_id" + printf "\\nTried to update the following %s records to %s: %s %s\\n\\nThere is no error checking on the RPCs so check the web interface if you want to be sure the update was successful, or look at the methodResponse from domain.zone.version.set() above (a response of "1" means success).\\n" "$record_type" "$ext_ip" "$records_to_update" "$records_to_create" + else + printf "Created version %s of the zonefile for domain %s.\\n\\nTried to update the following %s records to %s: %s %s\\n\\nThere is no error checking on the RPCs so check the web interface if you want to be sure the update was successful.\\n" "$new_version_id" "$domain" "$record_type" "$ext_ip" "$records_to_update" "$records_to_create" + fi + exit + else + new_snapshot_json=$(rest "POST" "zones/${zone_id}/snapshots" "") + new_snapshot_id=$(get_json_field "uuid" "$new_snapshot_json") + if [ "$debug" = "yes" ]; then + printf "new_snapshot_json:\\n---\\n%s\\n---\\n\\n" "$new_snapshot_json" + printf "new_snapshot_id:\\n---\\n%s\\n---\\n\\n" "$new_snapshot_id" + fi + if [ "$testing" != "yes" ]; then + set -f + update $records_to_update + create $records_to_create + set +f + printf "Created a new snapshot and tried to update the following live %s records to %s with TTL of %s seconds: %s %s\\n" "$record_type" "$ext_ip" "$new_ttl" "$records_to_update" "$records_to_create" + else + printf "Testing mode! Not sending any updates to the LiveDNS API.\\nIn non-testing mode, gad would have tried to update the following live %s records to %s with TTL of %s seconds: %s %s\\n" "$record_type" "$ext_ip" "$new_ttl" "$records_to_update" "$records_to_create" + fi + fi else - if [ "$verbose" = "yes" ]; then - printf "External IP address %s detected with %s and TTL value of %s matches records: %s. No update needed. Exiting.\\n" "$ext_ip" "$ext_ip_method" "$new_ttl" "$records" - fi - exit + if [ "$verbose" = "yes" ]; then + printf "External IP address %s detected with %s and TTL value of %s matches records: %s. No update needed. Exiting.\\n" "$ext_ip" "$ext_ip_method" "$new_ttl" "$records" + fi + exit fi diff --git a/.local/bin/gitrect-clean b/.local/bin/gitrect-clean index 163f9b6..555094e 100755 --- a/.local/bin/gitrect-clean +++ b/.local/bin/gitrect-clean @@ -13,8 +13,8 @@ touch $PREFIX # Reset all variables that might be set DEFAULT_STATE="$HOME/.local/share/git/repolist" WORKDIR="$HOME/.code" -verbose=0 # Variables to be evaluated as shell arithmetic - # should be initialized to a default or validated beforehand. +verbose=0 # Variables to be evaluated as shell arithmetic +# should be initialized to a default or validated beforehand. usage="$(basename "$0") [-h] [-v -w WORKDIR -f FILE] @@ -27,51 +27,51 @@ Scan directories in the state file and delete those not referenced: " while :; do - case $1 in - -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. - echo "$usage" - exit - ;; - -f) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - DEFAULT_STATE=$2 - shift - fi - ;; - -f=?*) - DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -f=) # Handle the case of an empty --file= - printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - -w) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - WORKDIR=$2 - shift - fi - ;; - -w=?*) - WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -w=) # Handle the case of an empty --file= - printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - -v) - verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity. - ;; - --) # End of all options. - shift - break - ;; - -?*) - printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 - ;; - *) # Default case: If no more options then break out of the loop. - break ;; - esac - shift + case $1 in + -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. + echo "$usage" + exit + ;; + -f) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + DEFAULT_STATE=$2 + shift + fi + ;; + -f=?*) + DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -f=) # Handle the case of an empty --file= + printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + -w) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + WORKDIR=$2 + shift + fi + ;; + -w=?*) + WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -w=) # Handle the case of an empty --file= + printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + -v) + verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity. + ;; + --) # End of all options. + shift + break + ;; + -?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + *) # Default case: If no more options then break out of the loop. + break ;; + esac + shift done # If there are input files (for example) that follow the options, they @@ -80,43 +80,41 @@ done # No posix-compliant way of getting processor count, so leverage OS-specifics FS_SEPARATOR="/" if [ "$(uname -s)" = "Darwin" ]; then - NPROC=$(eval "sysctl -n hw.ncpu") + NPROC=$(eval "sysctl -n hw.ncpu") elif [ "$(uname -s)" = "Linux" ]; then - NPROC=$(eval "nproc") + NPROC=$(eval "nproc") else - # Windows uses backslash - FS_SEPARATOR="\\" - if [ $verbose -gt 0 ]; then - echo "cannot detect CPU total" - fi + # Windows uses backslash + FS_SEPARATOR="\\" + if [ $verbose -gt 0 ]; then + echo "cannot detect CPU total" + fi fi - # This double loop is super inefficient, but, it works (?) while read -r line; do - dir=$(echo $line | cut -d, -f1|rev|cut -d/ -f2-|rev) #top directories - echo ${dir}\$ >> $PREFIX + dir=$(echo $line | cut -d, -f1 | rev | cut -d/ -f2- | rev) #top directories + echo ${dir}\$ >>$PREFIX done <$DEFAULT_STATE uniq $PREFIX $TEMPFILE rm $PREFIX while read -r line; do - dir=$(echo $line | cut -d, -f 1) - echo $dir >> $TEMPFILE + dir=$(echo $line | cut -d, -f 1) + echo $dir >>$TEMPFILE done <$DEFAULT_STATE - if [ $verbose -gt 0 ]; then - find ${WORKDIR}/* -depth \ - | sed -e "s,${WORKDIR}/,," \ - | grep -E -v -f ${TEMPFILE} \ - | xargs -P $NPROC -t -r -I {} rm -rf ${WORKDIR}${FS_SEPARATOR}{} + find ${WORKDIR}/* -depth | + sed -e "s,${WORKDIR}/,," | + grep -E -v -f ${TEMPFILE} | + xargs -P $NPROC -t -r -I {} rm -rf ${WORKDIR}${FS_SEPARATOR}{} else - find ${WORKDIR}/* -depth \ - | sed -e "s,${WORKDIR}/,," \ - | grep -E -v -f ${TEMPFILE} \ - | xargs -P $NPROC -r -I {} rm -rf ${WORKDIR}${FS_SEPARATOR}{} + find ${WORKDIR}/* -depth | + sed -e "s,${WORKDIR}/,," | + grep -E -v -f ${TEMPFILE} | + xargs -P $NPROC -r -I {} rm -rf ${WORKDIR}${FS_SEPARATOR}{} fi rm $TEMPFILE diff --git a/.local/bin/gitrect-fix b/.local/bin/gitrect-fix index 2790ace..aebebbb 100755 --- a/.local/bin/gitrect-fix +++ b/.local/bin/gitrect-fix @@ -14,8 +14,8 @@ WORKDIR="$HOME/.code" verbose=0 # Variables to be evaluated as shell arithmetic should be initialized to a default or validated beforehand. if [ ! $(command -v git) ]; then - echo "git command not found. git must be installed and available in \$PATH to continue" - exit 1 + echo "git command not found. git must be installed and available in \$PATH to continue" + exit 1 fi usage="$(basename "$0") [-h] [-v -w WORKDIR -f FILE] @@ -29,51 +29,51 @@ Set remotes and pull new changes where: " while :; do - case $1 in - -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. - echo "$usage" - exit - ;; - -f) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - DEFAULT_STATE=$2 - shift - fi - ;; - -f=?*) - DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -f=) # Handle the case of an empty --file= - printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - -w) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - WORKDIR=$2 - shift - fi - ;; - -w=?*) - WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -w=) # Handle the case of an empty --file= - printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - -v) - verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity. - ;; - --) # End of all options. - shift - break - ;; - -?*) - printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 - ;; - *) # Default case: If no more options then break out of the loop. - break ;; - esac - shift + case $1 in + -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. + echo "$usage" + exit + ;; + -f) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + DEFAULT_STATE=$2 + shift + fi + ;; + -f=?*) + DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -f=) # Handle the case of an empty --file= + printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + -w) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + WORKDIR=$2 + shift + fi + ;; + -w=?*) + WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -w=) # Handle the case of an empty --file= + printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + -v) + verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity. + ;; + --) # End of all options. + shift + break + ;; + -?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + *) # Default case: If no more options then break out of the loop. + break ;; + esac + shift done # If there are input files (for example) that follow the options, they @@ -83,72 +83,72 @@ done FS_SEPARATOR="/" if [ "$(uname -s)" = "Darwin" ]; then - NPROC=$(eval "sysctl -n hw.ncpu") + NPROC=$(eval "sysctl -n hw.ncpu") elif [ "$(uname -s)" = "Linux" ]; then - NPROC=$(eval "nproc") + NPROC=$(eval "nproc") else - # Windows uses backslash - FS_SEPARATOR="\\" - if [ $verbose -gt 0 ]; then - echo "cannot detect CPU total" - fi + # Windows uses backslash + FS_SEPARATOR="\\" + if [ $verbose -gt 0 ]; then + echo "cannot detect CPU total" + fi fi while read -r line; do - dir=$(echo $line | cut -d, -f 1) - remlist=$(echo $line | cut -d, -f 2-) - clone=0 #False - if [ $verbose -gt 0 ]; then - echo "Operating on ${WORKDIR}${FS_SEPARATOR}${dir}" - fi - if [ ! -d "${WORKDIR}${FS_SEPARATOR}${dir}" ]; then - #clone repo - clone=$((clone + 1)) # Set true - mkdir -p "${WORKDIR}${FS_SEPARATOR}${dir}" - fi - cd "${WORKDIR}${FS_SEPARATOR}${dir}" || exit - if [ $clone -gt 0 ]; then - git init - fi - if [ $verbose -gt 1 ]; then - echo "Remote list: ${remlist}" - fi - echo "${remlist}" | awk -F ',' '{for (i = 1; i <= NF; i++) {printf "%s \n", $i}; printf "\n"}' | while read -r r || [ -n "$r" ]; do - remoteName=$(echo $r | awk -F'=' '{print $1}') # Just get the remote name - # Allow for blank / newlines during processing - if [ "${remoteName}" = "" ]; then continue; fi - if [ -d .git/refs/remotes/$remoteName ]; then - if [ $verbose -gt 0 ]; then - echo "Setting remote ${r}" - fi - echo $r | awk -F'=' '{ print $1, $2; }' | xargs -n 2 git remote set-url - else + dir=$(echo $line | cut -d, -f 1) + remlist=$(echo $line | cut -d, -f 2-) + clone=0 #False + if [ $verbose -gt 0 ]; then + echo "Operating on ${WORKDIR}${FS_SEPARATOR}${dir}" + fi + if [ ! -d "${WORKDIR}${FS_SEPARATOR}${dir}" ]; then + #clone repo + clone=$((clone + 1)) # Set true + mkdir -p "${WORKDIR}${FS_SEPARATOR}${dir}" + fi + cd "${WORKDIR}${FS_SEPARATOR}${dir}" || exit + if [ $clone -gt 0 ]; then + git init + fi + if [ $verbose -gt 1 ]; then + echo "Remote list: ${remlist}" + fi + echo "${remlist}" | awk -F ',' '{for (i = 1; i <= NF; i++) {printf "%s \n", $i}; printf "\n"}' | while read -r r || [ -n "$r" ]; do + remoteName=$(echo $r | awk -F'=' '{print $1}') # Just get the remote name + # Allow for blank / newlines during processing + if [ "${remoteName}" = "" ]; then continue; fi + if [ -d .git/refs/remotes/$remoteName ]; then + if [ $verbose -gt 0 ]; then + echo "Setting remote ${r}" + fi + echo $r | awk -F'=' '{ print $1, $2; }' | xargs -n 2 git remote set-url + else - if [ $verbose -gt 0 ]; then - echo "Adding remote ${r}" - fi - echo $r | awk -F'=' '{ print $1, $2; }' | xargs -n 2 git remote add - fi - done - if [ $clone -gt 0 ]; then - git fetch --all - rev=$(git ls-remote origin | grep HEAD | cut -f 1) - if [ $verbose -gt 0 ]; then - echo "Rev: ${rev}" - fi - br=$(git ls-remote origin | grep "${rev}" | grep -v HEAD | cut -f 2 | cut -d / -f 3|head -n 1) - if [ $verbose -gt 0 ]; then - echo "Branch: ${br}" - fi - git switch $br - fi + if [ $verbose -gt 0 ]; then + echo "Adding remote ${r}" + fi + echo $r | awk -F'=' '{ print $1, $2; }' | xargs -n 2 git remote add + fi + done + if [ $clone -gt 0 ]; then + git fetch --all + rev=$(git ls-remote origin | grep HEAD | cut -f 1) + if [ $verbose -gt 0 ]; then + echo "Rev: ${rev}" + fi + br=$(git ls-remote origin | grep "${rev}" | grep -v HEAD | cut -f 2 | cut -d / -f 3 | head -n 1) + if [ $verbose -gt 0 ]; then + echo "Branch: ${br}" + fi + git switch $br + fi done <$DEFAULT_STATE # Update all remotes if we have the fetchgit command if [ $(command -v fetchgit) ]; then - if [ $(command -v fd) ]; then - fd -H -I -t d -E mod -E vendor "\.git$" $WORKDIR -x fetchgit - else - find $WORKDIR -name ".git" -exec fetchgit {} \; - fi + if [ $(command -v fd) ]; then + fd -H -I -t d -E mod -E vendor "\.git$" $WORKDIR -x fetchgit + else + find $WORKDIR -name ".git" -exec fetchgit {} \; + fi fi diff --git a/.local/bin/gitrect-update b/.local/bin/gitrect-update index ca0a23e..0ec9415 100755 --- a/.local/bin/gitrect-update +++ b/.local/bin/gitrect-update @@ -19,8 +19,8 @@ verbose=0 # Variables to be evaluated as shell arithmetic should be initialized noconfirm=0 if [ ! $(command -v git) ]; then - echo "git command not found. git must be installed and available in \$PATH to continue" - exit 1 + echo "git command not found. git must be installed and available in \$PATH to continue" + exit 1 fi usage="$(basename "$0") [-h] [-v -w WORKDIR -f FILE] @@ -36,62 +36,62 @@ where: " while :; do - case $1 in - -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. - echo "$usage" - exit - ;; - -f) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - DEFAULT_STATE=$2 - shift - fi - ;; - -f=?*) - DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -f=) # Handle the case of an empty --file= - printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - -w) # Takes an option argument, ensuring it has been specified. - if [ -n "$2" ]; then - WORKDIR=$2 - shift - fi - ;; - -w=?*) - WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder. - ;; - -w=) # Handle the case of an empty --file= - printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2 - exit 1 - ;; - -y) - noconfirm=1 - ;; - -v) - verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity. - ;; - --) # End of all options. - shift - break - ;; - -?*) - printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 - ;; - *) # Default case: If no more options then break out of the loop. - break ;; - esac - shift + case $1 in + -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. + echo "$usage" + exit + ;; + -f) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + DEFAULT_STATE=$2 + shift + fi + ;; + -f=?*) + DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -f=) # Handle the case of an empty --file= + printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + -w) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + WORKDIR=$2 + shift + fi + ;; + -w=?*) + WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + -w=) # Handle the case of an empty --file= + printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2 + exit 1 + ;; + -y) + noconfirm=1 + ;; + -v) + verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity. + ;; + --) # End of all options. + shift + break + ;; + -?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + *) # Default case: If no more options then break out of the loop. + break ;; + esac + shift done # Make sure local state folder is available if [ ! -d "$HOME/.local/share" ]; then - if [ $verbose -gt 0 ]; then - echo "Creating directory: $HOME/.local/share" - fi - mkdir -p ~/.local/share + if [ $verbose -gt 0 ]; then + echo "Creating directory: $HOME/.local/share" + fi + mkdir -p ~/.local/share fi # Rest of the program here. @@ -99,39 +99,39 @@ fi # will remain in the "$@" positional parameters. if [ "$(command -v fd)" ]; then - LIST=$(eval "fd -I -H -t d '\.git$' ${WORKDIR}") + LIST=$(eval "fd -I -H -t d '\.git$' ${WORKDIR}") else - LIST=$(eval "find -type d -name \.git ${WORKDIR}") + LIST=$(eval "find -type d -name \.git ${WORKDIR}") fi # for each directory, create a line in TEMPFILE equivalent to: # short/path/here,origin=url1,upstream=url2,... # for dirpath in $LIST; do - # shortpath trims the working directory prefix and the '.git' suffix from the directory - shortpath=$(eval "echo $dirpath | sed -e 's,${WORKDIR}/,,' -e 's,/\.git/,,'") - if [ $verbose -gt 1 ]; then - echo "Repository found: ${shortpath}" >&2 - fi - - cd "${dirpath}/.." # change into the parent of the git directory - rems=$(eval "git remote") # list all remotes - len_rems=${#rems} - if [ $len_rems -lt 1 ]; then - # If the list of remotes is zero, skip this folder - continue - fi - - # We haven't skipped the path, so write it to the file - printf "%s" $shortpath >>$TEMPFILE - for r in $rems; do - url=$(eval "git remote get-url ${r}") - printf ",%s=%s" $r $url >>$TEMPFILE - if [ $verbose -gt 1 ]; then - echo "remote: ${r} ${url} " >&2 - fi - done - printf "\n" >>$TEMPFILE + # shortpath trims the working directory prefix and the '.git' suffix from the directory + shortpath=$(eval "echo $dirpath | sed -e 's,${WORKDIR}/,,' -e 's,/\.git/,,'") + if [ $verbose -gt 1 ]; then + echo "Repository found: ${shortpath}" >&2 + fi + + cd "${dirpath}/.." # change into the parent of the git directory + rems=$(eval "git remote") # list all remotes + len_rems=${#rems} + if [ $len_rems -lt 1 ]; then + # If the list of remotes is zero, skip this folder + continue + fi + + # We haven't skipped the path, so write it to the file + printf "%s" $shortpath >>$TEMPFILE + for r in $rems; do + url=$(eval "git remote get-url ${r}") + printf ",%s=%s" $r $url >>$TEMPFILE + if [ $verbose -gt 1 ]; then + echo "remote: ${r} ${url} " >&2 + fi + done + printf "\n" >>$TEMPFILE done # Sort results @@ -139,32 +139,32 @@ sort -o ${TEMPFILE} ${TEMPFILE} diffval=$(eval "diff -b ${DEFAULT_STATE} ${TEMPFILE}") if [ $? -eq 0 ]; then - if [ $verbose -gt 0 ]; then - echo "No change. Configuration already up to date." >&2 - fi - # Cleanup temp file - rm "${TEMPFILE}" + if [ $verbose -gt 0 ]; then + echo "No change. Configuration already up to date." >&2 + fi + # Cleanup temp file + rm "${TEMPFILE}" else - if [ $verbose -gt 0 ]; then - echo "Diff between current and updated state: " >&2 - echo "$diffval" >&2 - fi - if [ $noconfirm -ne 0 ]; then - # Overwite origiaal with the new - mv "${TEMPFILE}" "${DEFAULT_STATE}" - else - printf 'Accept changes (y/n)? \n' - old_stty_cfg=$(stty -g) - stty raw -echo - answer=$(while ! head -c 1 | grep -i '[ny]'; do true; done) - stty $old_stty_cfg - if [ "$answer" != "${answer#[Yy]}" ]; then - echo "Updating file..." - # Overwite origiaal with the new - mv "${TEMPFILE}" "${DEFAULT_STATE}" - else - echo "Discarding changes..." - rm "${TEMPFILE}" - fi - fi + if [ $verbose -gt 0 ]; then + echo "Diff between current and updated state: " >&2 + echo "$diffval" >&2 + fi + if [ $noconfirm -ne 0 ]; then + # Overwite origiaal with the new + mv "${TEMPFILE}" "${DEFAULT_STATE}" + else + printf 'Accept changes (y/n)? \n' + old_stty_cfg=$(stty -g) + stty raw -echo + answer=$(while ! head -c 1 | grep -i '[ny]'; do true; done) + stty $old_stty_cfg + if [ "$answer" != "${answer#[Yy]}" ]; then + echo "Updating file..." + # Overwite origiaal with the new + mv "${TEMPFILE}" "${DEFAULT_STATE}" + else + echo "Discarding changes..." + rm "${TEMPFILE}" + fi + fi fi diff --git a/.local/bin/gotools.sh b/.local/bin/gotools.sh index 1c849a4..59d4879 100755 --- a/.local/bin/gotools.sh +++ b/.local/bin/gotools.sh @@ -1,37 +1,37 @@ #!/usr/bin/env sh if [ "$(command -v go)" ]; then - # Always clean caches - go clean -cache; - go clean -modcache; + # Always clean caches + go clean -cache + go clean -modcache - rm $GOPATH/bin/golangci-lint - echo "install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2" - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2 + rm $GOPATH/bin/golangci-lint + echo "install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2" + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2 - rm $GOPATH/bin/goplay - echo "install github.com/haya14busa/goplay/cmd/goplay@latest" - go install github.com/haya14busa/goplay/cmd/goplay@latest + rm $GOPATH/bin/goplay + echo "install github.com/haya14busa/goplay/cmd/goplay@latest" + go install github.com/haya14busa/goplay/cmd/goplay@latest - rm $GOPATH/bin/impl - echo "install github.com/josharian/impl@latest" - go install github.com/josharian/impl@latest + rm $GOPATH/bin/impl + echo "install github.com/josharian/impl@latest" + go install github.com/josharian/impl@latest - rm $GOPATH/bin/gomodifytags - echo "install github.com/fatih/gomodifytags@latest" - go install github.com/fatih/gomodifytags@latest + rm $GOPATH/bin/gomodifytags + echo "install github.com/fatih/gomodifytags@latest" + go install github.com/fatih/gomodifytags@latest - rm $GOPATH/bin/gotests - echo "install github.com/cweill/gotests/gotests@latest" - go install github.com/cweill/gotests/gotests@latest + rm $GOPATH/bin/gotests + echo "install github.com/cweill/gotests/gotests@latest" + go install github.com/cweill/gotests/gotests@latest - rm $GOPATH/bin/dlv - echo "install github.com/go-delve/delve/cmd/dlv@latest" - go install github.com/go-delve/delve/cmd/dlv@latest + rm $GOPATH/bin/dlv + echo "install github.com/go-delve/delve/cmd/dlv@latest" + go install github.com/go-delve/delve/cmd/dlv@latest - rm $GOPATH/bin/gopls - echo "install golang.org/x/tools/gopls@latest" - go install golang.org/x/tools/gopls@latest + rm $GOPATH/bin/gopls + echo "install golang.org/x/tools/gopls@latest" + go install golang.org/x/tools/gopls@latest else - echo "Go not found" + echo "Go not found" fi diff --git a/.local/bin/ssg b/.local/bin/ssg index e220a41..82b88b1 100755 --- a/.local/bin/ssg +++ b/.local/bin/ssg @@ -21,23 +21,21 @@ main() { test -n "$2" || usage test -n "$3" || usage test -n "$4" || usage - test -d "$1" || no_dir "$1" - test -d "$2" || no_dir "$2" + test -d "$1" || no_dir "$1" + test -d "$2" || no_dir "$2" src=$(readlink_f "$1") dst=$(readlink_f "$2") IGNORE=$( - if ! test -f "$src/.ssgignore" - then + if ! test -f "$src/.ssgignore"; then printf ' ! -path "*/.*"' return fi - while read -r x - do + while read -r x; do test -n "$x" || continue printf ' ! -path "*/%s*"' "$x" - done < "$src/.ssgignore" + done <"$src/.ssgignore" ) # files @@ -50,35 +48,31 @@ main() { test -f "$h_file" && HEADER=$(cat "$h_file") && export HEADER list_dirs "$src" | - (cd "$src" && cpio -pdu "$dst") + (cd "$src" && cpio -pdu "$dst") fs=$( - if test -f "$dst/.files" - then list_affected_files "$src" "$dst/.files" - else list_files "$1" - fi + if test -f "$dst/.files"; then + list_affected_files "$src" "$dst/.files" + else + list_files "$1" + fi ) - if test -n "$fs" - then + if test -n "$fs"; then echo "$fs" | tee "$dst/.files" - if echo "$fs" | grep -q '\.md$' - then - if test -x "$(which pandoc 2> /dev/null)" - then + if echo "$fs" | grep -q '\.md$'; then + if test -x "$(which pandoc 2>/dev/null)"; then echo "Using pandoc" echo "$fs" | grep '\.md$' | - render_md_files_pandoc "$src" "$dst" "$title" - elif test -x "$(which lowdown 2> /dev/null)" - then + render_md_files_pandoc "$src" "$dst" "$title" + elif test -x "$(which lowdown 2>/dev/null)"; then echo "$fs" | grep '\.md$' | - render_md_files_lowdown "$src" "$dst" "$title" + render_md_files_lowdown "$src" "$dst" "$title" else - if test -x "$(which Markdown.pl 2> /dev/null)" - then + if test -x "$(which Markdown.pl 2>/dev/null)"; then echo "$fs" | grep '\.md$' | - render_md_files_Markdown_pl "$src" "$dst" "$title" + render_md_files_Markdown_pl "$src" "$dst" "$title" else echo "couldn't find lowdown nor Markdown.pl" exit 3 @@ -87,36 +81,33 @@ main() { fi echo "$fs" | grep '\.html$' | - render_html_files "$src" "$dst" "$title" + render_html_files "$src" "$dst" "$title" echo "$fs" | grep -Ev '\.md$|\.html$' | - (cd "$src" && cpio -pu "$dst") + (cd "$src" && cpio -pu "$dst") fi printf '[ssg] ' >&2 print_status 'file, ' 'files, ' "$fs" >&2 - # sitemap base_url="$4" date=$(date +%Y-%m-%d) - urls=$(list_pages "$src") + urls=$(list_pages "$src") test -n "$urls" && - render_sitemap "$urls" "$base_url" "$date" > "$dst/sitemap.xml" + render_sitemap "$urls" "$base_url" "$date" >"$dst/sitemap.xml" print_status 'url' 'urls' "$urls" >&2 echo >&2 } - readlink_f() { file="$1" cd "$(dirname "$file")" file=$(basename "$file") - while test -L "$file" - do + while test -L "$file"; do file=$(readlink "$file") cd "$(dirname "$file")" file=$(basename "$file") @@ -125,7 +116,6 @@ readlink_f() { echo "$dir/$file" } - print_status() { test -z "$3" && printf 'no %s' "$2" && return @@ -136,13 +126,11 @@ print_status() { }' } - usage() { echo "usage: ${0##*/} src dst title base_url" >&2 exit 1 } - no_dir() { echo "${0##*/}: $1: No such directory" >&2 exit 2 @@ -152,14 +140,12 @@ list_dirs() { cd "$1" && eval "find . -type d ! -name '.' ! -path '*/_*' $IGNORE" } - list_files() { cd "$1" && eval "find . -type f ! -name '.' ! -path '*/_*' $IGNORE" } - -list_dependant_files () { - e="\\( -name '*.html' -o -name '*.md' -o -name '*.css' -o -name '*.js' \\)" +list_dependant_files() { + e="\\( -name '*.html' -o -name '*.md' -o -name '*.css' -o -name '*.js' \\)" cd "$1" && eval "find . -type f ! -name '.' ! -path '*/_*' $IGNORE $e" } @@ -167,65 +153,58 @@ list_newer_files() { cd "$1" && eval "find . -type f ! -name '.' $IGNORE -newer $2" } - has_partials() { grep -qE '^./_.*\.html$|^./_.*\.js$|^./_.*\.css$' } - list_affected_files() { fs=$(list_newer_files "$1" "$2") - if echo "$fs" | has_partials - then list_dependant_files "$1" - else echo "$fs" + if echo "$fs" | has_partials; then + list_dependant_files "$1" + else + echo "$fs" fi } - render_html_files() { - while read -r f - do render_html_file "$3" < "$1/$f" > "$2/$f" + while read -r f; do + render_html_file "$3" <"$1/$f" >"$2/$f" done } - render_md_files_lowdown() { - while read -r f - do + while read -r f; do lowdown \ - --html-no-head-ids \ - --html-no-escapehtml \ - --html-no-skiphtml \ - < "$1/$f" | - render_html_file "$3" "$1/$f" \ - > "$2/${f%\.md}.html" + --html-no-head-ids \ + --html-no-escapehtml \ + --html-no-skiphtml \ + <"$1/$f" | + render_html_file "$3" "$1/$f" \ + >"$2/${f%\.md}.html" done } render_md_files_pandoc() { - while read -r f - do - pandoc -f markdown --from markdown-auto_identifiers-implicit_figures < "$1/$f" | \ + while read -r f; do + pandoc -f markdown --from markdown-auto_identifiers-implicit_figures <"$1/$f" | render_html_file "$3" "$1/$f" \ - > "$2/${f%\.md}.html" + >"$2/${f%\.md}.html" done } render_md_files_Markdown_pl() { - while read -r f - do - Markdown.pl < "$1/$f" | - render_html_file "$3" "$1/$f" \ - > "$2/${f%\.md}.html" + while read -r f; do + Markdown.pl <"$1/$f" | + render_html_file "$3" "$1/$f" \ + >"$2/${f%\.md}.html" done } - render_html_file() { - # h/t Devin Teske - lastmod=$(date -d @$(stat -c %Y "$2")) - awk -v title="$1" -v moddate="$lastmod" -v srcfile="$2" ' + # h/t Devin Teske + lastmod=$(date -d @$(stat -c %Y "$2")) + awk -v title="$1" -v moddate="$lastmod" -v srcfile="$2" ' { body = body "\n" $0 } END { body = substr(body, 2) @@ -263,14 +242,11 @@ render_html_file() { }' } - - list_pages() { cd "$1" && find -name '*.md' -not -name '404.md' -type f -printf "%TY-%Tm-%Td %p\n" | - sed 's#./##;s#.md$#.html#;s#/index.html$#/#' + sed 's#./##;s#.md$#.html#;s#/index.html$#/#' } - render_sitemap() { urls="$1" base_url="$2" @@ -283,7 +259,7 @@ render_sitemap() { echo 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"' echo 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' echo "$urls" | - awk -v base="$base_url" '{print "<url><loc>"base"/"$2"</loc><lastmod>"$1"</lastmod><priority>1.0</priority></url>"}' + awk -v base="$base_url" '{print "<url><loc>"base"/"$2"</loc><lastmod>"$1"</lastmod><priority>1.0</priority></url>"}' echo '</urlset>' } diff --git a/.local/bin/wat b/.local/bin/wat index 7d5cd14..c359b35 100755 --- a/.local/bin/wat +++ b/.local/bin/wat @@ -1,37 +1,37 @@ #!/bin/sh log_lines() { - if [ "$SHOW_TIMESTAMPS" -eq 1 ]; then - regex_start='\[\([^]]*\)\] ' - replacement='\1 \3' - else - regex_start='.*' - replacement='\2' - fi + if [ "$SHOW_TIMESTAMPS" -eq 1 ]; then + regex_start='\[\([^]]*\)\] ' + replacement='\1 \3' + else + regex_start='.*' + replacement='\2' + fi - if [ "$SHOW_INSTALLED" -eq 1 ]; then - regex_end='\(starting full system upgrade\|upgraded.*\|installed.*\)' - else - regex_end='\(starting full system upgrade\|upgraded.*\)' - fi + if [ "$SHOW_INSTALLED" -eq 1 ]; then + regex_end='\(starting full system upgrade\|upgraded.*\|installed.*\)' + else + regex_end='\(starting full system upgrade\|upgraded.*\)' + fi - regex='^'"$regex_start"'\[\(PACMAN\|ALPM\)\] '"$regex_end"'$' + regex='^'"$regex_start"'\[\(PACMAN\|ALPM\)\] '"$regex_end"'$' - sed "/$regex/!d; s//$replacement/" "$PACMAN_LOG" + sed "/$regex/!d; s//$replacement/" "$PACMAN_LOG" } mark_lines() { - n=1 + n=1 - while read -r line; do - case "$line" in - *starting\ full\ system\ upgrade*) - printf "%i " "$n" - n=$((n + 1)) - ;; - esac + while read -r line; do + case "$line" in + *starting\ full\ system\ upgrade*) + printf "%i " "$n" + n=$((n + 1)) + ;; + esac - printf "%s\n" "$line" - done + printf "%s\n" "$line" + done } : "${PACMAN_LOG:=/var/log/pacman.log}" @@ -39,16 +39,16 @@ mark_lines() { : "${SHOW_TIMESTAMPS:=0}" while [ -n "$1" ]; do - case "$1" in - -i | --installed) SHOW_INSTALLED=1 ;; - -t | --timestamps) SHOW_TIMESTAMPS=1 ;; - -l | --log) - shift - PACMAN_LOG="$1" - ;; - *) break ;; - esac - shift + case "$1" in + -i | --installed) SHOW_INSTALLED=1 ;; + -t | --timestamps) SHOW_TIMESTAMPS=1 ;; + -l | --log) + shift + PACMAN_LOG="$1" + ;; + *) break ;; + esac + shift done n_start=${1:-1} @@ -60,18 +60,18 @@ n_max=$(grep -cF ' starting full system upgrade' "$PACMAN_LOG") end='$' # EOF if [ -n "$2" ]; then - n_stop=$2 + n_stop=$2 - if [ "$n_start" -le "$n_stop" ] || [ "$n_max" -lt "$n_stop" ]; then - # Empty or negative range, or non-intersecting with available log. - exit - fi + if [ "$n_start" -le "$n_stop" ] || [ "$n_max" -lt "$n_stop" ]; then + # Empty or negative range, or non-intersecting with available log. + exit + fi - end="/^\($n_stop\) .*\(starting.*\)$/" + end="/^\($n_stop\) .*\(starting.*\)$/" fi log_lines | tac | mark_lines | tac | - sed " + sed " /^\($n_start\) .*\(starting.*\)$/,$end !d; s//\1 \2/; " |