| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | GITHUB_REPO="https://github.com/stevedylandev/snippets-cli" |
| 5 | CLI_NAME="snip" |
| 6 | |
| 7 | INSTALL_DIR="$HOME/.local/share" |
| 8 | BIN_DIR="$INSTALL_DIR/snip" |
| 9 | |
| 10 | # Color codes for output |
| 11 | RED='\033[0;31m' |
| 12 | GREEN='\033[0;32m' |
| 13 | NC='\033[0m' # No Color |
| 14 | |
| 15 | error() { |
| 16 | echo -e "${RED}error:${NC} $*" >&2 |
| 17 | exit 1 |
| 18 | } |
| 19 | |
| 20 | success() { |
| 21 | echo -e "${GREEN}$*${NC}" |
| 22 | } |
| 23 | |
| 24 | # Detect platform |
| 25 | detect_platform() { |
| 26 | local platform=$(uname -s) |
| 27 | local arch=$(uname -m) |
| 28 | |
| 29 | case "$platform" in |
| 30 | "Darwin") |
| 31 | platform="Darwin" |
| 32 | ;; |
| 33 | "Linux") |
| 34 | platform="Linux" |
| 35 | ;; |
| 36 | MINGW*|MSYS*|CYGWIN*) |
| 37 | platform="Windows" |
| 38 | ;; |
| 39 | *) |
| 40 | error "Unsupported platform: $platform" |
| 41 | ;; |
| 42 | esac |
| 43 | |
| 44 | case "$arch" in |
| 45 | "x86_64"|"amd64") |
| 46 | arch="x86_64" |
| 47 | ;; |
| 48 | "arm64"|"aarch64") |
| 49 | arch="arm64" |
| 50 | ;; |
| 51 | "i386"|"i686") |
| 52 | arch="i386" |
| 53 | ;; |
| 54 | *) |
| 55 | error "Unsupported architecture: $arch" |
| 56 | ;; |
| 57 | esac |
| 58 | |
| 59 | echo "${platform}_${arch}" |
| 60 | } |
| 61 | |
| 62 | # Download and install the CLI |
| 63 | install_cli() { |
| 64 | local platform=$1 |
| 65 | local download_url="${GITHUB_REPO}/releases/latest/download/snippets-cli_${platform}.tar.gz" |
| 66 | local temp_dir=$(mktemp -d) |
| 67 | |
| 68 | echo "Downloading ${CLI_NAME}..." |
| 69 | curl -L "$download_url" -o "$temp_dir/${CLI_NAME}.tar.gz" || error "Failed to download ${CLI_NAME}" |
| 70 | |
| 71 | echo "Extracting ${CLI_NAME}..." |
| 72 | tar -xzf "$temp_dir/${CLI_NAME}.tar.gz" -C "$temp_dir" || error "Failed to extract ${CLI_NAME}" |
| 73 | |
| 74 | mkdir -p "$BIN_DIR" || error "Failed to create bin directory" |
| 75 | mv "$temp_dir/${CLI_NAME}" "$BIN_DIR/" || error "Failed to move ${CLI_NAME} to bin directory" |
| 76 | chmod +x "$BIN_DIR/${CLI_NAME}" || error "Failed to make ${CLI_NAME} executable" |
| 77 | |
| 78 | rm -rf "$temp_dir" |
| 79 | } |
| 80 | |
| 81 | # Update shell configuration |
| 82 | update_shell_config() { |
| 83 | local shell_config |
| 84 | case $SHELL in |
| 85 | */zsh) |
| 86 | shell_config="$HOME/.zshrc" |
| 87 | ;; |
| 88 | */bash) |
| 89 | shell_config="$HOME/.bashrc" |
| 90 | ;; |
| 91 | */fish) |
| 92 | shell_config="$HOME/.config/fish/config.fish" |
| 93 | ;; |
| 94 | *) |
| 95 | echo "Unsupported shell. Please add the following to your shell configuration:" |
| 96 | echo "export PATH=\"$BIN_DIR:\$PATH\"" |
| 97 | return |
| 98 | ;; |
| 99 | esac |
| 100 | |
| 101 | echo "Updating shell configuration..." |
| 102 | echo "export PATH=\"$BIN_DIR:\$PATH\"" >> "$shell_config" |
| 103 | echo "Shell configuration updated. Please restart your shell or run 'source $shell_config'" |
| 104 | } |
| 105 | |
| 106 | main() { |
| 107 | local platform=$(detect_platform) |
| 108 | install_cli "$platform" |
| 109 | update_shell_config |
| 110 | success "${CLI_NAME} has been successfully installed to $BIN_DIR/${CLI_NAME}" |
| 111 | echo "Run '${CLI_NAME} --help' to get started" |
| 112 | } |
| 113 | |
| 114 | main |