Skip to content

Commit f2d77bd

Browse files
committed
Updated install script with more configuration options
1 parent 6a98f1e commit f2d77bd

File tree

1 file changed

+95
-69
lines changed

1 file changed

+95
-69
lines changed

install.sh

Lines changed: 95 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,130 @@
11
#!/usr/bin/env bash
22

3-
4-
#
5-
# Show url after getting latest version
6-
# Improve output formatting
7-
#
8-
93
set -u
104
set -e
115

12-
echo
13-
echo "## Checking configuration and environment"
14-
echo
6+
PATH_INSTALL="/opt/coscale/cli"
7+
PATH_SYMLINK="/usr/bin/coscale-cli"
8+
SYMLINK="false"
159

16-
if [ ! -f "/opt/coscale/cli/api.conf" ]; then
17-
if [ -z ${COSCALE_APPID+x} ] || [ -z ${COSCALE_TOKEN+x} ]; then
18-
echo "### Configuration"
19-
echo
20-
# Check command arguments
21-
if [ -z ${COSCALE_APPID+x} ]; then
22-
echo "Please enter your app id:"
23-
read -e -r COSCALE_APPID
24-
fi
25-
echo
10+
# Check for unexisting variables
11+
if [ -z ${COSCALE_APPID+x} ]; then
12+
COSCALE_APPID=""
13+
fi
2614

27-
if [ -z ${COSCALE_TOKEN+x} ]; then
28-
echo "Please enter your access token:"
29-
read -e -r COSCALE_TOKEN
30-
fi
31-
echo
32-
fi
33-
else
34-
echo "Configuration detected in /opt/coscale/cli/api.conf"
35-
echo
15+
if [ -z ${COSCALE_TOKEN+x} ]; then
16+
COSCALE_TOKEN=""
3617
fi
3718

38-
# Detect operation system
39-
echo "### Detecting operating system"
40-
echo
41-
os=$(uname -o | awk '{split($0,a,"/"); print tolower(a[2])}')
42-
echo "Operation system: $os"
19+
ask_yesno() {
20+
local message=$1
21+
while true; do
22+
read -r -p "$message (yes/no)" yn
23+
case $yn in
24+
[Yy]* )
25+
break
26+
;;
27+
[Nn]* )
28+
break
29+
;;
30+
* )
31+
echo "Please answer yes or no."
32+
;;
33+
esac
34+
done
35+
36+
if echo "$yn" | grep -iq "^y" ;then
37+
return 0
38+
else
39+
return 1
40+
fi
41+
}
42+
43+
echo "----------------------------------------------"
44+
echo -e "\tCoScale CLI installation script"
45+
echo "----------------------------------------------"
46+
47+
read -i "$COSCALE_APPID" -p "Please enter your application id: " -e -r COSCALE_APPID
48+
49+
read -i "$COSCALE_TOKEN" -p "Please enter your access token: " -e -r COSCALE_TOKEN
50+
51+
read -i "$PATH_INSTALL" -p "Installation path: " -e -r PATH_INSTALL
52+
PATH_INSTALL=$(readlink -f $PATH_INSTALL)
53+
54+
if ask_yesno "Create a symlink [$PATH_SYMLINK]?"; then
55+
SYMLINK="true"
56+
fi
4357
echo
4458

45-
# Fetch latest release list from Github
59+
echo "----------------------------------------------"
60+
echo -e "\t Summary"
4661
echo
47-
echo "## Getting latest release information"
62+
echo -e "Application ID \t\t: $COSCALE_APPID"
63+
echo -e "Access token \t\t: $COSCALE_TOKEN"
4864
echo
49-
github_data=$(curl -s -L https://api.github.com/repos/CoScale/coscale-cli/releases/latest | grep "browser_download_url" | awk '{ print $2; }' | sed 's/"//g')
65+
echo -e "Installation path \t: $PATH_INSTALL"
66+
echo -e "Symlink \t\t: $SYMLINK"
67+
if [ "$SYMLINK" = true ] ; then
68+
echo -e "Symlink path \t\t: $PATH_SYMLINK"
69+
fi
70+
echo
71+
if ! ask_yesno "Is everything correct?"; then
72+
exit 1
73+
fi
5074

51-
# Select correct release
52-
release=`echo "$github_data" | grep -v ".exe"`
53-
echo "### Latest release: $release"
75+
echo "----------------------------------------------"
76+
echo -e "Starting installation"
5477

55-
# Start install
56-
echo
57-
echo "## Installing CoScale CLI tool"
78+
# Fetch latest release list from Github
5879
echo
80+
echo -e "- Getting latest release information"
81+
github_data=$(curl -s -L https://api.github.com/repos/CoScale/coscale-cli/releases/latest | grep "browser_download_url" | awk '{ print $2; }' | sed 's/"//g')
82+
release=$(echo "$github_data" | grep -v ".exe")
5983

6084
# Create dirs
61-
echo "### Creating directories /opt/coscale/cli"
62-
echo
63-
mkdir -v -p /opt/coscale/cli
64-
pushd /opt/coscale/cli
65-
echo
85+
if [ ! -d "$PATH_INSTALL" ]; then
86+
echo echo -e "- Creating directories $PATH_INSTALL/"
87+
mkdir -v -p "$PATH_INSTALL"
88+
echo
89+
fi
6690

6791
# Install client
68-
echo "### Downloading client to /opt/coscale/cli/coscale-cli"
69-
curl -L "$release" > coscale-cli
70-
chmod -v +x coscale-cli
92+
echo -e "- Downloading client to $PATH_INSTALL/coscale-cli"
93+
curl -L "$release" > "$PATH_INSTALL/coscale-cli"
94+
chmod -v +x "$PATH_INSTALL/coscale-cli"
7195
echo
7296

7397
# Create symlink from /usr/bin/coscale-cli to /opt/coscale/cli/coscale-cli
74-
echo "### Creating symlink"
75-
# Check if file exists
76-
if [ -f "/usr/bin/coscale-cli" ]; then
77-
# Check if symlink is correct
78-
if [ "$(readlink /usr/bin/coscale-cli)" = "/opt/coscale/cli/coscale-cli" ]; then
79-
echo "Correct symlink detected"
98+
if [ "$SYMLINK" = true ] ; then
99+
echo -e "- Creating symlink"
100+
# Check if file exists
101+
if [ -f "/usr/bin/coscale-cli" ]; then
102+
# Check if symlink is correct
103+
if [ "$(readlink /usr/bin/coscale-cli)" = "$PATH_INSTALL/coscale-cli" ]; then
104+
echo -e "\tExisting symlink detected"
105+
else
106+
echo -e "\tIncorrect symlink detected, please remove the file /usr/bin/coscale-cli and start again"
107+
exit 1
108+
fi
80109
else
81-
echo "Incorrect symlink detected, please the file /usr/bin/coscale-cli and start again"
82-
exit 1
110+
# Symlink does not exist, create
111+
ln -v -s "$PATH_INSTALL/coscale-cli" /usr/bin/coscale-cli
112+
echo -e "\tSymlink created"
83113
fi
84-
else
85-
# Symlink does not exist, create
86-
ln -v -s /opt/coscale/cli/coscale-cli /usr/bin/coscale-cli
114+
echo
87115
fi
88-
echo
89116

90117
# Create config
91-
if [ ! -f "/opt/coscale/cli/api.conf" ]; then
92-
echo "Generating config"
93-
echo "{\"baseurl\":\"https://api.coscale.com\", \"appid\":\"$COSCALE_APPID\", \"accesstoken\":\"$COSCALE_TOKEN\"}" | gzip -c > /opt/coscale/cli/api.conf
118+
if [ ! -f "$PATH_INSTALL/api.conf" ]; then
119+
echo -e "- Generating config file"
120+
echo "{\"baseurl\":\"https://api.coscale.com\", \"appid\":\"$COSCALE_APPID\", \"accesstoken\":\"$COSCALE_TOKEN\"}" | gzip -c > $PATH_INSTALL/api.conf
94121
echo
95122
fi
96123

97124
# Test config
98-
echo "Testing configuration"
99-
./coscale-cli check-config | sed -e 's/[{}]//g' | awk -F ":" '{print $2 }'
125+
echo -e "- Testing configuration"
126+
$PATH_INSTALL/coscale-cli check-config | sed -e 's/[{}]//g' | awk -F ":" '{print $2 }'
100127
echo
101128

102129
# Done
103-
echo "Done, you can now start using the CoScale CLI tool."
104-
popd
130+
echo -e "Done, you can now start using the CoScale CLI tool."

0 commit comments

Comments
 (0)