-
Notifications
You must be signed in to change notification settings - Fork 3
/
codeserver.sh
executable file
·336 lines (279 loc) · 6.22 KB
/
codeserver.sh
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
set -eu
###################################################################
#Script Name : codeserver.sh
#Description : Run the latest version of Code Server (Visual Studio Code) in the browser.
#Author : Renan Teixeira
#Email : [email protected]
#Version : 0.5 - 03/07/2020 - 21:50
###################################################################
#Global variables
GREEN="\e[92m"
RED="\e[31m"
END="\e[0m"
BLUE="\e[34m"
YELLOW="\e[33m"
echo_latest_version() {
version="$(curl -fsSLI -o /dev/null -w "%{url_effective}" https://github.com/cdr/code-server/releases/latest)"
version="${version#https://github.com/cdr/code-server/releases/tag/}"
version="${version#v}"
echo "$version"
}
VERSION="${VERSION-$(echo_latest_version)}"
CODE_PATH="$HOME/.config/code-server"
CONFIG_FILE=$CODE_PATH/config.yaml
CODE_FILE=$CODE_PATH/$VERSION/bin/code-server
main() {
OS="$(os)"
if [ ! "$OS" ]; then
echoerr "Unsupported OS $(uname)."
exit 1
fi
distro_name
ARCH="$(arch)"
if [ ! -d "$CODE_PATH" ]; then
mkdir $CODE_PATH
if [ ! -f $CODE_FILE ]; then
downloadCodeServer
else
runCodeServer
fi
else
clear
runCodeServer
fi
}
os() {
case "$(uname)" in
Linux)
echo linux
;;
Darwin)
echo macos
;;
FreeBSD)
echo freebsd
;;
esac
}
distro() {
if [ "$OS" = "macos" ] || [ "$OS" = "freebsd" ]; then
echo "$OS"
return
fi
if [ -f /etc/os-release ]; then
(
. /etc/os-release
case "$ID" in opensuse-*)
# opensuse's ID's look like opensuse-leap and opensuse-tumbleweed.
echo "opensuse"
return
;;
esac
echo "$ID"
)
return
fi
}
# os_name prints a pretty human readable name for the OS/Distro.
distro_name() {
if [ "$(uname)" = "Darwin" ]; then
echo "macOS v$(sw_vers -productVersion)"
return
fi
if [ -f /etc/os-release ]; then
(
. /etc/os-release
echo "$PRETTY_NAME"
)
return
fi
# Prints something like: Linux 4.19.0-9-amd64
uname -sr
}
arch() {
case "$(uname -m)" in
aarch64)
echo arm64
;;
x86_64)
echo amd64
;;
amd64) # FreeBSD.
echo amd64
;;
esac
}
downloadCodeServer(){
echoh "Downloading standalone release archive v$VERSION from GitHub releases."
echoh
cd $CODE_PATH
fetch "https://github.com/cdr/code-server/releases/download/v$VERSION/code-server-$VERSION-$OS-$ARCH.tar.gz" \
"$VERSION.tar.gz"
untarCodeServer
}
untarCodeServer(){
cd $CODE_PATH
mkdir $VERSION && tar xzf $VERSION.tar.gz -C $VERSION --strip-components 1
echo "Unpacking completed..."
rm -f $VERSION.tar.gz
configFile
}
runCodeServer(){
if [ -f $CODE_FILE ]; then
printf "${GREEN}"
printf "Code Server Version ${BLUE}$VERSION${END} ${GREEN}exists\n"
echo "Running..."
echo
printf "${END}"
configFile
else
downloadCodeServer
fi
}
aboutConfigFile(){
cath << EOF
The configuration file contains the variables that facilitate the parameterization of the Code Server according to its environment.
This file is located at $CONFIG_FILE if you need to modify it manually.
EOF
}
randomString() {
# Return random alpha-numeric string of given LENGTH
#
# Usage: VALUE=$(randomString $LENGTH)
# or: VALUE=$(randomString)
local DEFAULT_LENGTH=64
local LENGTH=${1:-$DEFAULT_LENGTH}
tr -dc A-Za-z0-9 </dev/urandom | head -c $LENGTH
# -dc: delete complementary set == delete all except given set
}
configFile(){
aboutConfigFile
if [ ! -f $CONFIG_FILE ]; then
printf "${RED}"
echo "Configuration file does not exist, let's create!"
printf "${END}"
newConfigFile
elif [ ! -s $CONFIG_FILE ]; then
printf "${RED}"
echo "Configuration file is empty!"
printf "${END}"
newConfigFile
else
printf "${YELLOW}"
echo "Use existing settings or create new ones?"
printf "${END}\n"
printf "${BLUE}"
cat $CONFIG_FILE
printf "${END}"
echo
echo "[1] Keep current"
echo "[2] Create again"
echo
echo -n "Chose an option: "
read optionConfig
case $optionConfig in
1) execCodeServer;;
2) newConfigFile;;
*) execCodeServer ;;
esac
fi
}
newConfigFile(){
rm -Rf $CONFIG_FILE
touch $CONFIG_FILE
DEFAULT_HOST=127.0.0.1:8080
#bind-addr
clear
aboutConfigFile
printf "${GREEN}"
echo "VS Code Access Address!"
printf "${END}"
echo
echo "[1] Use default host address - http://"$DEFAULT_HOST
echo "[2] Define custom host"
echo
echo -n "Chose an option: "
read optionHost
case $optionHost in
1) echo "bind-addr: "$DEFAULT_HOST >> $CONFIG_FILE;;
2) echo "Enter the host and port in the format: domain.com:8181" && read bindAddr && echo "bind-addr:" $bindAddr >> $CONFIG_FILE;;
*) newConfigFile ;;
esac
#auth
echo
printf "${GREEN}"
echo "Authentication mode!"
printf "${END}"
echo
echo "[1] Generate random password"
echo "[2] Define custom password"
echo "[3] No password"
echo
echo -n "Chose an option: "
read optionAuth
NEWPWD=$(randomString 15)
case $optionAuth in
1) echo "auth: password" >> $CONFIG_FILE && echo "password:" $NEWPWD >> $CONFIG_FILE;;
2) echo "Enter the custom password:" && customPasswd;;
3) echo "auth: none" >> $CONFIG_FILE;;
*) newConfigFile ;;
esac
execCodeServer
}
customPasswd(){
read -s authPass
if [ -z "${authPass}" ];then
echo "Password is required!"
customPasswd
else
echo "auth: password" >> $CONFIG_FILE
echo "password:" $authPass >> $CONFIG_FILE
fi
}
execCodeServer(){
clear
printf "\n${GREEN}"
if grep -q "password:" $CONFIG_FILE; then
cat $CONFIG_FILE | grep password:
else
echo "No password"
fi
printf "${END}\n"
$CODE_FILE
}
fetch() {
URL="$1"
FILE="$2"
if [ -e "$FILE" ]; then
echoh "+ Reusing $FILE"
return
fi
sh_c curl \
-#fL \
-o "$FILE.incomplete" \
-C - \
"$URL"
sh_c mv "$FILE.incomplete" "$FILE"
}
sh_c() {
echoh "+ $*"
if [ ! "${DRY_RUN-}" ]; then
sh -c "$*"
fi
}
echoh() {
echo "$@" | humanpath
}
cath() {
humanpath
}
echoerr() {
echoh "$@" >&2
}
# humanpath replaces all occurances of " $HOME" with " ~"
# and all occurances of '"$HOME' with the literal '"$HOME'.
humanpath() {
sed "s# $HOME# ~#g; s#\"$HOME#\"\$HOME#g"
}
main "$@"