-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinstall-unix.sh
More file actions
163 lines (134 loc) · 5.15 KB
/
install-unix.sh
File metadata and controls
163 lines (134 loc) · 5.15 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
#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# Define helper functions
# =============================================================================
text_bold() {
echo -e "\033[1m$1\033[0m"
}
text_title() {
echo ""
text_bold "$1"
if [ "$2" != "" ]; then echo "$2"; fi
}
text_title_error() {
echo ""
echo -e "\033[1;31m$1\033[00m"
}
# =============================================================================
# Define base variables
# =============================================================================
NAME="smbcloud-cli"
VERSION="0.3.26"
GITHUB_REPO="https://github.com/smbcloudXYZ/smbcloud-cli"
DOWNLOAD_BASE_URL="https://github.com/$GITHUB_REPO/releases/download/$VERSION"
if [ "$VERSION" == "latest" ]; then
# The latest version is accessible from a slightly different URL
DOWNLOAD_BASE_URL="https://github.com/$GITHUB_REPO/releases/latest/download"
fi
# =============================================================================
# Define binary list for supported OS & Arch
# - this is a map of "OS:Arch" -> "download binary name"
# - you can remove or add to this list as needed
# =============================================================================
declare -A BINARIES=(
["Linux:x86_64"]="smb"
["Darwin:x86_64"]="smb"
["Darwin:arm64"]="smb"
)
# =============================================================================
# Get the user's OS and Arch
# =============================================================================
OS="$(uname -s)"
ARCH="$(uname -m)"
SYSTEM="${OS}:${ARCH}"
# =============================================================================
# Match a binary to check if the system is supported
# =============================================================================
if [[ ! ${BINARIES["$SYSTEM"]+_} ]]; then
text_title_error "Error"
echo " Unsupported OS or arch: ${SYSTEM}"
echo ""
exit 1
fi
# =============================================================================
# Set the default installation variables
# =============================================================================
INSTALL_DIR="/usr/local/bin"
BINARY="${BINARIES["$SYSTEM"]}"
DOWNLOAD_URL="$DOWNLOAD_BASE_URL/$BINARY"
# =============================================================================
# Handle script arguments if passed
# -u: install to user bin directory
# -d <path>: specify installation directory
# =============================================================================
if [ $# -gt 0 ]; then
while getopts ":ud:" opt; do
case $opt in
u)
# Set default install dir based on OS
[ "$OS" == "Darwin" ] && INSTALL_DIR="$HOME/bin" || INSTALL_DIR="$HOME/.local/bin"
# Check that the user bin directory is in their PATH
IFS=':' read -ra PATHS <<< "$PATH"
INSTALL_DIR_IN_PATH="false"
for P in "${PATHS[@]}"; do
if [[ "$P" == "$INSTALL_DIR" ]]; then
INSTALL_DIR_IN_PATH="true"
fi
done
# If user bin directory doesn't exist or not in PATH, exit
if [ ! -d "$INSTALL_DIR" ] || [ "$INSTALL_DIR_IN_PATH" == "false" ]; then
text_title_error "Error"
echo " The user bin directory '$INSTALL_DIR' does not exist or is not in your environment PATH variable"
echo " To fix this error:"
echo " - Omit the '-u' option and to install system-wide"
echo " - Specify an installation directory with -d <path>"
echo ""
exit 1
fi
;;
d)
# Get absolute path in case a relative path is provided
INSTALL_DIR=$(cd "$OPTARG"; pwd)
if [ ! -d "$INSTALL_DIR" ]; then
text_title_error "Error"
echo " The installation directory '$INSTALL_DIR' does not exist or is not a directory"
echo ""
exit 1
fi
;;
\?)
text_title_error "Error"
echo " Invalid option: -$OPTARG" >&2
echo ""
exit 1
;;
:)
text_title_error "Error"
echo " Option -$OPTARG requires an argument." >&2
echo ""
exit 1
;;
esac
done
fi
# =============================================================================
# Create and change to temp directory
# =============================================================================
cd "$(mktemp -d)"
# =============================================================================
# Download binary
# =============================================================================
text_title "Downloading Binary" " $DOWNLOAD_URL"
curl -LO --proto '=https' --tlsv1.2 -sSf "$DOWNLOAD_URL"
# =============================================================================
# Make binary executable and move to install directory with appropriate name
# =============================================================================
text_title "Installing Binary" " $INSTALL_DIR/$NAME"
chmod +x "$BINARY"
mv "$BINARY" "$INSTALL_DIR/$NAME"
# =============================================================================
# Display post install message
# =============================================================================
text_title "Installation Complete" " Run $NAME --help for more information"
echo ""