11#! /usr/bin/env bash
22
3- # Purpose : Script installs aah CLI into 'GOPATH/bin' based OS and ARCH.
4- # It should work on macOS, Linux, Unix systems and
5- # on Windows with msys and mingw.
3+ # Purpose : Script installs aah CLI into 'GOPATH/bin'.
4+ # It should work on macOS, Linux BSD systems and Windows with Cygwin.
65# Script License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
76# Creator : Jeevanandam M. (https://github.com/jeevatkm, [email protected] )8- # Requires : bash, mkdir, cp, rm, curl/wget, sed/awk, tr, type, unzip , mktemp
7+ # Requires : bash, mkdir, rm, curl/wget, sed/awk, tr, type, mktemp
98# Credits : This script inspired by Caddy server and Glide installer.
109#
1110# Version History
@@ -55,19 +54,44 @@ fetch() {
5554 eval " $1 ='$body '"
5655}
5756
58- download () {
59- local url=" $1 "
60- local filePath=" $2 "
61- if [ " $download_cmd " = " curl" ]; then
62- httpStatusCode=$( curl -s -w ' %{http_code}' -L " $url " -o " $filePath " )
63- elif [ " $download_cmd " = " wget" ]; then
64- body=$( wget --quiet --server-response --content-on-error -O " $filePath " " $url " )
65- httpStatusCode=$( cat $tmpFile | awk ' /^ HTTP/{print $2}' )
66- fi
67- echo " $httpStatusCode "
57+ quiet_git () {
58+ stdout=$( mktemp)
59+ stderr=$( mktemp)
60+ if ! git " $@ " < /dev/null > $stdout 2> $stderr ; then
61+ cat $stderr >&2
62+ rm -f $stdout $stderr
63+ exit 1
64+ fi
65+ rm -f $stdout $stderr
6866}
6967
70- cli_version=" $1 "
68+ quiet_go () {
69+ stdout=$( mktemp)
70+ stderr=$( mktemp)
71+ if ! go " $@ " < /dev/null > $stdout 2> $stderr ; then
72+ cat $stderr >&2
73+ rm -f $stdout $stderr
74+ exit 1
75+ fi
76+ rm -f $stdout $stderr
77+ }
78+
79+ # check prerequesties check
80+ if ! type -p git > /dev/null 2>&1 ; then
81+ quit " aah CLI Installation aborted, unable to find 'git'"
82+ fi
83+ if ! type -p go > /dev/null 2>&1 ; then
84+ quit " aah CLI Installation aborted, unable to find 'go'"
85+ fi
86+
87+ # Infer download command
88+ if type -p curl > /dev/null 2>&1 ; then
89+ download_cmd=" curl"
90+ elif type -p wget > /dev/null 2>&1 ; then
91+ download_cmd=" wget"
92+ else
93+ quit " Installation aborted, unable to find 'curl' or 'wget'"
94+ fi
7195
7296# Infer target arch
7397target_arch=$( uname -m)
@@ -86,12 +110,8 @@ case "$target_os" in
86110esac
87111
88112say " Starting aah CLI installer for $target_os /$target_arch "
113+ say " aah requires >= go.11"
89114
90- # Verify Go installation and Check GOPATH environment variable is set
91- go_cmd=$( which go)
92- if [ " $? " = " 1" ]; then
93- quit " Go is not installed, aah CLI requires Go. Please install it"
94- fi
95115go_path=$GOPATH
96116if [ -z " $go_path " ]; then
97117 go_path=$( go env GOPATH)
107127go_bin_dir=" $go_path /bin"
108128go_bin_dir=" ${go_bin_dir// \\ // } "
109129
110- # Infer download command
111- if type -p curl > /dev/null 2>&1 ; then
112- download_cmd=" curl"
113- elif type -p wget > /dev/null 2>&1 ; then
114- download_cmd=" wget"
115- else
116- quit " Installation aborted, unable to find 'curl' or 'wget'"
117- fi
130+ export GO111MODULE=on
118131
119- # Fetch latest version no and Download aah CLI binary
132+ cli_version=" $1 "
133+ # Fetch latest version no
120134if [ -z " $cli_version " ]; then
121- say " Fetch latest aah CLI version info from https://aahframework.org/version-cli"
135+ say " Fetch latest version info from https://aahframework.org/version-cli"
122136 fetch cli_version https://aahframework.org/version-cli
123- fi
124- say " Resolve aah CLI release artifact for $cli_version "
125- cli_release=aah-$cli_version -$target_os -$target_arch
126- cli_file_name=" $cli_release .zip"
127- cli_tmp_file=" /tmp/$cli_file_name "
128- cli_download_uri=" https://github.com/go-aah/tools/releases/download/$cli_version /$cli_file_name "
129- rm -rf $cli_tmp_file
130- say " Downloading $cli_download_uri "
131- status_code=$( download " $cli_download_uri " " $cli_tmp_file " )
132- if [ " $status_code " -ne 200 ]; then
133- say " Unable to find aah CLI release $cli_version for your system $target_os /$target_arch "
134- say " Note: aah CLI binary distribution available since CLI v0.13.0"
135- quit " Please create an issue here https://aahframework.org/issues to add support"
137+ else
138+ say " Use given version info $cli_version "
136139fi
137140
138- # Install the aah CLI into GOPATH/bin
139- cli_extract_tmp_dir= " /tmp/ $cli_release / "
140- rm -rf $cli_extract_tmp_dir
141- mkdir -p $cli_extract_tmp_dir
142- if type unzip > /dev/null 2>&1 ; then
143- unzip -q $cli_tmp_file -d $cli_extract_tmp_dir
144- else
145- quit " Installation aborted, unable to find 'unzip' program. Please install it "
141+ cli_import_path=aahframe.work/cli/aah
142+ cli_tmp_dir= $( mktemp -d )
143+ cli_tmp_dir= " $cli_tmp_dir /aah-cli "
144+
145+ say " Getting git tag $cli_version "
146+ quiet_git clone -b $cli_version --single-branch --depth 1 https://github.com/go-aah/tools.git $cli_tmp_dir
147+ if [ ! -d " $cli_tmp_dir /aah " ] ; then
148+ quit " It seems there was issue with fetching source code from github for tag $cli_version "
146149fi
147- say " Installing aah CLI into $go_bin_dir /"
148- rm -rf " $go_bin_dir /aah"
149- cp " $cli_extract_tmp_dir /aah" $go_bin_dir /
150- rm -rf $cli_tmp_file $cli_extract_tmp_dir
150+ cd $cli_tmp_dir /aah
151+ cli_git_commit_sha=$( git rev-parse HEAD)
152+ just_cli_version=${cli_version# " v" }
153+
154+ say " Build & Install aah CLI into $go_bin_dir /"
155+ quiet_go install -ldflags=" -s -w -X main.Version=$just_cli_version -X main.CliCommitID=$cli_git_commit_sha -X main.CliPackaged=true -X main.CliOS=$target_os -X main.CliArch=$target_arch " $cli_import_path
156+ rm -rf $cli_tmp_dir
151157say " Congrats, aah CLI installed successfully"
152- say " Running aah --version"
153- aah --version
0 commit comments