Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
325 lines (318 loc) · 16.1 KB
/
Copy pathinstall.sh
File metadata and controls
325 lines (318 loc) · 16.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/sh
# First-time setup from an extracted official macOS or Linux release archive.
set -eu
umask 077
fail() {
printf '%s\n' "$1" >&2
exit 1
}
package=false
require_gh=false
if [ "${1:-}" = --linux-package ]; then package=true; require_gh=true; shift; fi
if [ "${1:-}" = --require-gh ]; then require_gh=true; shift; fi
# Only the explicit Homebrew opt-in can install a missing verifier.
brew_bin=
if [ "${1:-}" = --homebrew ]; then
[ "$#" -eq 4 ] && [ "$3" = --zsh ] || fail 'Supply the Homebrew executable and zsh settings directory.'
case $2 in /*/brew) brew_bin=$2 ;; *) fail 'Supply the absolute path of the selected Homebrew executable.' ;; esac
require_gh=true
shift 2
fi
profile_dir=
profile_name=.zshrc
login_profile=
case $#:${1:-} in
0:) ;;
2:--bash) [ "$2" = "${HOME:-}" ] || fail 'Bash startup files must use the current HOME directory.'
profile_dir=$2; [ -n "$profile_dir" ] || fail 'Supply the directory containing .bashrc.'; profile_name=.bashrc ;;
2:--zsh) profile_dir=$2; [ -n "$profile_dir" ] || fail 'Supply the directory containing .zshrc.' ;;
*) fail 'Usage: install.sh [--require-gh] [--zsh DIRECTORY | --bash DIRECTORY]' ;;
esac
system=$(uname -s)
case $system in
Darwin|Linux) ;;
*) fail 'This installer supports macOS and Linux.' ;;
esac
if [ "$package" = true ]; then
[ "$system" = Linux ] && [ -n "$profile_dir" ] || fail 'Run the Linux package helper from Bash or zsh.'
fi
uid=$(id -u)
[ "$uid" != 0 ] || fail 'Run this installer as your ordinary user, without sudo.'
case ${HOME:-} in
/*) ;;
*) fail 'Set HOME to your absolute home directory.' ;;
esac
# Darwin ACLs can grant write access independently of the mode bits.
safe_acl() {
[ "$system" = Darwin ] || return 0
listing=$(LC_ALL=C /bin/ls -ldBne "$1") || return 1
[ -n "$listing" ] || return 1
printf '%s\n' "$listing" | LC_ALL=C /usr/bin/awk '
NR == 1 { has_acl = ($1 ~ /[+]$/); next }
$1 !~ /^[0-9]+:$/ || (NF != 4 && !(NF == 5 && $3 == "inherited")) { exit 1 }
$(NF-1) == "deny" { next }
$(NF-1) != "allow" { exit 1 }
{
count = split($NF, rights, ",")
for (i = 1; i <= count; i++)
if (rights[i] !~ /^(read|list|search|execute|readattr|readextattr|readsecurity|synchronize|file_inherit|directory_inherit|limit_inherit|only_inherit)$/)
exit 1
}
END { if (has_acl && NR == 1) exit 1 }
'
}
# find does not follow links. Check each directory before entering it.
safe_directory() {
[ -n "$(find "$1" -prune -type d -user "$uid" ! -perm -020 ! -perm -002 -print)" ] && safe_acl "$1"
}
CDPATH=
if [ "$package" = true ]; then
# The package manager owns this fixed path. Never select the source using PATH.
[ -n "$(find /usr/bin/openai -prune -type f -user 0 ! -perm -020 ! -perm -002 -size +0 -print)" ] ||
fail 'Reinstall the official Linux package to restore /usr/bin/openai.'
exec 3< /usr/bin/openai
else
cd -P "$(dirname "$0")"
safe_directory . || fail 'Extract the archive into a directory only you can modify.'
[ -n "$(find ./openai -prune -type f -user "$uid" -links 1 ! -perm -020 ! -perm -002 -size +0 -print)" ] && safe_acl ./openai ||
fail 'Extract a fresh archive in a private directory. Its openai executable must belong to you and reject writes from others.'
# Pin the checked file before leaving this directory. cp retains macOS quarantine.
exec 3< ./openai
fi
cd -P "$HOME"
safe_directory . || fail 'Your home directory must belong to you and reject writes from other users.'
# Keep setup inside checked directories. Capture the profile identity before installing.
profile_identity() (
name=$1
cd -P "$HOME" || exit 1
case $profile_dir in
"$HOME") remaining= ;;
"$HOME"/*) remaining=${profile_dir#"$HOME"/} ;;
*) fail 'For shell settings outside your home directory, add the PATH export yourself.' ;;
esac
while [ -n "$remaining" ]; do
part=${remaining%%/*}
case $part in ''|.|..) fail 'Supply a direct path to your shell settings directory.' ;; esac
safe_directory "./$part" || fail 'Your shell settings directory must reject symbolic links and writes from others.'
cd -P "./$part" || exit 1
case $remaining in */*) remaining=${remaining#*/} ;; *) remaining= ;; esac
done
# The device and inode refer to the checked directory and regular file.
# The installed CLI compares them to its open descriptors before appending.
if [ "$system" = Darwin ]; then set -- -f '%d:%i'; else set -- -c '%d:%i'; fi
dir_id=$(stat "$@" .) || exit 1
file_id=new
if [ -e "./$name" ] || [ -L "./$name" ]; then
file_id=$(stat "$@" "./$name") || exit 1
[ -n "$(find "./$name" -prune -type f -user "$uid" -links 1 ! -perm -020 ! -perm -002 -print)" ] &&
safe_acl "./$name" && [ -w "./$name" ] ||
fail "For a linked, shared, or read-only $name, add the PATH export yourself."
else
[ -w . ] || fail "Cannot create $name."
fi
printf '%s/%s\n' "$dir_id" "$file_id"
)
if [ -n "$profile_dir" ]; then
profile_id=$(profile_identity "$profile_name")
if [ "$profile_name" = .bashrc ]; then
# Bash reads the first existing login profile. Do not mask a lower-priority file.
for login_profile in .bash_profile .bash_login .profile; do
[ ! -e "$profile_dir/$login_profile" ] && [ ! -L "$profile_dir/$login_profile" ] || break
done
login_id=$(profile_identity "$login_profile")
elif [ -e "$profile_dir/.zlogin" ] || [ -L "$profile_dir/.zlogin" ]; then
# A login shell reads .zlogin after .zshrc. Never create this optional file.
login_profile=.zlogin
login_id=$(profile_identity "$login_profile" 2>/dev/null) || login_id=
fi
fi
for directory in .openai bin; do
if [ ! -d "$directory" ]; then
mkdir "$directory" 2>/dev/null || [ -d "$directory" ] || fail 'Cannot create the installation directory.'
fi
safe_directory "$directory" || fail 'The installation directories must belong to you and reject symlinks and writes from other users.'
cd -P "$directory"
done
clean_stage() {
rm -f "$1/openai" "$1/.archive-install" "$1/.setup"
rmdir "$1" 2>/dev/null || [ ! -d "$1" ]
}
existing=false
if [ -e openai ] || [ -L openai ]; then
# Recover a kill between link and unlink. Only remove our marked staging
# link to this exact regular file. Never remove the installed name.
if [ -f openai ] && [ ! -L openai ]; then
for previous in .openai-install.????????; do
[ -d "$previous" ] && safe_directory "$previous" || continue
[ -f "$previous/.archive-install" ] && [ ! -L "$previous/.archive-install" ] || continue
[ ! -L "$previous/openai" ] && [ "$previous/openai" -ef openai ] || continue
clean_stage "$previous"
done
fi
if [ -n "$profile_dir" ] &&
[ -n "$(find ./openai -prune -type f -user "$uid" -links 1 ! -perm -020 ! -perm -002 -print)" ] &&
[ -x ./openai ] && safe_acl ./openai; then
existing=true
else
fail 'An entry already exists at $HOME/.openai/bin/openai. Use its existing update method or select another installation.'
fi
fi
stage=$(mktemp -d .openai-install.XXXXXXXX)
trap 'clean_stage "$stage"' EXIT
trap 'exit 1' HUP INT TERM
: > "$stage/.archive-install"
cp /dev/fd/3 "$stage/openai"
exec 3<&-
chmod 700 "$stage/openai"
same_binary() {
if command -v cmp >/dev/null 2>&1; then cmp -s "$1" "$2"; return; fi
# Minimal Fedora installations include coreutils but may omit diffutils.
first=$(sha256sum < "$1") && second=$(sha256sum < "$2") && [ "$first" = "$second" ]
}
selected_cli=$(pwd -P)/$stage/openai
if [ "$package" = true ]; then selected_cli=/usr/bin/openai; fi
package_current() {
env -i HOME="$HOME" PATH=/usr/bin:/bin OPENAI_CLI_ARCHIVE_INSTALL=1 "$selected_cli" __openai_package_current
}
transaction=$package
if [ "$existing" = true ] && ! same_binary "$stage/openai" openai; then
if { [ "$package" = true ] || [ "$system" = Darwin ]; } && [ -n "$(package_current)" ]; then
transaction=true
else
fail 'A different executable already exists at $HOME/.openai/bin/openai. Keep its existing update method.'
fi
fi
version=$(env -i HOME="$HOME" PATH=/usr/bin:/bin "$stage/openai" --version) ||
fail 'This binary could not run. Download the archive for your operating system and processor.'
release=${version#"openai version "}
case $release in
''|*[!0-9.]*|.*|*.|*..*|*.*.*.*) fail 'The archive binary did not report an OpenAI CLI release version.' ;;
esac
case $version in
"openai version "*.*.*) ;;
*) fail 'The archive binary did not report an OpenAI CLI release version.' ;;
esac
if [ "$package" = true ]; then
stamp=$(env -i HOME="$HOME" PATH=/usr/bin:/bin OPENAI_CLI_ARCHIVE_INSTALL=1 "$stage/openai" __openai_linux_package_starter "$release")
[ "$stamp" = ready ] || fail 'The package executable lacks the official release identity required for automatic updates.'
fi
# Check the verifier that the documented PATH will select after installation.
check_verifier() {
env -i HOME="$HOME" PATH="$HOME/.openai/bin:$PATH" OPENAI_CLI_HOMEBREW_SETUP="${brew_bin:+1}" OPENAI_CLI_ARCHIVE_INSTALL=1 "$stage/openai" __openai_archive_verifier 2>/dev/null
}
if [ "$package" = true ]; then
# The explicit package opt-in can bootstrap a private, digest-pinned verifier.
# Exclude API credentials and arbitrary process-injection variables.
readiness=$(env -i HOME="$HOME" PATH="$HOME/.openai/bin:$PATH" TMPDIR="${TMPDIR:-/tmp}" \
HTTPS_PROXY="${HTTPS_PROXY:-}" https_proxy="${https_proxy:-}" HTTP_PROXY="${HTTP_PROXY:-}" http_proxy="${http_proxy:-}" \
NO_PROXY="${NO_PROXY:-}" no_proxy="${no_proxy:-}" SSL_CERT_FILE="${SSL_CERT_FILE:-}" SSL_CERT_DIR="${SSL_CERT_DIR:-}" \
OPENAI_CLI_ARCHIVE_INSTALL=1 "$stage/openai" __openai_setup_update_verifier) || readiness=unavailable
[ "$readiness" = ready ] || fail 'Retry the same package setup command after checking network access and your private installation directory.'
elif [ -n "$brew_bin" ]; then
prefix=$("$brew_bin" --prefix)
[ "$prefix/bin/brew" -ef "$brew_bin" ] || fail 'The selected Homebrew installation reported a different prefix.'
use_homebrew_verifier() {
env -i HOME="$HOME" PATH=/usr/bin:/bin OPENAI_CLI_ARCHIVE_INSTALL=1 "$stage/openai" __openai_homebrew_verifier "$prefix"
}
# Prefer Homebrew's protected formula before consulting the user's gh.
readiness=$(use_homebrew_verifier) || readiness=unavailable
[ "$readiness" != unsafe ] || fail 'The Homebrew verifier or private settings have unsafe ownership or permissions.'
if [ "$readiness" != ready ]; then readiness=$(check_verifier) || readiness=unavailable; fi
if [ "$readiness" != ready ]; then
printf '%s\n' 'Setting up GitHub CLI for update verification...'
"$brew_bin" install gh
readiness=$(use_homebrew_verifier) || readiness=unavailable
[ "$readiness" != unsafe ] || fail 'The Homebrew verifier or private settings have unsafe ownership or permissions.'
if [ "$readiness" != ready ]; then readiness=$(check_verifier) || readiness=unavailable; fi
[ "$readiness" = ready ] || fail 'Homebrew could not supply a compatible GitHub CLI verifier. Check the selected Homebrew installation, then retry setup.'
fi
else
readiness=$(check_verifier) || readiness=unavailable
fi
case $readiness in
ready) advice='GitHub CLI supports update verification.' ;;
missing)
if [ "$system" = Darwin ] && command -v brew >/dev/null 2>&1; then
advice='Install GitHub CLI with: brew install gh'
else
advice='Install GitHub CLI from https://cli.github.com/ and put gh on PATH.'
fi ;;
*) advice='Update GitHub CLI from https://cli.github.com/ and check which gh your PATH selects.' ;;
esac
if [ "$readiness" != ready ] && [ "$require_gh" = true ]; then
fail "Automatic update verification is unavailable. $advice Then rerun this installer."
fi
if [ "$transaction" = true ]; then
if [ "$system" = Darwin ]; then
# The transaction may rename the staged release into place. Keep its
# trusted profile helper available even if a newer installation wins.
cp "$stage/openai" "$stage/.setup"
selected_cli=$(pwd -P)/$stage/.setup
fi
active=$(env -i HOME="$HOME" PATH=/usr/bin:/bin OPENAI_CLI_ARCHIVE_INSTALL=1 "$stage/openai" __openai_package_install)
[ -n "$active" ] || fail 'Could not safely enroll the package. Check for a different executable or an update in progress, then retry.'
elif [ "$existing" = false ]; then
# Record the default before the executable becomes visible to another shell.
initialized=$(env -i HOME="$HOME" PATH=/usr/bin:/bin OPENAI_CLI_ARCHIVE_INSTALL=1 \
"$stage/openai" __openai_initialize_update_mode) || initialized=
[ "$initialized" = ready ] || fail 'Could not prepare manual updates for the new installation. Check the installation directory and retry.'
ln "$stage/openai" . || fail 'Could not install openai. Check whether another installation already exists.'
fi
check_destination() {
if [ "$transaction" = true ]; then
[ "$(package_current)" = "$active" ]
elif [ "$existing" = false ]; then
[ ! -L openai ] && [ openai -ef "$stage/openai" ]
else
[ -n "$(find ./openai -prune -type f -user "$uid" -links 1 ! -perm -020 ! -perm -002 -print)" ] &&
safe_acl ./openai && same_binary "$stage/openai" ./openai
fi
}
check_destination || fail 'The installed executable changed. Check which copy you want before enabling updates.'
if [ -n "$profile_dir" ]; then
update_profile() {
[ -n "$2" ] || return 1
if [ "$1" = .zlogin ]; then
set -- __openai_zsh_login_profile "$profile_dir" "$2"
elif [ "$package" = false ] && [ "$profile_name" = .bashrc ]; then
set -- __openai_archive_shell_profile "$profile_dir" "$2" "$1"
elif [ "$package" = true ]; then
set -- __openai_linux_package_profile "$profile_dir" "$2" "$1"
else
set -- __openai_archive_zsh_profile "$profile_dir" "$2"
fi
result=$(cd -P "$profile_dir" && env -i HOME="$HOME" PATH=/usr/bin:/bin OPENAI_CLI_ARCHIVE_INSTALL=1 \
"$selected_cli" "$@") || result=
[ "$result" = ready ]
}
profile_error='The CLI is installed. Check your shell profile and rerun setup.'
update_profile "$profile_name" "$profile_id" || fail "$profile_error"
if [ -n "$login_profile" ] && ! update_profile "$login_profile" "$login_id"; then
[ "$login_profile" = .zlogin ] || fail "$profile_error"
printf '%s\n' 'Setup could not safely configure ${ZDOTDIR:-$HOME}/.zlogin. A new login shell may select another openai.' \
'Use "$HOME/.openai/bin/openai" directly, or make .zlogin a private, writable regular file and rerun setup.' >&2
fi
check_destination || fail 'The installed executable changed during setup. Check the selected copy.'
fi
clean_stage "$stage"
trap - EXIT
# A sourced installer sets up insertion once in the calling shell and its startup files.
# An explicitly shell-aware standalone install can still prepare future sessions.
if [ -n "$profile_dir" ] && [ "${OPENAI_CLI_SOURCED_INSTALL:-}" != 1 ]; then
case $profile_name in
.bashrc) set -- bash ;;
*) set -- zsh --profile "$profile_dir/$profile_name" ;;
esac
PATH="$HOME/.openai/bin:$PATH" "$HOME/.openai/bin/openai" @completion "$@" --auto-suggestions >/dev/null ||
printf '%s\n' 'The CLI is installed. Prompt insertion setup could not finish.' >&2
fi
printf 'Installed OpenAI CLI %s at $HOME/.openai/bin/openai.\n' "${active:-$release}"
if [ -z "$profile_dir" ] && [ "$(command -v openai || :)" != "$HOME/.openai/bin/openai" ]; then
printf '%s\n' 'Select it in this shell: export PATH="$HOME/.openai/bin:$PATH"'
fi
printf '%s\n' "$advice"
if [ "$readiness" = ready ]; then
printf '%s\n' 'To enable automatic updates for this installation, run openai updates enable.'
else
printf '%s\n' 'To enable automatic updates, set up GitHub CLI first. Then run openai updates enable.'
fi