-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-go.sh
More file actions
executable file
·167 lines (151 loc) · 4.57 KB
/
install-go.sh
File metadata and controls
executable file
·167 lines (151 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# shellcheck disable=SC2016
set -e
VERSION=$(curl -s https://go.dev/dl/ | grep -oP '(?<=go)[0-9.]+(?=.linux-amd64.tar.gz)' | head -n 1)
[ -z "$GOROOT" ] && GOROOT="$HOME/.go"
[ -z "$GOPATH" ] && GOPATH="$HOME/go"
OS="$(uname -s)"
ARCH="$(uname -m)"
case $OS in
"Linux")
case $ARCH in
"x86_64")
ARCH=amd64
;;
"aarch64")
ARCH=arm64
;;
"armv6" | "armv7l")
ARCH=armv6l
;;
"armv8")
ARCH=arm64
;;
.*386.*)
ARCH=386
;;
esac
PLATFORM="linux-$ARCH"
;;
"Darwin")
case $ARCH in
"x86_64")
ARCH=amd64
;;
"arm64")
ARCH=arm64
;;
esac
PLATFORM="darwin-$ARCH"
;;
esac
print_help() {
echo "Usage: bash goinstall.sh OPTIONS"
echo -e "\nOPTIONS:"
echo -e " --remove\tRemove currently installed version"
echo -e " --version\tSpecify a version number to install"
}
if [ -z "$PLATFORM" ]; then
echo "Your operating system is not supported by the script."
exit 1
fi
if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
shell_profile="$HOME/.zshrc"
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then
shell_profile="$HOME/.bashrc"
elif [ -n "$($SHELL -c 'echo $FISH_VERSION')" ]; then
shell="fish"
if [ -d "$XDG_CONFIG_HOME" ]; then
shell_profile="$XDG_CONFIG_HOME/fish/config.fish"
else
shell_profile="$HOME/.config/fish/config.fish"
fi
fi
if [ "$1" == "--remove" ]; then
rm -rf "$GOROOT"
if [ "$OS" == "Darwin" ]; then
if [ "$shell" == "fish" ]; then
sed -i "" '/# GoLang/d' "$shell_profile"
sed -i "" '/set GOROOT/d' "$shell_profile"
sed -i "" '/set GOPATH/d' "$shell_profile"
sed -i "" '/set PATH $GOPATH\/bin $GOROOT\/bin $PATH/d' "$shell_profile"
else
sed -i "" '/# GoLang/d' "$shell_profile"
sed -i "" '/export GOROOT/d' "$shell_profile"
sed -i "" '/$GOROOT\/bin/d' "$shell_profile"
sed -i "" '/export GOPATH/d' "$shell_profile"
sed -i "" '/$GOPATH\/bin/d' "$shell_profile"
fi
else
if [ "$shell" == "fish" ]; then
sed -i '/# GoLang/d' "$shell_profile"
sed -i '/set GOROOT/d' "$shell_profile"
sed -i '/set GOPATH/d' "$shell_profile"
sed -i '/set PATH $GOPATH\/bin $GOROOT\/bin $PATH/d' "$shell_profile"
else
sed -i '/# GoLang/d' "$shell_profile"
sed -i '/export GOROOT/d' "$shell_profile"
sed -i '/$GOROOT\/bin/d' "$shell_profile"
sed -i '/export GOPATH/d' "$shell_profile"
sed -i '/$GOPATH\/bin/d' "$shell_profile"
fi
fi
echo "Go removed."
exit 0
elif [ "$1" == "--help" ]; then
print_help
exit 0
elif [ "$1" == "--version" ]; then
if [ -z "$2" ]; then # Check if --version has a second positional parameter
echo "Please provide a version number for: $1"
else
VERSION=$2
fi
elif [ ! -z "$1" ]; then
echo "Unrecognized option: $1"
exit 1
fi
if [ -d "$GOROOT" ]; then
echo "The Go install directory ($GOROOT) already exists. Exiting."
exit 1
fi
PACKAGE_NAME="go$VERSION.$PLATFORM.tar.gz"
TEMP_DIRECTORY=$(mktemp -d)
echo "Downloading $PACKAGE_NAME ..."
if hash wget 2>/dev/null; then
wget "https://go.dev/dl/$PACKAGE_NAME" -O "$TEMP_DIRECTORY/go.tar.gz"
else
curl -o "$TEMP_DIRECTORY/go.tar.gz" "https://go.dev/dl/$PACKAGE_NAME"
fi
if [ $? -ne 0 ]; then
echo "Download failed! Exiting."
exit 1
fi
echo "Extracting File..."
mkdir -p "$GOROOT"
tar -C "$GOROOT" --strip-components=1 -xzf "$TEMP_DIRECTORY/go.tar.gz"
echo "Configuring shell profile in: $shell_profile"
touch "$shell_profile"
if [ "$shell" == "fish" ]; then
{
echo '# GoLang'
echo "set GOROOT '${GOROOT}'"
echo "set GOPATH '$GOPATH'"
echo 'set PATH $GOPATH/bin $GOROOT/bin $PATH'
} >> "$shell_profile"
else
{
echo '# GoLang'
echo "export GOROOT=${GOROOT}"
echo 'export PATH=$GOROOT/bin:$PATH'
echo "export GOPATH=$GOPATH"
echo 'export PATH=$GOPATH/bin:$PATH'
} >> "$shell_profile"
fi
mkdir -p "${GOPATH}/"{src,pkg,bin}
echo -e "\nGo $VERSION was installed into $GOROOT.\n"
echo -e "\nApplying environment variables now..."
# Source shell profile to apply exports in current script session
source "$shell_profile"
echo "Environment variables loaded."
rm -f "$TEMP_DIRECTORY/go.tar.gz"