-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·184 lines (164 loc) · 6.13 KB
/
install.sh
File metadata and controls
executable file
·184 lines (164 loc) · 6.13 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
#!/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
API_KEY_ARG=""
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
;;
*)
# 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
# Sanitize single quotes for safe single-quoted export
sanitized_key=$(printf "%s" "$API_KEY_ARG" | sed "s/'/'\"'\"'/g")
key_line="export OPENAI_API_KEY='${sanitized_key}'"
# Ensure target is set from the PATH section above; then write/update key there
if [ -z "${target:-}" ]; then
# Fallback selection (should not happen, but be safe)
if [ "$(id -u)" -eq 0 ]; then
target="/etc/profile.d/node-gpt-cli.sh"
else
if [ -f "$HOME/.profile" ]; then
target="$HOME/.profile"
else
target="$HOME/.bash_profile"
fi
fi
fi
# Make sure the file exists before editing/appending
touch "$target"
if grep -q '^export OPENAI_API_KEY=' "$target" >/dev/null 2>&1; then
# Replace existing definition
sed -i "s|^export OPENAI_API_KEY=.*$|${key_line}|" "$target"
echo "Updated OPENAI_API_KEY in $target"
else
printf "\n# OpenAI API key for node-gpt-cli\n%s\n" "$key_line" >> "$target"
echo "Added OPENAI_API_KEY to $target"
fi
# Ensure readable permissions for profile.d when root
if [ "$(id -u)" -eq 0 ] && [ "$target" = "/etc/profile.d/node-gpt-cli.sh" ]; then
chmod 644 "$target"
fi
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
# Sanitize single quotes for safe single-quoted export
sanitized_key=$(printf "%s" "$OPENAI_API_KEY_INPUT" | sed "s/'/'\"'\"'/g")
key_line="export OPENAI_API_KEY='${sanitized_key}'"
# Ensure target is set from the PATH section above; then write/update key there
if [ -z "${target:-}" ]; then
# Fallback selection (should not happen, but be safe)
if [ "$(id -u)" -eq 0 ]; then
target="/etc/profile.d/node-gpt-cli.sh"
else
if [ -f "$HOME/.profile" ]; then
target="$HOME/.profile"
else
target="$HOME/.bash_profile"
fi
fi
fi
# Make sure the file exists before editing/appending
touch "$target"
if grep -q '^export OPENAI_API_KEY=' "$target" >/dev/null 2>&1; then
# Replace existing definition
sed -i "s|^export OPENAI_API_KEY=.*$|${key_line}|" "$target"
echo "Updated OPENAI_API_KEY in $target"
else
printf "\n# OpenAI API key for node-gpt-cli\n%s\n" "$key_line" >> "$target"
echo "Added OPENAI_API_KEY to $target"
fi
# Ensure readable permissions for profile.d when root
if [ "$(id -u)" -eq 0 ] && [ "$target" = "/etc/profile.d/node-gpt-cli.sh" ]; then
chmod 644 "$target"
fi
else
echo "Skipping OPENAI_API_KEY configuration"
fi
fi