github.com/acornpublishing/functional-programming-go@v0.0.0-20220401005601-c3bd3786d5a1/Chapter06/04_onion/init (about)

     1  #!/bin/bash
     2  # Author : Lex Sheehan
     3  # Purpose: This script initializes a go project with glide dependency management
     4  #
     5  # This script will:
     6  # 1) create a link to this project root directory in your MY_DEV_DIR directory
     7  # 2) verify that you are running the correct version of go
     8  # 3) verify that you have a toml config file (it will create one if you don't have one)
     9  # 4) verify that you have glide installed
    10  # 5) verify that you have a src directory (it will create one if you don't have one)
    11  # 6) create aliases for your convenience
    12  #
    13  # Notes:
    14  # 1) Source this file.  Run it like this:  source init
    15  # 2) Required dependency: https://github.com/Masterminds/glide
    16  # 3) Optional dependency: https://github.com/l3x/goenv
    17  # 4) Glide commands must be run real directory (not a linked one)
    18  #
    19  # License: MIT, 2017 Lex Sheehan LLC
    20  MY_DEV_DIR=~/dev
    21  CURRENT_GO_VERSION=1.9
    22  USES_TOML_CONFIG_YN=yes
    23  # ---------------------------------------------------------------
    24  # Verify variables above are correct.  Do not modify lines below.
    25  if [ -L "$(pwd)" ]; then
    26      echo "You must be in the real project directory to run this init script.  You are currently in a linked directory"
    27      echo "Running:  ln -l \"$(pwd)\""
    28      ls -l "$(pwd)"
    29      return
    30  fi
    31  CURRENT_GOVERSION="go$CURRENT_GO_VERSION"
    32  DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    33  DEV_DIR="$MY_DEV_DIR/$(basename $DIR)"
    34  set -x
    35  PROJECT_DIR_LINK="$MY_DEV_DIR/$(basename $DIR)"
    36  { set +x; } 2>/dev/null
    37  if [ -L "$PROJECT_DIR_LINK" ]; then
    38      rm "$PROJECT_DIR_LINK"
    39  fi
    40  if [ ! -d "$MY_DEV_DIR" ]; then
    41      mkdir "$MY_DEV_DIR"
    42  fi
    43  # Create link to project directory in MY_DEV_DIR
    44  set -x
    45  ln -s "$DIR" "$PROJECT_DIR_LINK"
    46  { set +x; } 2>/dev/null
    47  cd "$PROJECT_DIR_LINK"
    48  export GOPATH=$DIR
    49  APP_NAME=$(basename $(pwd))
    50  GOVERSION=$(go version)
    51  echo "Installed Go version: $GOVERSION"
    52  if [[ $(type goenv) ]]; then
    53      # Attempt to automatically set desired/current go version.  This requires goenv.
    54      . goenv "$CURRENT_GO_VERSION"
    55      if [ -z "$GOVERSION" ] || [[ "$(echo $GOVERSION | awk '{print $3}')" != "$CURRENT_GOVERSION" ]]; then
    56        echo "Expected Go version $CURRENT_GOVERSION to be installed"
    57        return
    58      fi
    59  else
    60      if [ -z "$GOVERSION" ] || [[ "$(echo $GOVERSION | awk '{print $3}')" != "$CURRENT_GOVERSION" ]]; then
    61          echo "Expected Go version $CURRENT_GOVERSION to be installed"
    62          return
    63      fi
    64  fi
    65  command -v glide >/dev/null 2>&1 || { echo >&2 "Missing glide.  For details, see: https://github.com/Masterminds/glide"; return; }
    66  if [ ! -e ./src ]; then
    67      mkdir src
    68  fi
    69  if [ "${PATH/$GOBIN}" == "$PATH" ] ; then
    70    export PATH=$PATH:$GOBIN
    71  fi
    72  if [[ "$USES_TOML_CONFIG_YN" =~ ^(yes|y)$ ]] && [ ! -e ./config.toml ]; then
    73      echo You were missing the config.toml configuration file... Creating bare config.toml file ...
    74      echo -e "# Runtime environment\napp_env = \"development\"" > config.toml
    75      ls -l config.toml
    76      alias go-run="go install && $APP_NAME -config ./config.toml"
    77  else
    78      alias go-run="go install && $APP_NAME"
    79  fi
    80  alias glide-ignore-project-dirs="printf \"ignore:\n$(find ./src -maxdepth 1 -type d | tail -n +2 | sed 's|./src\/||' | sed -e 's/^/- \.\//')\n\""
    81  alias mvglide='mkdir -p vendors && mv vendor/ vendors/src/ && export GOPATH=$(pwd):$(pwd)/vendors;echo "vendor packages have been moved to $(pwd)/vendors and your GOPATH: $GOPATH"'
    82  alias glide-update='if [ ! -z $(readlink `pwd`) ]; then export LINKED=true && pushd "$(readlink `pwd`)"; fi;rm -rf {vendor,vendors};rm glide.*;export GOPATH=$(pwd):$(pwd)/vendors && export GOBIN=$(pwd)/bin && glide init --non-interactive && glide-ignore-project-dirs >> glide.yaml && glide up && mvglide && if [ $LINKED==true ]; then popd;fi'
    83  alias prune-project='(rm -rf bin pkg vendors;rm glide.lock) 2>/dev/null'
    84  alias tree2='tree -C -d -L 2'
    85  alias find-imports='find . -type f -name "*.go" -exec grep -A3 "import" {} \; -exec echo {} \; -exec echo --- \;'
    86  echo You should only need to run this init script once.
    87  echo Add Go source code files under the src directory.
    88  echo After updating dependencies, i.e., adding a new import statement, run:  glide-update
    89  echo To build and run your app, run:  go-run