Skip to content

Instantly share code, notes, and snippets.

@ITler
Last active October 10, 2019 10:04
Show Gist options
  • Save ITler/e50d6b37ef56e49de1ed463c82e68520 to your computer and use it in GitHub Desktop.
Save ITler/e50d6b37ef56e49de1ed463c82e68520 to your computer and use it in GitHub Desktop.
Useful shell functions
gh_api() {
local -r gh_api_auth=${GH_API_AUTH}
local -r method=${1:-GET}
local -r path=${2:-user}
shift 2
http --auth "${gh_api_auth}" ${method} "https://api.github.com/${path}" Accept:application/vnd.github.v3+json "${@}"
}
handle_sops() {
if [[ $(which sops) == '' ]]; then
echo "Mozilla sops could not be found. Make sure it is in your path [${PATH}]"
fi
# Remove duplicate file names (without sorting) and iterate over them
for f in $(echo $@ | tr ' ' '\n' | awk '!x[$0]++'); do
local FILE_NAME=$(basename ${f})
if [[ $f != /* ]]; then
f=$(pwd)/${f}
fi
if [[ -w /tmp ]] && [[ -r ${f} ]]; then
echo "Processing ${f}"
local CREDS_RAW=/tmp/${FILE_NAME/sops/unenc}
sops -d ${f} >${CREDS_RAW}
source ${CREDS_RAW}
rm ${CREDS_RAW}
fi
done
}
vault_api() {
local -r vault_token=${VAULT_TOKEN:-$(pass show my/dev/vault_token)}
local -r method=${1:-GET}
local -r path=${2:-user}
local -r host=${3:-${VAULT_HOST:-"http://127.0.0.1:8200"}}
http ${method} "${host}/v1/${path}" X-Vault-Token:${vault_token}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment