-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·199 lines (179 loc) · 6.38 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·199 lines (179 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
set -euo pipefail
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
project_dir="$(cd "$script_dir/.." && pwd)"
# Parse arguments
# Supported:
# -y -> skip API key prompt entirely
# --api-key <key> -> set OPENAI_API_KEY without prompting
# --api-key=<key> -> same as above
# --lms-port <port> -> set LM Studio base URL port without prompting
# --lms-port=<port> -> same as above
API_KEY_ARG=""
LMS_PORT_ARG=""
is_valid_port() {
local p="$1"
[[ "$p" =~ ^[0-9]+$ ]] || return 1
(( p >= 1 && p <= 65535 )) || return 1
return 0
}
set_profile_var() {
local var_name="$1"
local var_value="$2"
local comment="$3"
local escaped_value
escaped_value=$(printf "%s" "$var_value" | sed "s/'/'\"'\"'/g")
local var_line="export ${var_name}='${escaped_value}'"
touch "$target"
if grep -q "^export ${var_name}=" "$target" >/dev/null 2>&1; then
sed -i "s|^export ${var_name}=.*$|${var_line}|" "$target"
echo "Updated ${var_name} in $target"
else
printf "\n# %s\n%s\n" "$comment" "$var_line" >> "$target"
echo "Added ${var_name} to $target"
fi
if [ "$(id -u)" -eq 0 ] && [ "$target" = "/etc/profile.d/node-gpt-cli.sh" ]; then
chmod 644 "$target"
fi
}
if [ "$#" -gt 0 ]; then
while [ "$#" -gt 0 ]; do
case "$1" in
-y)
SKIP_API_PROMPT=1
shift
;;
--api-key=*)
API_KEY_ARG="${1#*=}"
shift
;;
--api-key)
shift
if [ "$#" -eq 0 ]; then
echo "Error: --api-key requires a value" >&2
exit 2
fi
API_KEY_ARG="$1"
shift
;;
--lms-port=*)
LMS_PORT_ARG="${1#*=}"
shift
;;
--lms-port)
shift
if [ "$#" -eq 0 ]; then
echo "Error: --lms-port requires a value" >&2
exit 2
fi
LMS_PORT_ARG="$1"
shift
;;
*)
# Ignore unknown args for now
shift
;;
esac
done
fi
if command -v npm >/dev/null 2>&1; then
echo "Running npm install in $project_dir"
(cd "$project_dir" && npm install --no-audit --no-fund)
else
echo "npm not found; skipping npm install" >&2
fi
cli_dir="$(cd "$script_dir/../cli" 2>/dev/null && pwd || true)"
if [ -z "$cli_dir" ] || [ ! -d "$cli_dir" ]; then
echo "CLI directory not found: $script_dir/../cli" >&2
exit 1
fi
# Ensure POSIX shim is executable
if [ -f "$cli_dir/gpt" ] && [ ! -x "$cli_dir/gpt" ]; then
chmod +x "$cli_dir/gpt"
echo "Marked $cli_dir/gpt as executable"
fi
export_line="export PATH=\"$cli_dir:\$PATH\""
if [ "$(id -u)" -eq 0 ]; then
target="/etc/profile.d/node-gpt-cli.sh"
# Ensure target exists and add PATH line if missing
touch "$target"
if grep -F -- "$cli_dir" "$target" >/dev/null 2>&1; then
echo "Path already present in $target"
else
printf "%s\n" "$export_line" >> "$target"
echo "Appended PATH export to $target"
fi
chmod 644 "$target"
else
if [ -f "$HOME/.profile" ]; then
target="$HOME/.profile"
else
target="$HOME/.bash_profile"
fi
if grep -F -- "$cli_dir" "$target" >/dev/null 2>&1; then
echo "Path already present in $target"
else
printf "\n# Add node-gpt-cli\n%s\n" "$export_line" >> "$target"
echo "Appended PATH export to $target"
fi
fi
# Optionally set OPENAI_API_KEY permanently
# Controls:
# - -y or env SKIP_API_PROMPT=1: skip prompting entirely (do not set key)
# - --api-key <key>: set key without prompting
if [ -n "${API_KEY_ARG:-}" ]; then
set_profile_var "OPENAI_API_KEY" "$API_KEY_ARG" "OpenAI API key for node-gpt-cli"
elif [ "${SKIP_API_PROMPT:-0}" = "1" ]; then
echo "Skipping OPENAI_API_KEY prompt (-y/SKIP_API_PROMPT provided)"
else
echo
prompt_msg="Enter OPENAI_API_KEY (leave blank or type -y to skip): "
# Prefer the controlling TTY so this works even when stdin is a pipe (e.g., wget ... | bash)
if [ -r "/dev/tty" ] && [ -w "/dev/tty" ]; then
printf "%s" "$prompt_msg" > /dev/tty
IFS= read -r OPENAI_API_KEY_INPUT < /dev/tty || OPENAI_API_KEY_INPUT=""
elif [ -t 0 ]; then
printf "%s" "$prompt_msg"
IFS= read -r OPENAI_API_KEY_INPUT || OPENAI_API_KEY_INPUT=""
else
echo "No interactive terminal available; skipping OPENAI_API_KEY configuration (pass -y to skip or set OPENAI_API_KEY environment)" >&2
OPENAI_API_KEY_INPUT=""
fi
if [ -n "${OPENAI_API_KEY_INPUT:-}" ] && [ "${OPENAI_API_KEY_INPUT}" != "-y" ]; then
set_profile_var "OPENAI_API_KEY" "$OPENAI_API_KEY_INPUT" "OpenAI API key for node-gpt-cli"
else
echo "Skipping OPENAI_API_KEY configuration"
fi
fi
# Optionally set LM_STUDIO_BASE_URL permanently
if [ -n "${LMS_PORT_ARG:-}" ]; then
if ! is_valid_port "$LMS_PORT_ARG"; then
echo "Error: --lms-port must be an integer between 1 and 65535" >&2
exit 2
fi
set_profile_var "LM_STUDIO_BASE_URL" "http://127.0.0.1:${LMS_PORT_ARG}/v1" "LM Studio base URL for node-gpt-cli"
elif [ "${SKIP_API_PROMPT:-0}" = "1" ]; then
echo "Skipping LM Studio port prompt (-y/SKIP_API_PROMPT provided)"
else
echo
prompt_msg="Enter LM Studio port (leave blank to skip, default server is 1234): "
if [ -r "/dev/tty" ] && [ -w "/dev/tty" ]; then
printf "%s" "$prompt_msg" > /dev/tty
IFS= read -r LMS_PORT_INPUT < /dev/tty || LMS_PORT_INPUT=""
elif [ -t 0 ]; then
printf "%s" "$prompt_msg"
IFS= read -r LMS_PORT_INPUT || LMS_PORT_INPUT=""
else
echo "No interactive terminal available; skipping LM Studio base URL configuration (pass --lms-port to set it non-interactively)" >&2
LMS_PORT_INPUT=""
fi
if [ -n "${LMS_PORT_INPUT:-}" ]; then
if ! is_valid_port "$LMS_PORT_INPUT"; then
echo "Invalid port. Skipping LM Studio configuration." >&2
else
set_profile_var "LM_STUDIO_BASE_URL" "http://127.0.0.1:${LMS_PORT_INPUT}/v1" "LM Studio base URL for node-gpt-cli"
fi
else
echo "Skipping LM Studio base URL configuration"
fi
fi