From b4c834846bd0b2d5c3c04cfaa4f36c3110314ee0 Mon Sep 17 00:00:00 2001 From: William Beuil Date: Sat, 23 Jan 2021 15:15:22 +0100 Subject: [PATCH] First commit with help and version commands, start README and custom logger --- .editorconfig | 9 +++++++ README.md | 56 +++++++++++++++++++++++++++++++++++++++++ bin/dctlenv | 49 ++++++++++++++++++++++++++++++++++++ lib/log.sh | 45 +++++++++++++++++++++++++++++++++ lib/utils.sh | 4 +++ libexec/dctlenv | 44 ++++++++++++++++++++++++++++++++ libexec/dctlenv-help | 19 ++++++++++++++ libexec/dctlenv-root | 4 +++ libexec/dctlenv-version | 12 +++++++++ 9 files changed, 242 insertions(+) create mode 100644 .editorconfig create mode 100644 README.md create mode 100755 bin/dctlenv create mode 100755 lib/log.sh create mode 100755 lib/utils.sh create mode 100755 libexec/dctlenv create mode 100755 libexec/dctlenv-help create mode 100755 libexec/dctlenv-root create mode 100755 libexec/dctlenv-version diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..99580d0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee236d2 --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# dctlenv + +Version manager for [driftctl](https://github.com/cloudskiff/driftctl) inspired by [tfenv](https://github.com/tfutils/tfenv). + +## Installation + +### Basic GitHub Checkout + +This will get you going with the latest version of dctlenv and make it easy to fork and contribute any changes back upstream. + +**1. Check out dctlenv where you want** + +```shell +$ git clone https://github.com/wbeuil/dctlenv ~/.dctlenv +``` + +**2. Add `~/.dctlenv/bin` to your `$PATH` for access to the dctlenv CLI** + +- For **Bash**: + +```shell +$ echo 'export PATH="$HOME/.dctlenv/bin:$PATH"' >> ~/.bash_profile +``` + +- For **Zsh**: + +```shell +$ echo 'export PATH="$HOME/.dctlenv/bin:$PATH"' >> ~/.zshrc +``` + +- For **Fish**: + +```shell +$ set -Ux fish_user_paths $HOME/.dctlenv/bin $fish_user_paths +``` + +**3. Restart your shell so that PATH changes take effect** + +#### Upgrading With Git + +If you've installed dctlenv using the instructions above, you can upgrade to the latest version by pulling from GitHub. + +```shell +$ cd ~/.dctlenv +$ git pull +``` + +## Uninstalling dctlenv + +You will need to first remove the line from your shell startup configuration file. This will remove dctlenv shims directory from your `$PATH`. + +Then, you just need to remove its root directory: + +```shell +$ rm -rf `dctlenv root` +``` diff --git a/bin/dctlenv b/bin/dctlenv new file mode 100755 index 0000000..d800167 --- /dev/null +++ b/bin/dctlenv @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -uo pipefail + +panic() { + echo "panic: $*" >&2 + exit 1 +} + +if [ -z "${DCTLENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="$1" + local file_name + + while [ "$target_file" != "" ]; do + cd "$(dirname $target_file)" + file_name="$(basename "$target_file")" + target_file="$(readlink "$file_name")" + done + + echo "$(pwd -P)/$file_name" + } + + DCTLENV_ROOT="$(cd "$(dirname "$(readlink_f "$0")")/.." && pwd)" + [ -n $DCTLENV_ROOT ] || panic "Failed to set DCTLENV_ROOT" +else + DCTLENV_ROOT="${DCTLENV_ROOT%/}" +fi +export DCTLENV_ROOT + +if source "$DCTLENV_ROOT/lib/utils.sh"; then + log_debug "Utils from $DCTLENV_ROOT/lib/utils.sh sourced successfully" +else + panic "Failed to source utils from $DCTLENV_ROOT/lib/utils.sh" +fi + +for dir in libexec bin; do + case ":$PATH:" in + *:$DCTLENV_ROOT/$dir:*) + log_debug "\$PATH already contains '$DCTLENV_ROOT/$dir'" + ;; + *) + log_debug "Adding '$DCTLENV_ROOT/$dir' to \$PATH" + export PATH="$DCTLENV_ROOT/$dir:$PATH" + ;; + esac +done + +source "$DCTLENV_ROOT/libexec/dctlenv" diff --git a/lib/log.sh b/lib/log.sh new file mode 100755 index 0000000..7c2b3d4 --- /dev/null +++ b/lib/log.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -uo pipefail + +log() { + local text="$2" + + local level="$(echo "$1" | awk '{print toupper($0)}')" + + local date="$(date +"%Y-%m-%d %H:%M:%S")" + + local line="$date [$level] $text" + + case "$1" in + 'info' | 'debug' | 'warn') + if [ "${DCTLENV_DEBUG:-0}" -gt 0 ]; then + echo "$line" >&2 + fi + ;; + 'error') + if [ "${DCTLENV_DEBUG:-0}" -gt 0 ]; then + echo "$line" >&2 + fi + exit 1 + ;; + *) + log_error "Undefined log level: $*" + ;; + esac +} + +log_info() { + log "info" "$1" +} + +log_debug() { + log "debug" "$1" +} + +log_warn() { + log "warn" "$1" +} + +log_error() { + log "error" "$1" +} diff --git a/lib/utils.sh b/lib/utils.sh new file mode 100755 index 0000000..05ddbac --- /dev/null +++ b/lib/utils.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -uo pipefail + +source "$DCTLENV_ROOT/lib/log.sh" diff --git a/libexec/dctlenv b/libexec/dctlenv new file mode 100755 index 0000000..3b03846 --- /dev/null +++ b/libexec/dctlenv @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +set -uo pipefail + +abort() { + { + if [ "$#" -eq 0 ]; then + cat - + else + echo "dctlenv: $*" + echo "" + fi + } >&2 + dctlenv-help + exit 1 +} + +command="${1:-""}" + +log_info "CLI command args: \"$command\"" + +case "$command" in + "") + { + dctlenv-version + echo "" + dctlenv-help + } + exit 0 + ;; + -v | --version) + exec dctlenv-version + ;; + -h | --help) + exec dctlenv-help + ;; + *) + command_path="$(command -v "dctlenv-$command" || true)" + if [ -z "$command_path" ]; then + abort "unknown command \"$command\"" + fi + shift 1 + exec "$command_path" "$@" + ;; +esac diff --git a/libexec/dctlenv-help b/libexec/dctlenv-help new file mode 100755 index 0000000..a8b184c --- /dev/null +++ b/libexec/dctlenv-help @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -uo pipefail + +echo 'Usage: dctlenv [] + +Commands: + install Install a specific version of driftctl + uninstall Uninstall a specific version of driftctl + use Switch a version to use + list List all installed versions + list-remote List all installable versions + version Display dctlenv version + help Show this help output + +Flags: + -v, --version An alias for the "version" command + -h, --help An alias for the "help" command + +For full documentation, see: https://github.com/wbeuil/dctlenv#readme' diff --git a/libexec/dctlenv-root b/libexec/dctlenv-root new file mode 100755 index 0000000..3a39024 --- /dev/null +++ b/libexec/dctlenv-root @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -uo pipefail + +echo $DCTLENV_ROOT diff --git a/libexec/dctlenv-version b/libexec/dctlenv-version new file mode 100755 index 0000000..adfda46 --- /dev/null +++ b/libexec/dctlenv-version @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -uo pipefail + +version="0.0.2" +git_revision="" + +if cd "${BASH_SOURCE%/*}" 2>/dev/null && git remote -v 2>/dev/null | grep -q dctlenv; then + git_revision="$(git describe --tags HEAD 2>/dev/null || true)" + git_revision="${git_revision#v}" +fi + +echo "dctlenv ${git_revision:-$version}"