Skip to content

Commit

Permalink
chore: use sh as shell interpreter instead of bash (#373)
Browse files Browse the repository at this point in the history
chore: use sh not bash
  • Loading branch information
healthjyk authored Jan 12, 2024
1 parent ca073fa commit 12040e1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 85 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
- uses: actions/checkout@v3
- name: check install script
run: static/scripts/install.sh
- name: check uninstall script
run: static/scripts/uninstall.sh
check-brew-on-macos:
runs-on: macos-latest
steps:
Expand All @@ -30,6 +32,8 @@ jobs:
- uses: actions/checkout@v3
- name: check install script
run: static/scripts/install.sh
- name: check uninstall script
run: static/scripts/uninstall.sh
check-brew-on-linux:
runs-on: ubuntu-latest
steps:
Expand Down
110 changes: 65 additions & 45 deletions static/scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh

# ------------------------------------------------------------
# Copyright 2022 The KusionStack Authors
Expand All @@ -17,14 +17,16 @@

set -o errexit
set -o nounset
set -o pipefail

# Sudo is required to copy binary to KUSION_HOME_DIR
USE_SUDO=${USE_SUDO:-"false"}

# Specified profile
PROFILE=${PROFILE:-""}

# SHELL
SHELL=${SHELL:-""}

# specifed Kusion version to install
KUSION_VERSION=${1:-""}

Expand Down Expand Up @@ -70,13 +72,13 @@ error() {
}

runAsRoot() {
local CMD="$*"
local CMD="$*"

if [ $EUID -ne 0 ] && [ $USE_SUDO = "true" ]; then
CMD="sudo $CMD"
fi
if [ $USE_SUDO = "true" ]; then
CMD="sudo $CMD"
fi

$CMD
$CMD
}

echoFexists() {
Expand All @@ -95,18 +97,18 @@ getSystemInfo() {
}

verifySupported() {
local supported=(darwin-amd64 linux-amd64 darwin-arm64)
local current_os_arch="${OS}-${ARCH}"

for osarch in "${supported[@]}"; do
if [ "$osarch" == "$current_os_arch" ]; then
info "Your system is ${OS}_${ARCH}."
return
fi
done

error "No prebuilt installation package for ${current_os_arch}"
exit 1
if [ "${current_os_arch}" = "darwin-amd64" ]; then
info "Your system is ${current_os_arch}."
elif [ "${current_os_arch}" = "darwin-arm64" ]; then
info "Your system is ${current_os_arch}."
elif [ "${current_os_arch}" = "linux-amd64" ]; then
info "Your system is ${current_os_arch}."
else
error "No prebuilt installation package for ${current_os_arch}"
exit 1
fi
}

checkHttpRequestCLI() {
Expand All @@ -121,25 +123,25 @@ checkHttpRequestCLI() {
}

toInstallVersion() {
if [ -z "$KUSION_VERSION" ]; then
info "Getting the latest Kusion version..."
getLatestReleaseVersion
else
echo "$KUSION_VERSION"
fi
if [ -z "$KUSION_VERSION" ]; then
info "Getting the latest Kusion version..."
getLatestReleaseVersion
else
echo "$KUSION_VERSION"
fi
}

getLatestReleaseVersion() {
local KusionReleaseURL="${RELEASE_URL}"
local latest_release=""
local KusionReleaseURL="${RELEASE_URL}"
local latest_release=""

if [ "$KUSION_HTTP_REQUEST_CLI" == "curl" ]; then
latest_release=$(runAsRoot curl -s $KusionReleaseURL | grep \"tag_name\" | awk '{print $2}' | sed -n 's/\"\(.*\)\",/\1/p' | grep -o '^v[0-9]\+\.[0-9]\+\.[0-9]\+$' | head -n 1)
else
latest_release=$(runAsRoot wget -q --header="Accept: application/json" -O - $KusionReleaseURL | grep \"tag_name\" | awk '{print $2}' | sed -n 's/\"\(.*\)\",/\1/p' | grep -o '^v[0-9]\+\.[0-9]\+\.[0-9]\+$' | head -n 1)
fi
if [ "$KUSION_HTTP_REQUEST_CLI" = "curl" ]; then
latest_release=$(runAsRoot curl -s $KusionReleaseURL | grep \"tag_name\" | awk '{print $2}' | sed -n 's/\"\(.*\)\",/\1/p' | awk '/^v[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}$/{print $0}' | head -1)
else
latest_release=$(runAsRoot wget -q --header="Accept: application/json" -O - $KusionReleaseURL | grep \"tag_name\" | awk '{print $2}' | sed -n 's/\"\(.*\)\",/\1/p' | awk '/^v[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}$/{print $0}' | head -1)
fi

echo "${latest_release:1}"
echo "$latest_release" | cut -c2-
}

download() {
Expand All @@ -153,7 +155,7 @@ download() {
ARTIFACT_TMP_FILE="$KUSION_TMP_ROOT/$KUSION_CLI_ARTIFACT"

info "Downloading installation package from $DOWNLOAD_URL..."
if [ "$KUSION_HTTP_REQUEST_CLI" == "curl" ]; then
if [ "$KUSION_HTTP_REQUEST_CLI" = "curl" ]; then
runAsRoot curl -SL "$DOWNLOAD_URL" -o "$ARTIFACT_TMP_FILE"
else
runAsRoot wget -O "$ARTIFACT_TMP_FILE" "$DOWNLOAD_URL"
Expand Down Expand Up @@ -187,9 +189,13 @@ install() {
fi

# detect profile
detected_profile=$(detectProfile "$(basename $SHELL)" "$(uname -s)")
local shell_name=""
if [ "$SHELL" ]; then
shell_name=$(basename $SHELL)
fi
detected_profile=$(detectProfile "$shell_name" "$(uname -s)")
if [ -z "${detected_profile-}" ]; then
error "No supported user profile found. Already tried \$PROFILE ($PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.profile, and ~/.config/fish/config.fish."
error "No supported user profile found. Already tried \$PROFILE, ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.profile, and ~/.config/fish/config.fish."
return 1
fi

Expand Down Expand Up @@ -257,25 +263,40 @@ detectProfile() {
*)
# Fall back to checking for profile file existence. Once again, the order
# differs between macOS and everything else.
local profiles
case $uname in
Darwin)
profiles=(.profile .bash_profile .bashrc .zshrc .config/fish/config.fish)
if [ -f "$HOME/.profile" ]; then
echo "$HOME/.profile"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
elif [ -f "$HOME/.config/fish/config.fish" ]; then
echo "$HOME/.config/fish/config.fish"
fi
;;
*)
profiles=(.profile .bashrc .bash_profile .zshrc .config/fish/config.fish)
if [ -f "$HOME/.profile" ]; then
echo "$HOME/.profile"
elif [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
elif [ -f "$HOME/.config/fish/config.fish" ]; then
echo "$HOME/.config/fish/config.fish"
fi
;;
esac

for profile in "${profiles[@]}"; do
echoFexists "$HOME/$profile" && break
done
;;
esac
}

buildEnvFile() {
local profile="$1"
local profile="$1"
local install_dir="$2"
local env_file_path="$3"

Expand All @@ -288,7 +309,7 @@ buildEnvContent() {
local profile="$1"
local install_dir="$2"

if [[ $profile =~ \.fish$ ]]; then
if [ $profile = "$HOME/.config/fish/config.fish" ]; then
# fish uses a little different syntax to modify the PATH
cat <<END_FISH_SCRIPT
set -gx KUSION_HOME "$install_dir"
Expand All @@ -308,7 +329,7 @@ END_BASH_SCRIPT
}

updateProfile() {
local profile="$1"
local profile="$1"
local env_file_path="$2"
local source_line_pattern="^source $env_file_path$"

Expand All @@ -334,7 +355,6 @@ END_SOURCE_SCRIPT
exit_trap() {
result=$?
if [ "$result" != "0" ]; then
# todo: update to install faq page when determined
error "Failed to install kusion. Please go to https://kusionstack.io for more support."
else
info "Install kusion into $KUSION_HOME_DIR succeeded:"
Expand Down
103 changes: 63 additions & 40 deletions static/scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh

# ------------------------------------------------------------
# Copyright 2022 The KusionStack Authors
Expand All @@ -15,11 +15,13 @@

set -o errexit
set -o nounset
set -o pipefail

# Sudo is required to delete binary from KUSION_HOME_DIR
USE_SUDO=${USE_SUDO:-"false"}

# SHELL
SHELL=${SHELL:-""}

# Specified profile
PROFILE=${PROFILE:-""}

Expand Down Expand Up @@ -49,7 +51,7 @@ error() {
runAsRoot() {
local CMD="$*"

if [ $EUID -ne 0 ] && [ $USE_SUDO = "true" ]; then
if [ $USE_SUDO = "true" ]; then
CMD="sudo $CMD"
fi

Expand All @@ -61,12 +63,12 @@ echoFexists() {
}

removeInstallationDir() {
info "Removing kusion installation dir $KUSION_HOME_DIR..."
runAsRoot rm -rf "$KUSION_HOME_DIR"
if [ -d "$KUSION_HOME_DIR" ]; then
error "Removing kusion installation dir failed."
return 1
fi
info "Removing kusion installation dir $KUSION_HOME_DIR..."
runAsRoot rm -rf "$KUSION_HOME_DIR"
if [ -d "$KUSION_HOME_DIR" ]; then
error "Removing kusion installation dir failed."
return 1
fi
}

clearProfileSource() {
Expand All @@ -75,13 +77,18 @@ clearProfileSource() {
return 0
fi

detected_profile=$(detectProfile "$(basename $SHELL)" "$(uname -s)")
if [ -z "${detected_profile-}" ]; then
warn "No supported user profile found. Already tried \$PROFILE ($PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.profile, and ~/.config/fish/config.fish. Skip clearing kusion env in user profile."
return 0
fi
# detect profile
local shell_name=""
if [ "$SHELL" ]; then
shell_name=$(basename $SHELL)
fi
detected_profile=$(detectProfile "$shell_name" "$(uname -s)")
if [ -z "${detected_profile-}" ]; then
warn "No supported user profile found. Already tried \$PROFILE ($PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.profile, and ~/.config/fish/config.fish. Skip clearing kusion env in user profile."
return 0
fi

info "Clearing kusion env in profile $detected_profile..."
info "Clearing kusion env in profile $detected_profile..."
deleteProfileSourceContent "$detected_profile"
}

Expand Down Expand Up @@ -120,48 +127,64 @@ detectProfile() {
*)
# Fall back to checking for profile file existence. Once again, the order
# differs between macOS and everything else.
local profiles
case $uname in
Darwin)
profiles=(.profile .bash_profile .bashrc .zshrc .config/fish/config.fish)
;;
*)
profiles=(.profile .bashrc .bash_profile .zshrc .config/fish/config.fish)
Darwin)
if [ -f "$HOME/.profile" ]; then
echo "$HOME/.profile"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
elif [ -f "$HOME/.config/fish/config.fish" ]; then
echo "$HOME/.config/fish/config.fish"
fi
;;
*)
if [ -f "$HOME/.profile" ]; then
echo "$HOME/.profile"
elif [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
elif [ -f "$HOME/.config/fish/config.fish" ]; then
echo "$HOME/.config/fish/config.fish"
fi
;;
esac

for profile in "${profiles[@]}"; do
echoFexists "$HOME/$profile" && break
done
;;
esac
}

deleteProfileSourceContent() {
local profile="$1"
local source_line_pattern="^$SOURCE_KUSION_CONTENT$"
local profile="$1"
local source_line_pattern="^$SOURCE_KUSION_CONTENT$"
local source_line_annotation_pattern="^$SOURCE_KUSION_ANNOTATION_CONTENT$"

if grep -qc "$source_line_pattern" "$profile"; then
line_number=$(grep -n "$source_line_pattern" "$profile" | cut -d: -f1)
sed -i '' ''"$line_number"'d' "$profile"
fi
if grep -q "$source_line_pattern" "$profile"; then
warn "Failed to delete $SOURCE_KUSION_CONTENT in $profile, please delete it manually."
else
info "Delete $SOURCE_KUSION_CONTENT in $profile succeeded."
fi
if grep -qc "$source_line_pattern" "$profile"; then
line_number=$(grep -n "$source_line_pattern" "$profile" | cut -d: -f1)
temp_file=$(mktemp)
sed ''"$line_number"'d' "$profile" > "$temp_file" && mv "$temp_file" "$profile"
fi
if grep -q "$source_line_pattern" "$profile"; then
warn "Failed to delete $SOURCE_KUSION_CONTENT in $profile, please delete it manually."
else
info "Delete $SOURCE_KUSION_CONTENT in $profile succeeded."
fi

if grep -q "$source_line_annotation_pattern" "$profile"; then
annotation_line_number=$(grep -n "$source_line_annotation_pattern" "$profile" | cut -d: -f1)
sed -i '' ''"$annotation_line_number"'d' "$profile"
fi
annotation_line_number=$(grep -n "$source_line_annotation_pattern" "$profile" | cut -d: -f1)
annotation_temp_file=$(mktemp)
sed ''"$annotation_line_number"'d' "$profile" > "$annotation_temp_file" && mv "$annotation_temp_file" "$profile"
fi
}

exit_trap() {
result=$?
if [ "$result" != "0" ]; then
# todo: update to install faq page when determined
error "Failed to uninstall kusion. Please go to https://kusionstack.io for more support."
else
info "Uninstall kusion succeeded. Hope you can use kusion again, visit https://kusionstack.io for more information."
Expand Down

0 comments on commit 12040e1

Please sign in to comment.