|
2 | 2 | # Setup script for CVE Scanner CLI (Linux only) |
3 | 3 | set -e |
4 | 4 |
|
5 | | -# Check for python3 |
6 | | -if ! command -v python3 >/dev/null 2>&1; then |
7 | | - echo "Python3 is required but not found. Please install Python3." |
8 | | - exit 1 |
9 | | -fi |
| 5 | +DOCKER_IMAGE="ghcr.io/debaa17/cve-scanner-cli:latest" |
| 6 | +DOCKER_ALIAS="alias cvecli='docker run --rm -it ${DOCKER_IMAGE}'" |
| 7 | +PATH_EXPORT='export PATH="$HOME/.local/bin:$PATH"' |
| 8 | +INSTALL_METHOD="" |
10 | 9 |
|
11 | | -# Create virtual environment if not exists |
12 | | -if [ ! -d "myenv" ]; then |
13 | | - python3 -m venv myenv |
14 | | -fi |
15 | | -source myenv/bin/activate |
| 10 | +resolve_rc_file() { |
| 11 | + local shell_name |
16 | 12 |
|
17 | | -# Upgrade pip and install requirements |
18 | | -pip install --upgrade pip |
19 | | -pip install -r requirements.txt |
| 13 | + shell_name="$(basename "${SHELL:-}")" |
20 | 14 |
|
21 | | -# Make CLI script executable |
22 | | -chmod +x cve_search_cli.py |
| 15 | + case "$shell_name" in |
| 16 | + bash) |
| 17 | + if [ -f "$HOME/.bashrc" ]; then |
| 18 | + printf '%s\n' "$HOME/.bashrc" |
| 19 | + else |
| 20 | + printf '%s\n' "$HOME/.profile" |
| 21 | + fi |
| 22 | + ;; |
| 23 | + zsh) |
| 24 | + printf '%s\n' "$HOME/.zshrc" |
| 25 | + ;; |
| 26 | + *) |
| 27 | + printf '%s\n' "$HOME/.profile" |
| 28 | + ;; |
| 29 | + esac |
| 30 | +} |
23 | 31 |
|
24 | | -# Optionally create a symlink in ~/.local/bin |
25 | | -mkdir -p "$HOME/.local/bin" |
26 | | -ln -sf "$PWD/cve_search_cli.py" "$HOME/.local/bin/cvecli" |
| 32 | +ensure_line_in_file() { |
| 33 | + local file_path="$1" |
| 34 | + local line="$2" |
27 | 35 |
|
28 | | -PATH_EXPORT='export PATH="$HOME/.local/bin:$PATH"' |
29 | | -SHELL_NAME="$(basename "${SHELL:-}")" |
30 | | -RC_FILE="" |
| 36 | + touch "$file_path" |
| 37 | + if ! grep -Fqx "$line" "$file_path"; then |
| 38 | + printf '\n%s\n' "$line" >> "$file_path" |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +remove_line_from_file() { |
| 43 | + local file_path="$1" |
| 44 | + local line="$2" |
| 45 | + local temp_file |
| 46 | + |
| 47 | + if [ ! -f "$file_path" ]; then |
| 48 | + return |
| 49 | + fi |
| 50 | + |
| 51 | + if ! grep -Fqx "$line" "$file_path"; then |
| 52 | + return |
| 53 | + fi |
| 54 | + |
| 55 | + temp_file="$(mktemp)" |
| 56 | + grep -Fvx "$line" "$file_path" > "$temp_file" || true |
| 57 | + mv "$temp_file" "$file_path" |
| 58 | +} |
| 59 | + |
| 60 | +print_reload_hint() { |
| 61 | + local rc_file="$1" |
| 62 | + |
| 63 | + printf "\033[0;33mOpen a new terminal or run: source %s\033[0m\n" "$rc_file" |
| 64 | +} |
| 65 | + |
| 66 | +install_docker() { |
| 67 | + local rc_file |
| 68 | + |
| 69 | + if ! command -v docker >/dev/null 2>&1; then |
| 70 | + printf "Docker is required for the recommended install method but was not found.\n" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + rc_file="$(resolve_rc_file)" |
| 75 | + docker pull "$DOCKER_IMAGE" |
| 76 | + ensure_line_in_file "$rc_file" "$DOCKER_ALIAS" |
| 77 | + |
| 78 | + printf "\033[0;32mDocker install complete. Added cvecli alias to %s\033[0m\n" "$rc_file" |
| 79 | + printf "\033[0;32mExample: cvecli --id CVE-2025-55184\033[0m\n" |
| 80 | + print_reload_hint "$rc_file" |
| 81 | +} |
| 82 | + |
| 83 | +install_local() { |
| 84 | + local rc_file |
| 85 | + |
| 86 | + if ! command -v python3 >/dev/null 2>&1; then |
| 87 | + printf "Python3 is required but not found. Please install Python3.\n" |
| 88 | + exit 1 |
| 89 | + fi |
31 | 90 |
|
32 | | -case "$SHELL_NAME" in |
33 | | - bash) |
34 | | - if [ -f "$HOME/.bashrc" ]; then |
35 | | - RC_FILE="$HOME/.bashrc" |
| 91 | + rc_file="$(resolve_rc_file)" |
| 92 | + |
| 93 | + if [ ! -d "myenv" ]; then |
| 94 | + python3 -m venv myenv |
| 95 | + fi |
| 96 | + source myenv/bin/activate |
| 97 | + |
| 98 | + pip install --upgrade pip |
| 99 | + pip install -r requirements.txt |
| 100 | + |
| 101 | + chmod +x cve_search_cli.py |
| 102 | + mkdir -p "$HOME/.local/bin" |
| 103 | + ln -sf "$PWD/cve_search_cli.py" "$HOME/.local/bin/cvecli" |
| 104 | + |
| 105 | + ensure_line_in_file "$rc_file" "$PATH_EXPORT" |
| 106 | + remove_line_from_file "$rc_file" "$DOCKER_ALIAS" |
| 107 | + |
| 108 | + printf "\033[0;32mLocal install complete. Use 'cvecli' from anywhere to run the tool.\033[0m\n" |
| 109 | + printf "\033[0;32mExample: cvecli --id CVE-2025-55184\033[0m\n" |
| 110 | + print_reload_hint "$rc_file" |
| 111 | +} |
| 112 | + |
| 113 | +choose_install_method() { |
| 114 | + printf "Choose installation method:\n" |
| 115 | + printf "1) Docker (recommended)\n" |
| 116 | + printf "2) Local machine\n" |
| 117 | + printf "Enter choice [1/2]: " |
| 118 | + read -r choice |
| 119 | + |
| 120 | + case "$choice" in |
| 121 | + 1|"") |
| 122 | + INSTALL_METHOD="docker" |
| 123 | + ;; |
| 124 | + 2) |
| 125 | + INSTALL_METHOD="local" |
| 126 | + ;; |
| 127 | + *) |
| 128 | + printf "Invalid choice. Use 1 for Docker or 2 for local machine.\n" |
| 129 | + exit 1 |
| 130 | + ;; |
| 131 | + esac |
| 132 | +} |
| 133 | + |
| 134 | +case "${1:-}" in |
| 135 | + --docker) |
| 136 | + INSTALL_METHOD="docker" |
| 137 | + ;; |
| 138 | + --local) |
| 139 | + INSTALL_METHOD="local" |
| 140 | + ;; |
| 141 | + "") |
| 142 | + if [ -t 0 ]; then |
| 143 | + choose_install_method |
36 | 144 | else |
37 | | - RC_FILE="$HOME/.profile" |
| 145 | + INSTALL_METHOD="docker" |
38 | 146 | fi |
39 | 147 | ;; |
40 | | - zsh) |
41 | | - RC_FILE="$HOME/.zshrc" |
42 | | - ;; |
43 | 148 | *) |
44 | | - if [ -f "$HOME/.profile" ]; then |
45 | | - RC_FILE="$HOME/.profile" |
46 | | - fi |
| 149 | + printf "Usage: bash setup.sh [--docker|--local]\n" |
| 150 | + exit 1 |
47 | 151 | ;; |
48 | 152 | esac |
49 | 153 |
|
50 | | -if [ -n "$RC_FILE" ]; then |
51 | | - touch "$RC_FILE" |
52 | | - if ! grep -Fqx "$PATH_EXPORT" "$RC_FILE"; then |
53 | | - printf '\n%s\n' "$PATH_EXPORT" >> "$RC_FILE" |
54 | | - printf "\033[0;32mAdded ~/.local/bin to PATH in %s\033[0m\n" "$RC_FILE" |
55 | | - printf "\033[0;33mOpen a new terminal or run: source %s\033[0m\n" "$RC_FILE" |
56 | | - fi |
57 | | -fi |
58 | | - |
59 | | -# Print green success message |
60 | | -printf "\033[0;32mSetup complete! Use 'cvecli' from anywhere to run the tool.\033[0m\n" |
61 | | -printf "\033[0;32mExample: cvecli --id CVE-2025-55184\033[0m\n" |
| 154 | +case "$INSTALL_METHOD" in |
| 155 | + docker) |
| 156 | + install_docker |
| 157 | + ;; |
| 158 | + local) |
| 159 | + install_local |
| 160 | + ;; |
| 161 | +esac |
0 commit comments