install 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP=crush
  4. RED='\033[0;31m'
  5. GREEN='\033[0;32m'
  6. YELLOW='\033[1;33m'
  7. ORANGE='\033[38;2;255;140;0m'
  8. NC='\033[0m' # No Color
  9. requested_version=${VERSION:-}
  10. os=$(uname -s | tr '[:upper:]' '[:lower:]')
  11. if [[ "$os" == "darwin" ]]; then
  12. os="mac"
  13. fi
  14. arch=$(uname -m)
  15. if [[ "$arch" == "aarch64" ]]; then
  16. arch="arm64"
  17. fi
  18. filename="$APP-$os-$arch.tar.gz"
  19. case "$filename" in
  20. *"-linux-"*)
  21. [[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1
  22. ;;
  23. *"-mac-"*)
  24. [[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1
  25. ;;
  26. *)
  27. echo "${RED}Unsupported OS/Arch: $os/$arch${NC}"
  28. exit 1
  29. ;;
  30. esac
  31. INSTALL_DIR=$HOME/.crush/bin
  32. mkdir -p "$INSTALL_DIR"
  33. if [ -z "$requested_version" ]; then
  34. url="https://github.com/charmbracelet/crush/releases/latest/download/$filename"
  35. specific_version=$(curl -s https://api.github.com/repos/charmbracelet/crush/releases/latest | awk -F'"' '/"tag_name": "/ {gsub(/^v/, "", $4); print $4}')
  36. if [[ $? -ne 0 ]]; then
  37. echo "${RED}Failed to fetch version information${NC}"
  38. exit 1
  39. fi
  40. else
  41. url="https://github.com/charmbracelet/crush/releases/download/v${requested_version}/$filename"
  42. specific_version=$requested_version
  43. fi
  44. print_message() {
  45. local level=$1
  46. local message=$2
  47. local color=""
  48. case $level in
  49. info) color="${GREEN}" ;;
  50. warning) color="${YELLOW}" ;;
  51. error) color="${RED}" ;;
  52. esac
  53. echo -e "${color}${message}${NC}"
  54. }
  55. check_version() {
  56. if command -v crush >/dev/null 2>&1; then
  57. crush_path=$(which crush)
  58. ## TODO: check if version is installed
  59. # installed_version=$(crush version)
  60. installed_version="0.0.1"
  61. installed_version=$(echo $installed_version | awk '{print $2}')
  62. if [[ "$installed_version" != "$specific_version" ]]; then
  63. print_message info "Installed version: ${YELLOW}$installed_version."
  64. else
  65. print_message info "Version ${YELLOW}$specific_version${GREEN} already installed"
  66. exit 0
  67. fi
  68. fi
  69. }
  70. download_and_install() {
  71. print_message info "Downloading ${ORANGE}crush ${GREEN}version: ${YELLOW}$specific_version ${GREEN}..."
  72. mkdir -p crushtmp && cd crushtmp
  73. curl -# -L $url | tar xz
  74. mv crush $INSTALL_DIR
  75. cd .. && rm -rf crushtmp
  76. }
  77. check_version
  78. download_and_install
  79. add_to_path() {
  80. local config_file=$1
  81. local command=$2
  82. if [[ -w $config_file ]]; then
  83. echo -e "\n# crush" >> "$config_file"
  84. echo "$command" >> "$config_file"
  85. print_message info "Successfully added ${ORANGE}crush ${GREEN}to \$PATH in $config_file"
  86. else
  87. print_message warning "Manually add the directory to $config_file (or similar):"
  88. print_message info " $command"
  89. fi
  90. }
  91. XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
  92. current_shell=$(basename "$SHELL")
  93. case $current_shell in
  94. fish)
  95. config_files="$HOME/.config/fish/config.fish"
  96. ;;
  97. zsh)
  98. config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
  99. ;;
  100. bash)
  101. config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  102. ;;
  103. ash)
  104. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  105. ;;
  106. sh)
  107. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  108. ;;
  109. *)
  110. # Default case if none of the above matches
  111. config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  112. ;;
  113. esac
  114. config_file=""
  115. for file in $config_files; do
  116. if [[ -f $file ]]; then
  117. config_file=$file
  118. break
  119. fi
  120. done
  121. if [[ -z $config_file ]]; then
  122. print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
  123. exit 1
  124. fi
  125. if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
  126. case $current_shell in
  127. fish)
  128. add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
  129. ;;
  130. zsh)
  131. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  132. ;;
  133. bash)
  134. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  135. ;;
  136. ash)
  137. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  138. ;;
  139. sh)
  140. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  141. ;;
  142. *)
  143. print_message warning "Manually add the directory to $config_file (or similar):"
  144. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  145. ;;
  146. esac
  147. fi
  148. if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
  149. echo "$INSTALL_DIR" >> $GITHUB_PATH
  150. print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
  151. fi