Skip to content

Commit

Permalink
refactor: download, pre-remove, post-add scripts. Improved loggin… (#17)
Browse files Browse the repository at this point in the history
refactor: logging and progress bar on download
refactor: pull download out to own script
docs: explain structure to contributors
fix: shellcheck
feat: check dependencies, use pre-remove and post-add scripts
fix: deps check
fix: improve docs
fix: unbound array variable
  • Loading branch information
jthegedus committed Mar 22, 2020
1 parent e9395d3 commit 0d8f298
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Dependencies

- `bash`, `curl`, `tar`, `sort`, `python`
Are tracked in [lib/dependencies.txt](lib/dependencies.txt) and checked on installation of plugin and `asdf install gcloud *`. The core dependencies are: `bash`, `curl`, `python`, `sort`, `tar`.

# Install

Expand Down
29 changes: 29 additions & 0 deletions bin/download
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -euo pipefail

# shellcheck source=../lib/helpers.bash
source "$(dirname "$0")/../lib/helpers.bash"
# shellcheck source=../lib/utils.bash
source "$(dirname "$0")/../lib/utils.bash"

download() {
local gcs_bucket_name="cloud-sdk-release"
local gcs_object
local status_code
gcs_object="$(construct_gcs_object "${ASDF_INSTALL_VERSION}" "$(get_os_name)" "$(get_os_architecture)")"

log_info "⏬ downloading ${gcs_object}"
status_code=$(curl -X GET \
--write-out "%{http_code}" \
--progress-bar \
-Lo "${ASDF_DOWNLOAD_PATH}/${gcs_object}" \
"https://storage.googleapis.com/storage/v1/b/${gcs_bucket_name}/o/${gcs_object}?alt=media")

if [[ ${status_code} -eq 404 ]]; then
log_failure_and_exit "gcloud not found for version ${ASDF_INSTALL_VERSION}. Full version required, not just major version numbers."
fi
log_success "downloaded!"
}

download
6 changes: 4 additions & 2 deletions bin/exec-env
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

set -euo pipefail

# shellcheck source=../lib/utils.bash
source "$(dirname "$0")/../lib/utils.bash"

if [[ ! -x "$(command -v python)" ]]; then
printf "🚨 Python not found and is required for gcloud. Might I suggest https://github.com/danhper/asdf-python\\n"
exit 1
log_failure_and_exit "Python not found and is required for gcloud. Might I suggest https://github.com/danhper/asdf-python"
fi

# stolen from https://unix.stackexchange.com/a/56846/397902
Expand Down
63 changes: 25 additions & 38 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,45 @@

set -euo pipefail

# shellcheck source=../lib/helpers.bash
source "$(dirname "$0")/../lib/helpers.bash"
# shellcheck source=../lib/utils.bash
source "$(dirname "$0")/../lib/utils.bash"

download() {
local gcs_bucket_name="cloud-sdk-release"
local gcs_object="${1}"
local tmp_download_dir="${2}"

printf "⏬ downloading %s\\n" "${gcs_object}"
status_code=$(curl -X GET \
--write-out "%{http_code}" \
-Lo "${tmp_download_dir}/${gcs_object}" \
"https://storage.googleapis.com/storage/v1/b/${gcs_bucket_name}/o/${gcs_object}?alt=media")

if [[ ${status_code} -eq 404 ]]; then
printf "🚨 gcloud not found for version %s. Full version required, not just major version numbers.\\n" "${ASDF_INSTALL_VERSION}"
exit 1
fi
printf "✅ downloaded!\\n"
}

extract() {
local gcs_tar_location="${1}"

printf "📦 extracting...\\n"
tar -zxf "${gcs_tar_location}" -C "${ASDF_INSTALL_PATH}" --strip-components=1
printf "✅ extracted!\\n"
}

install_gcloud() {
printf "🚧 installing...\\n"
"${ASDF_INSTALL_PATH}/install.sh" --usage-reporting=false --path-update=false --quiet
printf "✅ gcloud %s installed!\\n" "${ASDF_INSTALL_VERSION}"
}

setup() {
tmp_download_dir=$(mktemp -d -t 'asdf_gcloud_XXXXXX')
trap 'rm -rf "${tmp_download_dir}"' EXIT

gcs_object="google-cloud-sdk-${ASDF_INSTALL_VERSION}-$(get_os_name)-$(get_os_architecture).tar.gz"
gcs_object="$(construct_gcs_object "${ASDF_INSTALL_VERSION}" "$(get_os_name)" "$(get_os_architecture)")"

download "${gcs_object}" "${tmp_download_dir}"
# if not asdf version with asdf_download_path then call download script here
if [ -z "${ASDF_DOWNLOAD_PATH:-}" ]; then
tmp_download_dir=$(mktemp -d -t 'asdf_gcloud_XXXXXX')
trap 'rm -rf "${tmp_download_dir}"' EXIT
log_info "ℹ️ run download script for older version of asdf"
export ASDF_DOWNLOAD_PATH="${tmp_download_dir}"

extract "${tmp_download_dir}/${gcs_object}"
# download
bash "$(dirname "$0")/download"
fi

install_gcloud
log_info "📦 extracting..."
tar -zxf "${ASDF_DOWNLOAD_PATH}/${gcs_object}" -C "${ASDF_INSTALL_PATH}" --strip-components=1
log_success "extracted!"

log_info "🚧 installing..."
"${ASDF_INSTALL_PATH}/install.sh" --usage-reporting=false --path-update=false --quiet
# test executable
test -x "${ASDF_INSTALL_PATH}/bin/gcloud" || log_failure_and_exit "Expected ${ASDF_INSTALL_PATH}/bin/gcloud to be executable."
log_success "gcloud ${ASDF_INSTALL_VERSION} installed!"

# TODO: instruct on setup of shell completions. This should be some form of ".asdf/installs/gcloud/$version/completions
}

if [ "${ASDF_INSTALL_TYPE}" != "version" ]; then
printf "🚨 Please provide the gcloud version number you wish to install.\\n"
log_failure_and_exit "Please provide the gcloud version number you wish to install."
fi

setup
check_dependencies "$(dirname "$0")/../lib/dependencies.txt" "failure"

install_gcloud
8 changes: 8 additions & 0 deletions bin/post-plugin-add
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -euo pipefail

# shellcheck source=../lib/utils.bash
source "$(dirname "$0")/../lib/utils.bash"

check_dependencies "$(dirname "$0")/../lib/dependencies.txt" "warning"
10 changes: 10 additions & 0 deletions bin/pre-plugin-remove
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -euo pipefail

# shellcheck source=../lib/utils.bash
source "$(dirname "$0")/../lib/utils.bash"

# Google Cloud SDK uninstall instructions - https://cloud.google.com/sdk/docs/uninstall-cloud-sdk

log_info "ℹ️ Your gcloud project configuration(s) persist in you gcloud configuration directory. Usually ~/.config/gcloud"
7 changes: 5 additions & 2 deletions bin/uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

set -euo pipefail

# shellcheck source=../lib/utils.bash
source "$(dirname "$0")/../lib/utils.bash"

# Google Cloud SDK uninstall instructions - https://cloud.google.com/sdk/docs/uninstall-cloud-sdk

printf "ℹ️ Your gcloud project configuration(s) persist in you gcloud configuration directory. Usually ~/.config/gcloud\\n"
log_info "ℹ️ Your gcloud project configuration(s) persist in you gcloud configuration directory. Usually ~/.config/gcloud"

rm -rf "$ASDF_INSTALL_PATH"
rm -rf "${ASDF_INSTALL_PATH}"
19 changes: 18 additions & 1 deletion contributing.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Contributing

Testing Locally:
## Structure

- `bin/*`: asdf plugin functions
- `lib/dependencies.txt`: list of dependencies required for this plugin to execute and install the tool as well as the tool's dependencies.
- `lib/helpers.bash`: helper functions that are asdf-gcloud specific
- `lib/utils.bash`: utility functions that are asdf-plugin agnostic

## Testing Locally

### Manual

```shell
asdf plugin remove gcloud
asdf plugin add gcloud .
asdf install gcloud 285.0.1
```

### asdf plugin tests

```shell
asdf plugin test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]
Expand Down
11 changes: 11 additions & 0 deletions lib/dependencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bash
curl
echo
getconf
printf
python
rm
source
sort
tar
uname
11 changes: 11 additions & 0 deletions lib/helpers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -euo pipefail

function construct_gcs_object() {
local install_version="${1}"
local os_name="${2}"
local os_architecture="${3}"

echo "google-cloud-sdk-${install_version}-${os_name}-${os_architecture}.tar.gz"
}
75 changes: 62 additions & 13 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,67 @@

set -euo pipefail

get_os_name() {
# START Logging
function log_failure_and_exit() {
printf "🚨 %s\\n" "${@}"
exit 1
}

function log_failure() {
printf "🚨 %s\\n" "${@}"
}

function log_info() {
printf "%s\\n" "${@}"
}

function log_success() {
printf "✅ %s\\n" "${@}"
}

function log_warning() {
printf "⚠️ %s\\n" "${@}"
}
# END Logging

function check_dependencies() {
local dependencies_file="${1}"
local failure_type="${2}" # should be "warning" or "failure"
declare -a missing_dependencies=()

# loop over file of line separated list of dependencies required by this tool
while IFS="" read -r p || [ -n "${p}" ]; do
if [ ! "$(command -v "${p}")" ]; then
missing_dependencies+=("${p}")
fi
done <"${dependencies_file}"

if [ "${#missing_dependencies[@]}" -ne 0 ]; then
if [ "${failure_type}" == "warning" ]; then
log_warning "Missing dependencies! These are hard requirements to install the Google Cloud SDK."
log_warning "${missing_dependencies[@]}"
log_info "You should install the listed dependencies before continuing."
else
log_failure "Missing dependencies! These are hard requirements to install the Google Cloud SDK."
log_failure "${missing_dependencies[@]}"
exit 1
fi
else
log_success "All dependencies found on system!"
fi
}

function get_os_architecture() {
local architecture
if [[ $(getconf LONG_BIT) == "64" ]]; then
architecture="x86_64"
else
architecture="x86"
fi
echo "${architecture}"
}

function get_os_name() {
local os_name
case $(uname -s) in
Linux*)
Expand All @@ -12,19 +72,8 @@ get_os_name() {
os_name="darwin"
;;
*)
printf "🚨 Script only supports macOS and Ubuntu\\n"
exit 1
log_failure_and_exit "Script only supports macOS and Ubuntu"
;;
esac
echo "${os_name}"
}

get_os_architecture() {
local arch
if [[ $(getconf LONG_BIT) == "64" ]]; then
arch="x86_64"
else
arch="x86"
fi
echo "${arch}"
}

0 comments on commit 0d8f298

Please sign in to comment.