From 0c97d1f775d0620b60accf8fb62627cf08c3ac7d Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 26 Nov 2019 17:28:22 +0100 Subject: [PATCH] chg: dev: include color library and common element !minor Signed-off-by: Valentin Lab --- bin/compose | 189 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 170 insertions(+), 19 deletions(-) diff --git a/bin/compose b/bin/compose index 35aca98..f38e944 100755 --- a/bin/compose +++ b/bin/compose @@ -10,18 +10,166 @@ ## -get_os() { - local uname_output machine +ANSI_ESC=$'\e[' + +ansi_color() { + local choice="$1" + + if [ "$choice" == "tty" ]; then + if [ -t 1 ]; then + choice="yes" + else + choice="no" + fi + fi + if [ "$choice" != "no" ]; then + + SET_COL_CHAR="${ANSI_ESC}${COL_CHAR}G" + SET_COL_STATUS="${ANSI_ESC}${COL_STATUS}G" + SET_COL_INFO="${ANSI_ESC}${COL_INFO}G" + SET_COL_ELT="${ANSI_ESC}${COL_ELT}G" + + SET_BEGINCOL="${ANSI_ESC}0G" + + UP="${ANSI_ESC}1A" + DOWN="${ANSI_ESC}1B" + LEFT="${ANSI_ESC}1D" + RIGHT="${ANSI_ESC}1C" + + SAVE="${ANSI_ESC}7" + RESTORE="${ANSI_ESC}8" + + NORMAL="${ANSI_ESC}0m" + + GRAY="${ANSI_ESC}1;30m" + RED="${ANSI_ESC}1;31m" + GREEN="${ANSI_ESC}1;32m" + YELLOW="${ANSI_ESC}1;33m" + BLUE="${ANSI_ESC}1;34m" + PINK="${ANSI_ESC}1;35m" + CYAN="${ANSI_ESC}1;36m" + WHITE="${ANSI_ESC}1;37m" + + DARKGRAY="${ANSI_ESC}0;30m" + DARKRED="${ANSI_ESC}0;31m" + DARKGREEN="${ANSI_ESC}0;32m" + DARKYELLOW="${ANSI_ESC}0;33m" + DARKBLUE="${ANSI_ESC}0;34m" + DARKPINK="${ANSI_ESC}0;35m" + DARKCYAN="${ANSI_ESC}0;36m" + DARKWHITE="${ANSI_ESC}0;37m" + + SUCCESS=$GREEN + WARNING=$YELLOW + FAILURE=$RED + NOOP=$BLUE + ON=$SUCCESS + OFF=$FAILURE + ERROR=$FAILURE + + else + + SET_COL_CHAR= + SET_COL_STATUS= + SET_COL_INFO= + SET_COL_ELT= + + SET_BEGINCOL= + + NORMAL= + RED= + GREEN= + YELLOW= + BLUE= + GRAY= + WHITE= + + DARKGRAY= + DARKRED= + DARKGREEN= + DARKYELLOW= + DARKBLUE= + DARKPINK= + DARKCYAN= + + SUCCESS= + WARNING= + FAILURE= + NOOP= + ON= + OFF= + ERROR= + + fi + + ansi_color="$choice" + + export SET_COL_CHAR SET_COL_STATUS SET_COL_INFO SET_COL_ELT \ + SET_BEGINCOL UP DOWN LEFT RIGHT SAVE RESTORE NORMAL \ + GRAY RED GREEN YELLOW BLUE PINK CYAN WHITE DARKGRAY \ + DARKRED DARKGREEN DARKYELLOW DARKBLUE DARKPINK DARKCYAN \ + SUCCESS WARNING FAILURE NOOP ON OFF ERROR ansi_color +} + + +e() { printf "%s" "$*"; } + +warn() { e "${YELLOW}Warning:$NORMAL" "$*"$'\n' >&2 ; } +info() { e "${BLUE}II$NORMAL" "$*"$'\n' >&2 ; } +verb() { [ -z "$VERBOSE" ] || e "$*"$'\n' >&2; } +debug() { [ -z "$DEBUG" ] || e "$*"$'\n' >&2; } +err() { e "${RED}Error:$NORMAL $*"$'\n' >&2 ; } +die() { err "$@" ; exit 1; } + +ansi_color "${ansi_color:-tty}" + + +get_path() { ( + IFS=: + for d in $PATH; do + filename="$d/$1" + [ -f "$filename" -a -x "$filename" ] && { + echo "$d/$1" + return 0 + } + done + return 1 +) } + + +depends() { + ## Avoid colliding with variables that are created with depends. + local __i __path __new_name + + for __i in "$@"; do + if ! __path=$(get_path "$__i"); then + __new_name="$(echo "${__i//-/_}")" + if [ "$__new_name" != "$__i" ]; then + depends "$__new_name" + else + err "dependency check: couldn't find '$__i' required command." + exit 1 + fi + else + if ! test -z "$__path" ; then + export "$(echo "${__i//- /__}")"="$__path" + fi + fi + done +} + + +get_os() { + local uname_output uname_output="$(uname -s)" case "${uname_output}" in - Linux*) machine=linux;; - Darwin*) machine=mac;; - CYGWIN*) machine=cygwin;; - MINGW*) machine=mingw;; - *) machine="UNKNOWN:${uname_output}" + Linux*) e linux;; + Darwin*) e mac;; + CYGWIN*) e cygwin;; + MINGW*) e mingw;; + *) e "UNKNOWN:${uname_output}";; esac - echo "${machine}" } @@ -232,15 +380,15 @@ mk_docker_run_options() { filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX) { - printf "%s\0" "${docker_run_opts[@]}" + p0 "${docker_run_opts[@]}" } > "$filename" sha=$(sha256sum "$filename") sha=${sha:0:64} dest="$COMPOSE_VAR/sessions/$sha" { - printf "%s\0" "-v" "$dest:$dest" - printf "%s\0" "-e" "COMPOSE_LAUNCHER_OPTS=$dest" - printf "%s\0" "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN" + p0 "-v" "$dest:$dest" + p0 "-e" "COMPOSE_LAUNCHER_OPTS=$dest" + p0 "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN" } >> "$filename" mkdir -p "$COMPOSE_VAR"/sessions mv "$filename" "$dest" @@ -303,14 +451,17 @@ run() { fi done < <(cat "$COMPOSE_LAUNCHER_OPTS") - if [ -t 0 ]; then - docker_run_opts+=("-i") - fi - if [ -t 1 ]; then - docker_run_opts+=("-t") - fi + [ -t 0 ] && docker_run_opts+=("-i") + [ -t 1 ] && docker_run_opts+=("-t") exec docker run --rm "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@" } -run "$@" \ No newline at end of file +## +## Code +## + + +depends readlink + +run "$@"