-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinstall_gocmd.sh
More file actions
executable file
·145 lines (131 loc) · 4.57 KB
/
Copy pathinstall_gocmd.sh
File metadata and controls
executable file
·145 lines (131 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
#!/bin/bash
# Script to install gocommands
# --- Configuration ---
DEFAULT_GOCMD_VER=$(curl -L -s https://raw.githubusercontent.com/cyverse/gocommands/main/VERSION.txt)
INSTALL_DIR="/usr/local/bin" # Where to install the gocommands binary
BINARY_NAME="gocmd" # The name of the binary after install
USE_SUDO="true" # Use sudo for installation (can be overridden)
# --- Functions ---
# Function to download and extract into a temp directory
download_and_extract() {
local url="$1"
local dest="$2"
echo "Downloading and extracting from: $url"
curl -L -s "$url" | tar zxvf - -C "$dest"
}
# Function to display usage
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " --version <version> Specify the gocommands version to install (e.g., v1.2.3)"
echo " --no-sudo Install without using sudo"
echo " --install Install the binary"
echo " --help Display this help message"
exit 1
}
# --- Argument Parsing ---
GOCMD_VER="$DEFAULT_GOCMD_VER"
INSTALL="false" # Default to not installing
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
if [[ -z "$2" ]]; then
echo "Error: --version requires a value"
usage
fi
GOCMD_VER="$2"
shift # past argument
;;
--no-sudo)
USE_SUDO="false"
;;
--install)
INSTALL="true"
;;
--help)
usage
;;
*)
echo "Unknown parameter: $1"
usage
;;
esac
shift # past option
done
# Validate version (either fetched or user-supplied)
if [[ -z "$GOCMD_VER" ]]; then
echo "Error: Could not fetch latest version from GitHub. Use --version to specify a version."
exit 1
fi
# --- OS and Architecture Detection ---
OS=$(uname -s)
ARCH=$(uname -m)
case "$OS-$ARCH" in
Darwin-x86_64)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-darwin-amd64.tar.gz"
;;
Darwin-arm64)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-darwin-arm64.tar.gz"
;;
Darwin-aarch64)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-darwin-arm64.tar.gz"
;;
Linux-x86_64)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-linux-amd64.tar.gz"
;;
Linux-i386)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-linux-386.tar.gz"
;;
Linux-i686)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-linux-386.tar.gz"
;;
Linux-arm64)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-linux-arm64.tar.gz"
;;
Linux-aarch64)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-linux-arm64.tar.gz"
;;
Linux-arm)
URL="https://github.com/cyverse/gocommands/releases/download/${GOCMD_VER}/gocmd-${GOCMD_VER}-linux-arm.tar.gz"
;;
*)
echo "Unsupported OS/Architecture: $OS-$ARCH"
exit 1
;;
esac
# --- Download and Install ---
TMPDIR=$(mktemp -d)
trap "rm -rf '$TMPDIR'" EXIT
download_and_extract "$URL" "$TMPDIR"
if [[ ! -f "$TMPDIR/$BINARY_NAME" ]]; then
echo "Error: Binary '$BINARY_NAME' not found after extraction."
exit 1
fi
if [[ "$INSTALL" == "true" ]]; then
# After extraction, move the binary to the install location
BINARY_NAME_PATH="$TMPDIR/$BINARY_NAME"
install_binary() {
if [ ! -d "$INSTALL_DIR" ]; then
echo "Creating installation directory: $INSTALL_DIR"
if [[ "$USE_SUDO" == "true" ]]; then
sudo mkdir -p "$INSTALL_DIR"
else
mkdir -p "$INSTALL_DIR"
fi
fi
echo "Installing $BINARY_NAME to $INSTALL_DIR"
if [[ "$USE_SUDO" == "true" ]]; then
sudo mv "$BINARY_NAME_PATH" "$INSTALL_DIR/$BINARY_NAME"
sudo chmod +x "$INSTALL_DIR/$BINARY_NAME"
else
mv "$BINARY_NAME_PATH" "$INSTALL_DIR/$BINARY_NAME"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
fi
echo "$BINARY_NAME installed successfully to $INSTALL_DIR"
}
install_binary
else
mv "$TMPDIR/$BINARY_NAME" "./$BINARY_NAME"
chmod +x "./$BINARY_NAME"
echo "$BINARY_NAME downloaded successfully to $(pwd)/$BINARY_NAME"
fi