Skip to content

Instantly share code, notes, and snippets.

@micahbf
Last active November 5, 2021 14:50
Show Gist options
  • Save micahbf/fdae2c9760fbb1f6068ff2bacd311c3c to your computer and use it in GitHub Desktop.
Save micahbf/fdae2c9760fbb1f6068ff2bacd311c3c to your computer and use it in GitHub Desktop.
gig - A wrapper script for the gigalixir CLI
#!/bin/zsh
#
# gig
# A wrapper script for the gigalixir CLI
# Dependencies: gigalixir, fzf, jq
# Installation: Make sure dependencies are installed, put this in your $PATH, and chmod +x
# Author: Micah Buckley-Farlee <micah@xlmpq.com>
set -u
set +C
GIG_APPS_FILE="$HOME/.gigalixir-apps.json"
GIG_SELECTED_APP_FILE="$HOME/.gig-selected-app"
export FZF_DEFAULT_OPTS="--height=8 --layout=reverse"
function print_help() {
echo "Usage: gig [command]"
echo ""
echo "Commands:"
echo "current - show the currently selected app"
echo "use - change the currently selected app. Optionally takes a search term."
echo "update - update the list of apps from gigalixir"
echo ""
echo "All other commands are executed with the gigalixir utility."
echo "Run \`gig help\` to see the available commands from gigalixir. "
}
function update_apps_file() {
>&2 echo "Updating apps from gigalixir..."
gigalixir apps > "$GIG_APPS_FILE"
count=$(jq '. | length' < "$GIG_APPS_FILE")
>&2 echo "Found $count apps."
}
function update_apps_file_if_stale() {
if [[ ! -f "$GIG_APPS_FILE" ]]; then
update_apps_file
return
fi
# update if over 1 hour old
if [[ -z $(find "$GIG_APPS_FILE" -mmin -59) ]]; then
update_apps_file
fi
}
function current_app() {
if [[ -f "$GIG_SELECTED_APP_FILE" ]]; then
cat "$GIG_SELECTED_APP_FILE"
fi
}
function list_apps() {
jq -r '.[].unique_name' < "$GIG_APPS_FILE"
}
function choose_app() {
local selected_app
update_apps_file_if_stale
selected_app=$(list_apps | fzf -q "$1" -1)
if [[ -z "$selected_app" ]]; then
>&2 echo "App unchanged, using $(current_app)"
else
echo "$selected_app" > "$GIG_SELECTED_APP_FILE"
>&2 echo "Using $selected_app"
fi
}
function check_app_valid() {
local current
current="$(current_app)"
if [[ -z "$current" ]]; then
return 1
fi
list_apps | grep "$current"
}
function proxy_command_with_app() {
if [[ ! check_app_valid ]]; then
choose_app
fi
gigalixir "$1" -a "$(current_app)" $*[2,-1]
}
case "$1" in
# show the gig help
-h)
print_help
;;
# gig commands to view and change apps
current)
current_app
;;
use)
choose_app "${2-}"
;;
update)
update_apps_file
;;
# gigalixir commands that don't take an app
account*) ;&
apps) ;&
apps:create) ;&
help) ;&
login) ;&
logout) ;&
signup) ;&
version)
gigalixir $*
;;
# proxy to gigalixir with -a $current_app
*)
proxy_command_with_app $*
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment