| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #!/usr/bin/env bash
- set -euo pipefail
- APP=crush
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- ORANGE='\033[38;2;255;140;0m'
- NC='\033[0m' # No Color
- requested_version=${VERSION:-}
- os=$(uname -s | tr '[:upper:]' '[:lower:]')
- if [[ "$os" == "darwin" ]]; then
- os="mac"
- fi
- arch=$(uname -m)
- if [[ "$arch" == "aarch64" ]]; then
- arch="arm64"
- fi
- filename="$APP-$os-$arch.tar.gz"
- case "$filename" in
- *"-linux-"*)
- [[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1
- ;;
- *"-mac-"*)
- [[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1
- ;;
- *)
- echo "${RED}Unsupported OS/Arch: $os/$arch${NC}"
- exit 1
- ;;
- esac
- INSTALL_DIR=$HOME/.crush/bin
- mkdir -p "$INSTALL_DIR"
- if [ -z "$requested_version" ]; then
- url="https://github.com/charmbracelet/crush/releases/latest/download/$filename"
- specific_version=$(curl -s https://api.github.com/repos/charmbracelet/crush/releases/latest | awk -F'"' '/"tag_name": "/ {gsub(/^v/, "", $4); print $4}')
- if [[ $? -ne 0 ]]; then
- echo "${RED}Failed to fetch version information${NC}"
- exit 1
- fi
- else
- url="https://github.com/charmbracelet/crush/releases/download/v${requested_version}/$filename"
- specific_version=$requested_version
- fi
- print_message() {
- local level=$1
- local message=$2
- local color=""
- case $level in
- info) color="${GREEN}" ;;
- warning) color="${YELLOW}" ;;
- error) color="${RED}" ;;
- esac
- echo -e "${color}${message}${NC}"
- }
- check_version() {
- if command -v crush >/dev/null 2>&1; then
- crush_path=$(which crush)
- ## TODO: check if version is installed
- # installed_version=$(crush version)
- installed_version="0.0.1"
- installed_version=$(echo $installed_version | awk '{print $2}')
- if [[ "$installed_version" != "$specific_version" ]]; then
- print_message info "Installed version: ${YELLOW}$installed_version."
- else
- print_message info "Version ${YELLOW}$specific_version${GREEN} already installed"
- exit 0
- fi
- fi
- }
- download_and_install() {
- print_message info "Downloading ${ORANGE}crush ${GREEN}version: ${YELLOW}$specific_version ${GREEN}..."
- mkdir -p crushtmp && cd crushtmp
- curl -# -L $url | tar xz
- mv crush $INSTALL_DIR
- cd .. && rm -rf crushtmp
- }
- check_version
- download_and_install
- add_to_path() {
- local config_file=$1
- local command=$2
- if [[ -w $config_file ]]; then
- echo -e "\n# crush" >> "$config_file"
- echo "$command" >> "$config_file"
- print_message info "Successfully added ${ORANGE}crush ${GREEN}to \$PATH in $config_file"
- else
- print_message warning "Manually add the directory to $config_file (or similar):"
- print_message info " $command"
- fi
- }
- XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
- current_shell=$(basename "$SHELL")
- case $current_shell in
- fish)
- config_files="$HOME/.config/fish/config.fish"
- ;;
- zsh)
- config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
- ;;
- bash)
- config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
- ;;
- ash)
- config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
- ;;
- sh)
- config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
- ;;
- *)
- # Default case if none of the above matches
- config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
- ;;
- esac
- config_file=""
- for file in $config_files; do
- if [[ -f $file ]]; then
- config_file=$file
- break
- fi
- done
- if [[ -z $config_file ]]; then
- print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
- exit 1
- fi
- if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
- case $current_shell in
- fish)
- add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
- ;;
- zsh)
- add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
- ;;
- bash)
- add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
- ;;
- ash)
- add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
- ;;
- sh)
- add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
- ;;
- *)
- print_message warning "Manually add the directory to $config_file (or similar):"
- print_message info " export PATH=$INSTALL_DIR:\$PATH"
- ;;
- esac
- fi
- if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
- echo "$INSTALL_DIR" >> $GITHUB_PATH
- print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
- fi
|