diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index dd2a64d..eda1492 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -99,6 +99,6 @@ jobs: steps: - uses: actions/checkout@v4 - shell: bash - run: | - bash scripts/install.sh - terrac --help + run: bash scripts/install.sh + - shell: bash + run: terrac --help diff --git a/README.md b/README.md index f98ce9c..c450130 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ You can simply download a binary and put it in a directoy included in the `PATH` ### bash -The following script automates the manual installation with a bash script in Linux and MacOS. `sudo` permission is required. +The following script automates the manual installation with a bash script in Linux and MacOS. ``` bash curl https://raw.githubusercontent.com/haoliangyu/terrac/main/scripts/install.sh | bash diff --git a/scripts/install.sh b/scripts/install.sh index 3b7d0e6..e3a9720 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,9 +1,22 @@ -# A bash scrip to automate the terrac installation - #!/bin/bash set -e +# Get the user's home directory +home_dir=$HOME + +# Determine the shell configuration file based on the user's shell +if [ -n "$BASH_VERSION" ]; then + # Bash shell + shell_rc=".bashrc" +elif [ -n "$ZSH_VERSION" ]; then + # Zsh shell + shell_rc=".zshrc" +else + echo "Unsupported shell. Please configure the PATH manually." + exit 1 +fi + # GitHub API URL for the latest release api_url="https://api.github.com/repos/haoliangyu/terrac/releases/latest" @@ -22,21 +35,23 @@ latest_release=$(echo "$response" | jq -r '.name') os=$(uname -s) # Check the operating system and print a message -if [ "$os" = "Darwin" ]; then - download_url="https://github.com/haoliangyu/terrac/releases/download/$latest_release/terrac-macos" - curl -o "terrac" -J -L -H "Accept: application/octet-stream" "$download_url" - sudo mv -f terrac /usr/local/bin/terrac - sudo chmod +x /usr/local/bin/terrac -elif [ "$os" = "Linux" ]; then - download_url="https://github.com/haoliangyu/terrac/releases/download/$latest_release/terrac-linux" - curl -o "terrac" -J -L -H "Accept: application/octet-stream" "$download_url" - sudo mv -f terrac /usr/local/bin/terrac - sudo chmod +x /usr/local/bin/terrac -# Windows support is not added yet -# elif [ "$os" = "MINGW64_NT-10.0" ]; then -# download_url="https://github.com/haoliangyu/terrac/releases/download/$latest_release/terrac-win" -# curl -o "terrac" -J -L -H "Accept: application/octet-stream" "$download_url" -# mv -f terrac /usr/local/bin/ +if [ "$os" = "Darwin" ] || [ "$os" = "Linux" ]; then + # Determine the download URL based on the operating system + case "$os" in + Darwin) + install_dir=$home_dir/bin + download_url="https://github.com/haoliangyu/terrac/releases/download/$latest_release/terrac-macos" + ;; + Linux) + install_dir=$home_dir/.local/bin + download_url="https://github.com/haoliangyu/terrac/releases/download/$latest_release/terrac-linux" + ;; + esac + + # Download the binary + mkdir $install_dir + curl -o "$install_dir/terrac" -J -L -H "Accept: application/octet-stream" "$download_url" + chmod +x "$install_dir/terrac" else echo "Unsupported operating system: $os" exit 1 @@ -44,3 +59,16 @@ fi echo '' echo 'Terrac is installed successfully. You can run "terrac --help" to verify.' + +# Check if ~/bin is already in PATH +if echo "$PATH" | grep -q "$install_dir"; then + echo "Path '$install_dir' is already in PATH." +else + # Add ~/bin to PATH and update shell configuration file + echo "export PATH=\"$install_dir:\$PATH\"" >> "$home_dir/$shell_rc" + echo "Added '$install_dir' to PATH in $shell_rc." + + # Apply the changes to the current shell session + source "$home_dir/$shell_rc" + echo "Changes applied to the current shell session." +fi