Created
November 23, 2018 22:55
-
-
Save mook-as/59ace15b55d9ed4ba810589d732645a8 to your computer and use it in GitHub Desktop.
fly CLI wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # vim: set et sw=4 ts=4 : | |
| set -o errexit | |
| set -o nounset | |
| args=() | |
| target="$(cat ~/.flyrc | y2j | jq -r '.default // ""')" | |
| url="" | |
| command="" | |
| while test "${#@}" -gt 0 ; do | |
| case "$1" in | |
| -t|--target) | |
| target="$2" | |
| shift 2 | |
| ;; | |
| --target=*) | |
| target="${1#*=}" | |
| shift | |
| ;; | |
| --concourse-url) | |
| url="${2}" | |
| args+=("$1" "$2") | |
| shift 2 | |
| ;; | |
| --concourse-url=*) | |
| url="${1#*=}" | |
| args+=("$1") | |
| shift 2 | |
| ;; | |
| *) | |
| if test -z "${command}" ; then | |
| command="${1}" | |
| fi | |
| args+=("$1") | |
| shift | |
| ;; | |
| esac | |
| done | |
| if test -n "${command}" ; then | |
| binary="fly.real" | |
| if test -n "${target}" ; then | |
| args=("-t" "${target}" "${args[@]:-}") | |
| if test -z "${url}" ; then | |
| url="$(fly.real targets | awk '$1 == "'"${target}"'" { print $2 }')" | |
| fi | |
| info="$(curl --silent "${url%/}/api/v1/info")" | |
| if echo "${info}" | jq -r .version >/dev/null 2>/dev/null ; then | |
| binary="fly.real.$(echo "${info}" | jq -r .version)" | |
| if ! command -v "${binary}" >/dev/null ; then | |
| # The binary is not available | |
| cliurl="${url}/api/v1/cli?platform=$(go env GOOS)&arch=$(go env GOARCH)" | |
| curl -o ~/bin/"${binary}" "${cliurl}" | |
| chmod a+x ~/bin/"${binary}" | |
| fi | |
| fi | |
| fi | |
| "${binary}" "${args[@]:-}" | |
| fi | |
| if test -n "${target}" -a '(' "${command}" = "login" -o -z "${command}" ')' ; then | |
| config="$(cat ~/.flyrc | y2j | jq ".default = \"${target}\"" | j2y)" | |
| echo "${config}" > ~/.flyrc | |
| echo "Saving target to ${target}" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment