First commit with help and version commands, start README and custom logger

main v0.0.2
William Beuil 2021-01-23 15:15:22 +01:00
parent 9a0c38c996
commit b4c834846b
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
9 changed files with 242 additions and 0 deletions

9
.editorconfig Normal file
View File

@ -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

56
README.md Normal file
View File

@ -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`
```

49
bin/dctlenv Executable file
View File

@ -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"

45
lib/log.sh Executable file
View File

@ -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"
}

4
lib/utils.sh Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -uo pipefail
source "$DCTLENV_ROOT/lib/log.sh"

44
libexec/dctlenv Executable file
View File

@ -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

19
libexec/dctlenv-help Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -uo pipefail
echo 'Usage: dctlenv <command> [<options>]
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'

4
libexec/dctlenv-root Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -uo pipefail
echo $DCTLENV_ROOT

12
libexec/dctlenv-version Executable file
View File

@ -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}"