octomation/go-service

View on GitHub
bin/activate

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash

set -o pipefail

deactivate() {
  unset _OS
  unset _ARCH
  unset _ROOT
  unset _GOBINPATH
  unset _NPMBINPATH

  if [[ -v _OLD_GO111MODULE ]]; then
    export GO111MODULE="${_OLD_GO111MODULE}"
    if [[ -z "${_OLD_GO111MODULE}" ]]; then
      unset GO111MODULE
    fi
    unset _OLD_GO111MODULE
  fi

  if [[ -v _OLD_GOBIN ]]; then
    export GOBIN="${_OLD_GOBIN}"
    if [[ -z "${_OLD_GOBIN}" ]]; then
      unset GOBIN
    fi
    unset _OLD_GOBIN
  fi

  if [[ -v _OLD_GOFLAGS ]]; then
    export GOFLAGS="${_OLD_GOFLAGS}"
    if [[ -z "${_OLD_GOFLAGS}" ]]; then
      unset GOFLAGS
    fi
    unset _OLD_GOFLAGS
  fi

  if [[ -v _OLD_GOPRIVATE ]]; then
    export GOPRIVATE="${_OLD_GOPRIVATE}"
    if [[ -z "${_OLD_GOPRIVATE}" ]]; then
      unset GOPRIVATE
    fi
    unset _OLD_GOPRIVATE
  fi

  if [[ -v _OLD_GOPROXY ]]; then
    export GOPROXY="${_OLD_GOPROXY}"
    if [[ -z "${_OLD_GOPROXY}" ]]; then
      unset GOPROXY
    fi
    unset _OLD_GOPROXY
  fi

  if [[ -v _OLD_PATH ]]; then
    export PATH="${_OLD_PATH}"
    unset _OLD_PATH
  fi

  if [[ -n "${_OLD_PS1:-}" ]]; then
    export PS1="${_OLD_PS1}"
    unset _OLD_PS1
  fi

  [[ -n "${BASH:-}" || -n "${ZSH_VERSION:-}" ]] && hash -r
  [[ "${1:-}" != "nondestructive" ]] && unset -f deactivate
}

deactivate nondestructive

_OLD_GO111MODULE="${GO111MODULE:-}"
_OLD_GOBIN="${GOBIN:-}"
_OLD_GOFLAGS="${GOFLAGS:-}"
_OLD_GOPRIVATE="${GOPRIVATE:-}"
_OLD_GOPROXY="${GOPROXY:-}"

_OS=$(uname -s | tr '[:upper:]' '[:lower:]')
_ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
_ROOT=$(dirname "$(dirname "$(realpath "${BASH_SOURCE[0]:-$0}")")")
_GOBINPATH="${_ROOT}/bin/${_OS}/${_ARCH}"
_NPMBINPATH="${_ROOT}/tools/node_modules/.bin"
_OLD_PATH="${PATH:-}"

export GO111MODULE=on
export GOBIN="${_GOBINPATH}"
export GOFLAGS=-mod=
export GOPRIVATE=go.octolab.net
export GOPROXY=direct
export PATH="${_NPMBINPATH}:${_GOBINPATH}:${PATH}"

_OLD_PS1="${PS1:-}"
# shellcheck disable=SC2050
if [[ "x(activated) " != x ]]; then
  PS1="(activated) ${PS1:-}"
elif [[ "$(basename "${_GOBINPATH}")" == "__" ]]; then
  PS1="[$(basename "$(dirname "${_GOBINPATH}")")] $PS1"
else
  PS1="($(basename "${_GOBINPATH}"))$PS1"
fi
export PS1

[[ -n "${BASH:-}" || -n "${ZSH_VERSION:-}" ]] && hash -r