|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# bit-cli-lookup - Query Bit CLI command reference |
| 4 | +# Usage: bit-cli-lookup <command-name> |
| 5 | +# bit-cli-lookup --list |
| 6 | +# bit-cli-lookup --update |
| 7 | +# |
| 8 | + |
| 9 | +set -e |
| 10 | + |
| 11 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | +CLI_REF="$SCRIPT_DIR/cli-reference.json" |
| 13 | +CLI_REF_URL="https://raw.githubusercontent.com/teambit/bit/master/scopes/harmony/cli-reference/cli-reference.json" |
| 14 | + |
| 15 | +# Colors (disabled if not a terminal) |
| 16 | +if [ -t 1 ]; then |
| 17 | + BOLD='\033[1m' |
| 18 | + DIM='\033[2m' |
| 19 | + CYAN='\033[36m' |
| 20 | + GREEN='\033[32m' |
| 21 | + YELLOW='\033[33m' |
| 22 | + RESET='\033[0m' |
| 23 | +else |
| 24 | + BOLD='' DIM='' CYAN='' GREEN='' YELLOW='' RESET='' |
| 25 | +fi |
| 26 | + |
| 27 | +usage() { |
| 28 | + echo "Usage: bit-cli-lookup <command-name>" |
| 29 | + echo " bit-cli-lookup --list List all commands" |
| 30 | + echo " bit-cli-lookup --update Update CLI reference from GitHub" |
| 31 | + echo "" |
| 32 | + echo "Examples:" |
| 33 | + echo " bit-cli-lookup snap" |
| 34 | + echo " bit-cli-lookup tag" |
| 35 | + echo " bit-cli-lookup lane" |
| 36 | + exit 1 |
| 37 | +} |
| 38 | + |
| 39 | +check_jq() { |
| 40 | + if ! command -v jq &> /dev/null; then |
| 41 | + echo "Error: jq is required but not installed." |
| 42 | + echo "Install jq using your system's package manager (e.g. 'brew install jq' on macOS or 'apt install jq' on Debian/Ubuntu)" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +check_cli_ref() { |
| 48 | + if [ ! -f "$CLI_REF" ]; then |
| 49 | + echo "CLI reference not found. Downloading..." |
| 50 | + update_cli_ref |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +update_cli_ref() { |
| 55 | + echo "Updating CLI reference from GitHub..." |
| 56 | + if curl -fsL "$CLI_REF_URL" -o "$CLI_REF.tmp"; then |
| 57 | + # Validate that the downloaded file is valid JSON before replacing the existing reference |
| 58 | + if jq empty "$CLI_REF.tmp" >/dev/null 2>&1; then |
| 59 | + mv "$CLI_REF.tmp" "$CLI_REF" |
| 60 | + echo "Updated successfully." |
| 61 | + else |
| 62 | + echo "Error: Downloaded CLI reference is not valid JSON." |
| 63 | + rm -f "$CLI_REF.tmp" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + else |
| 67 | + echo "Error: Failed to download CLI reference." |
| 68 | + rm -f "$CLI_REF.tmp" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +list_commands() { |
| 74 | + echo -e "${BOLD}Available Bit Commands:${RESET}\n" |
| 75 | + jq -r ' |
| 76 | + .[] | |
| 77 | + select(.private != true) | |
| 78 | + " \(.name)\(.alias | if . != "" then " (alias: \(.))" else "" end)" |
| 79 | + ' "$CLI_REF" | sort |
| 80 | +} |
| 81 | + |
| 82 | +format_command() { |
| 83 | + jq -r --arg cmd "$1" ' |
| 84 | + def format_cmd: |
| 85 | + . as $c | |
| 86 | + "\n\u001b[1m\u001b[36mCommand: bit \(.name)\u001b[0m" + |
| 87 | + (if .alias != "" then "\n\u001b[2mAlias: \(.alias)\u001b[0m" else "" end) + |
| 88 | + "\n\n\(.description)" + |
| 89 | + (if .extendedDescription != "" then "\n\n\(.extendedDescription)" else "" end) + |
| 90 | +
|
| 91 | + # Arguments |
| 92 | + (if (.arguments // []) | length > 0 then |
| 93 | + "\n\n\u001b[1mArguments:\u001b[0m\n" + |
| 94 | + ([.arguments[] | " \(.name): \(.description // "")"] | join("\n")) |
| 95 | + else "" end) + |
| 96 | +
|
| 97 | + # Options |
| 98 | + (if (.options // []) | length > 0 then |
| 99 | + "\n\n\u001b[1mOptions:\u001b[0m\n" + |
| 100 | + ([.options[] | |
| 101 | + " " + |
| 102 | + (if .[0] != "" then "-\(.[0]), " else " " end) + |
| 103 | + "--\(.[1])" + |
| 104 | + (if .[2] != "" then "\n \(.[2])" else "" end) |
| 105 | + ] | join("\n")) |
| 106 | + else "" end) + |
| 107 | +
|
| 108 | + # Examples |
| 109 | + (if (.examples // []) | length > 0 then |
| 110 | + "\n\n\u001b[1mExamples:\u001b[0m\n" + |
| 111 | + ([.examples[] | " bit \(.cmd)\n \(.description)"] | join("\n")) |
| 112 | + else "" end) + |
| 113 | +
|
| 114 | + # Subcommands |
| 115 | + (if (.commands // []) | length > 0 then |
| 116 | + "\n\n\u001b[1mSubcommands:\u001b[0m\n" + |
| 117 | + ([.commands[] | " \(.name): \(.description)"] | join("\n")) |
| 118 | + else "" end); |
| 119 | +
|
| 120 | + # Search for matching commands (main commands and subcommands) |
| 121 | + [ |
| 122 | + # Match main commands |
| 123 | + (.[] | select( |
| 124 | + (.name | ascii_downcase | split(" ")[0]) == ($cmd | ascii_downcase) or |
| 125 | + (.name | ascii_downcase | startswith($cmd | ascii_downcase)) or |
| 126 | + (.alias | ascii_downcase) == ($cmd | ascii_downcase) |
| 127 | + )), |
| 128 | + # Match subcommands |
| 129 | + (.[] | .commands // [] | .[] | select( |
| 130 | + (.name | ascii_downcase) == ($cmd | ascii_downcase) or |
| 131 | + (.name | ascii_downcase | startswith($cmd | ascii_downcase)) |
| 132 | + )) |
| 133 | + ] | |
| 134 | + if length == 0 then |
| 135 | + "No command found matching: \($cmd)\n\nTry: bit-cli-lookup --list" |
| 136 | + else |
| 137 | + .[] | format_cmd |
| 138 | + end |
| 139 | + ' "$CLI_REF" |
| 140 | +} |
| 141 | + |
| 142 | +# Main |
| 143 | +check_jq |
| 144 | + |
| 145 | +case "${1:-}" in |
| 146 | + "") |
| 147 | + usage |
| 148 | + ;; |
| 149 | + --help|-h) |
| 150 | + usage |
| 151 | + ;; |
| 152 | + --list|-l) |
| 153 | + check_cli_ref |
| 154 | + list_commands |
| 155 | + ;; |
| 156 | + --update|-u) |
| 157 | + update_cli_ref |
| 158 | + ;; |
| 159 | + *) |
| 160 | + check_cli_ref |
| 161 | + format_command "$1" |
| 162 | + ;; |
| 163 | +esac |
0 commit comments