Skip to content

Commit 9013c47

Browse files
authored
Fix installation script (#85)
Fixing installation script
1 parent 20c391e commit 9013c47

File tree

1 file changed

+36
-47
lines changed

1 file changed

+36
-47
lines changed

scripts/publish/installers/install.sh

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,37 @@ function error() {
1919
exit 10
2020
}
2121

22-
if [[ "$OSTYPE" == "linux"* ]]; then
22+
if [[ "${OSTYPE}" == "linux"* ]]; then
2323
OS=linux
24-
elif [[ "$OSTYPE" == "darwin"* ]]; then
24+
elif [[ "${OSTYPE}" == "darwin"* ]]; then
2525
OS=darwin
2626
else
27-
error "Unsupported operating system: $OSTYPE. Please checkout the CLI docs on docs.stackstate.com or contact StackState for support with your OS."
27+
error "Unsupported operating system: ${OSTYPE}. Please checkout the CLI docs on docs.stackstate.com or contact StackState for support with your OS."
2828
fi
2929
ARCH=`uname -m`
30-
if [[ "$ARCH" != "x86_64" && "$ARCH" != "arm64" && "$ARCH" != "aarch64" ]]; then
31-
error "Unsupported architecture: $ARCH. Please checkout the CLI docs on docs.stackstate.com or contact StackState for support with your OS."
30+
if [[ "${ARCH}" != "x86_64" && "${ARCH}" != "arm64" && "${ARCH}" != "aarch64" ]]; then
31+
error "Unsupported architecture: ${ARCH}. Please checkout the CLI docs on docs.stackstate.com or contact StackState for support with your OS."
3232
fi
3333

3434
# binaries are only published for arm64
35-
if [[ "$ARCH" == "aarch64" ]]; then
35+
if [[ "${ARCH}" == "aarch64" ]]; then
3636
ARCH="arm64"
3737
fi
3838

39-
# Check if custom location was defined
40-
if [[ -z "$STS_CLI_LOCATION" ]]; then
41-
# Use default installation location
42-
TARGET_CLI_PATH="$(pwd)"
43-
# check if the user has permissions to write on default location
44-
if [[ !-w "$TARGET_CLI_PATH" ]]; then
45-
# user has no writing permissions, exit because no explicit location given.
46-
echo "Cannot write to the current directory. Please either execute the script from a writeable directory or set STS_CLI_LOCATION to a different directory."
47-
exit 1
48-
fi
49-
else
50-
# Check if the custom installation location is valid
51-
if [[ ! -d "$STS_CLI_LOCATION" ]]; then
52-
error "Provided directory does not exist: $STS_CLI_LOCATION."
53-
# Check if the user has writing permissions on custom location
54-
elif [[ ! -w "$STS_CLI_LOCATION" ]]; then
55-
# Location exists but user doesn't have writing permission.
56-
echo "Sudo will be used on the provided location $STS_CLI_LOCATION."
57-
else
58-
# Location exists and user has writing permission
59-
NO_SUDO=true
60-
fi
61-
# Set installation location as defined by the input
62-
TARGET_CLI_PATH="$STS_CLI_LOCATION"
39+
TARGET_CLI_PATH="${STS_CLI_LOCATION:-/usr/local/bin}"
40+
41+
echo "Trying to install StackState CLI to ${TARGET_CLI_PATH}"
42+
43+
# Check if the destination directory exists
44+
if [[ ! -d "${TARGET_CLI_PATH}" ]]; then
45+
error "The directory to install cli to does not exist: ${TARGET_CLI_PATH}."
46+
fi
47+
48+
# Check if the user has writing permissions on the destination directory
49+
if [[ ! -w "${TARGET_CLI_PATH}" ]]; then
50+
# Destination directory exists but user doesn't have writing permission.
51+
echo "Sudo will be used on the provided destination directory ${TARGET_CLI_PATH}."
52+
SUDO_REQUIRED="true"
6353
fi
6454

6555
# Download and unpack the CLI to the target CLI path
@@ -68,32 +58,31 @@ if [[ -z "$STS_CLI_VERSION" ]]; then
6858
# The LATEST_VERSION file contains the published tag name, strip the v prefix
6959
STS_CLI_VERSION=${STS_CLI_VERSION#v}
7060
fi
71-
DL="https://dl.stackstate.com/stackstate-cli/v${STS_CLI_VERSION}/stackstate-cli-${STS_CLI_VERSION}.$OS-$ARCH.tar.gz"
61+
DL="https://dl.stackstate.com/stackstate-cli/v${STS_CLI_VERSION}/stackstate-cli-${STS_CLI_VERSION}.${OS}-${ARCH}.tar.gz"
7262
echo "Installing: $DL"
7363

74-
if [[ -z "$NO_SUDO" ]]; then
75-
# check if custom location was passed to avoid redundant printing
76-
if [[ -z "$STS_CLI_LOCATION" ]]; then
77-
echo "STS requires sudo permission to install."
78-
echo "Alternatively, you can provide a custom location with STS_CLI_LOCATION="
79-
echo "Make sure that the provided 'STS_CLI_LOCATION' is in your OS Path."
80-
fi
64+
if [[ "$SUDO_REQUIRED" == "true" ]]; then
65+
echo "STS requires sudo permission to install."
66+
echo "Alternatively, you can provide a custom destination directory with STS_CLI_LOCATION="
67+
echo "Make sure that the provided 'STS_CLI_LOCATION' is in your OS PATH."
8168

8269
# sudo password will be asked when executing the command.
83-
curl $DL | sudo tar xz --directory $TARGET_CLI_PATH
70+
curl $DL | sudo tar xz --directory ${TARGET_CLI_PATH}
8471
else
85-
curl $DL | tar xz --directory $TARGET_CLI_PATH
72+
curl $DL | tar xz --directory ${TARGET_CLI_PATH}
8673
fi
8774

8875
# Verify that 'sts' works
89-
sts > /dev/null 2>&1
90-
if [ $? -ne 0 ]; then
91-
error "Can not find 'sts' on the path or execute it"
92-
fi
76+
${TARGET_CLI_PATH}/sts > /dev/null 2>&1
9377

9478
# Configure the CLI if config parameters have been set
95-
if [[ -n "$STS_URL" && -n "$STS_API_TOKEN" ]]; then
96-
sts context save --url $STS_URL --api-token $STS_API_TOKEN
79+
if [[ -n "${STS_URL}" && -n "${STS_API_TOKEN}" ]]; then
80+
${TARGET_CLI_PATH}/sts context save --url ${STS_URL} --api-token ${STS_API_TOKEN}
9781
fi
9882

99-
printf "Success! Type ${GREEN}sts${NO_COLOR} to get started!\n"
83+
if [ "$(whereis -q sts)" == "" ]; then
84+
printf "${RED}[WARNING]${NO_COLOR} Can not find 'sts' on the PATH or execute it. Consider adding the directory to your PATH: PATH=\"\$PATH:${TARGET_CLI_PATH}\"\n"
85+
printf "Type ${GREEN}${TARGET_CLI_PATH}/sts${NO_COLOR} to get started!\n"
86+
else
87+
printf "Success! Type ${GREEN}sts${NO_COLOR} to get started!\n"
88+
fi

0 commit comments

Comments
 (0)