forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1167 lines (981 loc) · 28.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1167 lines (981 loc) · 28.9 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Adapted from https://github.com/starship/starship/blob/master/install/install.sh
help_text="Options
-V, --verbose
Enable verbose output for the installer
-f, -y, --force, --yes
Skip the confirmation prompt during installation
-p, --platform
Override the platform identified by the installer
-b, --bin-dir
Override the bin installation directory
Precedence: --bin-dir > RAILWAY_BIN_DIR > \$RAILWAY_HOME/bin > ~/.railway/bin
-a, --arch
Override the architecture identified by the installer
-B, --base-url
Override the base URL used for downloading releases
--agents
Install or reuse the Railway CLI, then configure Railway agent support
--remote
When used with --agents, configure the remote HTTP MCP server at
mcp.railway.com instead of the local stdio server
-r, --remove
Uninstall railway
-h, --help
Get some help
"
set -eu
printf '\n'
BOLD="$(tput bold 2>/dev/null || printf '')"
GREY="$(tput setaf 0 2>/dev/null || printf '')"
UNDERLINE="$(tput smul 2>/dev/null || printf '')"
RED="$(tput setaf 1 2>/dev/null || printf '')"
GREEN="$(tput setaf 2 2>/dev/null || printf '')"
YELLOW="$(tput setaf 3 2>/dev/null || printf '')"
BLUE="$(tput setaf 4 2>/dev/null || printf '')"
MAGENTA="$(tput setaf 5 2>/dev/null || printf '')"
NO_COLOR="$(tput sgr0 2>/dev/null || printf '')"
SUPPORTED_TARGETS="x86_64-unknown-linux-gnu x86_64-unknown-linux-musl \
i686-unknown-linux-musl aarch64-unknown-linux-musl \
arm-unknown-linux-musleabihf x86_64-apple-darwin \
aarch64-apple-darwin x86_64-pc-windows-msvc \
i686-pc-windows-msvc aarch64-pc-windows-msvc \
x86_64-unknown-freebsd"
info() {
printf '%s\n' "${BOLD}${GREY}>${NO_COLOR} $*"
}
debug() {
if [ -n "${VERBOSE}" ]; then
printf '%s\n' "${BOLD}${GREY}>${NO_COLOR} $*"
fi
}
warn() {
printf '%s\n' "${YELLOW}! $*${NO_COLOR}"
}
error() {
printf '%s\n' "${RED}x $*${NO_COLOR}" >&2
}
completed() {
printf '%s\n' "${GREEN}✓${NO_COLOR} $*"
}
has() {
command -v "$1" 1>/dev/null 2>&1
}
RANDOM_FOR_SH=$(od -vAn -N4 -tu4 < /dev/urandom | sed 's/\t*$//g')
# Removes leading whitespace
RANDOM_FOR_SH=$(echo ${RANDOM_FOR_SH:-$RANDOM})
# Gets path to a temporary file, even if
get_tmpfile() {
local suffix
suffix="$1"
if has mktemp; then
printf "%s%s.%s.%s" "$(mktemp)" "-railway" "${RANDOM_FOR_SH}" "${suffix}"
else
# No really good options here--let's pick a default + hope
printf "/tmp/railway.%s" "${suffix}"
fi
}
# Test if a location is writeable by trying to write to it. Windows does not let
# you test writeability other than by writing: https://stackoverflow.com/q/1999988
test_writeable() {
local path
path="${1:-}/test.txt"
if touch "${path}" 2>/dev/null; then
rm "${path}"
return 0
else
return 1
fi
}
default_railway_home() {
if [ -n "${RAILWAY_HOME-}" ]; then
printf '%s' "${RAILWAY_HOME}"
return 0
fi
if [ -n "${HOME-}" ]; then
printf '%s' "${HOME}/.railway"
return 0
fi
return 1
}
tildify() {
if [ -n "${HOME-}" ]; then
case "$1" in
"$HOME"/*) printf '~/%s' "${1#"$HOME"/}" ;;
"$HOME") printf '~' ;;
*) printf '%s' "$1" ;;
esac
else
printf '%s' "$1"
fi
}
shell_quote() {
local value="$1"
value=${value//\'/\'\\\'\'}
printf "'%s'" "$value"
}
fish_quote() {
local value="$1"
value=${value//\\/\\\\}
value=${value//\'/\\\'}
printf "'%s'" "$value"
}
source_path() {
local path="$1"
if [ -n "${HOME-}" ]; then
case "$path" in
"$HOME"/*) printf '"$HOME/%s"' "${path#"$HOME"/}"; return ;;
esac
fi
shell_quote "$path"
}
fish_source_path() {
local path="$1"
if [ -n "${HOME-}" ]; then
case "$path" in
"$HOME"/*) printf '"$HOME/%s"' "${path#"$HOME"/}"; return ;;
esac
fi
fish_quote "$path"
}
bin_dir_uses_railway_home() {
[ -n "${RAILWAY_HOME_DIR-}" ] && [ "${BIN_DIR%/}" = "${RAILWAY_HOME_DIR%/}/bin" ]
}
download() {
file="$1"
url="$2"
touch "$file"
if has curl; then
cmd="curl --fail --silent --location --output $file $url"
elif has wget; then
cmd="wget --quiet --output-document=$file $url"
elif has fetch; then
cmd="fetch --quiet --output=$file $url"
else
error "No HTTP download program (curl, wget, fetch) found, exiting…"
return 1
fi
$cmd && return 0 || rc=$?
error "Command failed (exit code $rc): ${BLUE}${cmd}${NO_COLOR}"
printf "\n" >&2
info "This is likely due to railway not yet supporting your configuration."
info "If you would like to see a build for your configuration,"
info "please create an issue requesting a build for ${MAGENTA}${TARGET}${NO_COLOR}:"
info "${BOLD}${UNDERLINE}https://github.com/railwayapp/cli/issues/new/${NO_COLOR}"
return $rc
}
unpack() {
local archive=$1
local bin_dir=$2
local sudo=${3-}
case "$archive" in
*.tar.gz)
flags=$(test -n)
${sudo} tar "${flags}" -xzf "${archive}" -C "${bin_dir}"
return 0
;;
*.zip)
flags=$(test -z)
UNZIP="${flags}" ${sudo} unzip "${archive}" -d "${bin_dir}"
return 0
;;
esac
error "Unknown package extension."
printf "\n"
info "This almost certainly results from a bug in this script--please file a"
info "bug report at https://github.com/railwayapp/cli/issues"
return 1
}
elevate_priv() {
if ! has sudo; then
error 'Could not find the command "sudo", needed to get permissions for install.'
info "If you are on Windows, please run your shell as an administrator, then"
info "rerun this script. Otherwise, please run this script as root, or install"
info "sudo."
exit 1
fi
if ! sudo -v; then
error "Superuser not granted, aborting installation"
exit 1
fi
}
install() {
local msg
local sudo
local archive
local ext="$1"
if test_writeable "${BIN_DIR}"; then
sudo=""
msg="Installing railway, please wait…"
else
warn "Escalated permissions are required to install to ${BIN_DIR}"
elevate_priv
sudo="sudo"
msg="Installing railway as root, please wait…"
fi
info "$msg"
archive=$(get_tmpfile "$ext")
# download to the temp file
download "${archive}" "${URL}"
# unpack the temp file to the bin dir, using sudo if required
unpack "${archive}" "${BIN_DIR}" "${sudo}"
# remove tempfile
# rm "${archive}"
}
# Currently supporting:
# - win (Git Bash)
# - darwin
# - linux
# - linux_musl (Alpine)
# - freebsd
detect_platform() {
local platform
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "${platform}" in
msys_nt*) platform="pc-windows-msvc" ;;
cygwin_nt*) platform="pc-windows-msvc";;
# mingw is Git-Bash
mingw*) platform="pc-windows-msvc" ;;
# use the statically compiled musl bins on linux to avoid linking issues.
linux) platform="unknown-linux-musl" ;;
darwin) platform="apple-darwin" ;;
freebsd) platform="unknown-freebsd" ;;
esac
printf '%s' "${platform}"
}
# Currently supporting:
# - x86_64
# - i386
detect_arch() {
local arch
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
case "${arch}" in
amd64) arch="x86_64" ;;
armv*) arch="arm" ;;
arm64) arch="aarch64" ;;
esac
# `uname -m` in some cases mis-reports 32-bit OS as 64-bit, so double check
if [ "${arch}" = "x86_64" ] && [ "$(getconf LONG_BIT)" -eq 32 ]; then
arch=i686
elif [ "${arch}" = "aarch64" ] && [ "$(getconf LONG_BIT)" -eq 32 ]; then
arch=arm
fi
printf '%s' "${arch}"
}
detect_target() {
local arch="$1"
local platform="$2"
local target="$arch-$platform"
if [ "${target}" = "arm-unknown-linux-musl" ]; then
target="${target}eabihf"
fi
printf '%s' "${target}"
}
confirm() {
if [ -t 0 ]; then
if [ -z "${FORCE-}" ]; then
printf "%s " "${MAGENTA}?${NO_COLOR} $* ${BOLD}[y/N]${NO_COLOR}"
set +e
read -r yn </dev/tty
rc=$?
set -e
if [ $rc -ne 0 ]; then
error "Error reading from prompt (please re-run with the '--yes' option)"
exit 1
fi
if [ "$yn" != "y" ] && [ "$yn" != "yes" ]; then
error 'Aborting (please answer "yes" to continue)'
exit 1
fi
fi
fi
}
check_bin_dir() {
local bin_dir="$1"
if [ ! -d "$bin_dir" ]; then
error "Installation location $bin_dir does not appear to be a directory"
info "Make sure the location exists and is a directory, then try again."
exit 1
fi
}
is_build_available() {
local arch="$1"
local platform="$2"
local target="$3"
local good
good=$(
IFS=" "
for t in $SUPPORTED_TARGETS; do
if [ "${t}" = "${target}" ]; then
printf 1
break
fi
done
)
if [ "${good}" != "1" ]; then
error "${arch} builds for ${platform} are not yet available for railway"
printf "\n" >&2
info "If you would like to see a build for your configuration,"
info "please create an issue requesting a build for ${MAGENTA}${target}${NO_COLOR}:"
info "${BOLD}${UNDERLINE}https://github.com/railwayapp/cli/issues/new/${NO_COLOR}"
printf "\n"
exit 1
fi
}
UNINSTALL=0
HELP=0
AGENTS=0
REMOTE=0
RAILWAY_HOME_DIR=""
RAILWAY_ENV_FILE=""
RAILWAY_FISH_ENV_FILE=""
PATH_ACTIVATION_PRINTED=0
RAILWAY_PATH_MARKER_BEGIN="# >>> railway initialize >>>"
RAILWAY_PATH_MARKER_END="# <<< railway initialize <<<"
SHELL_STARTUP_FILE=""
SHELL_STARTUP_ACTION=""
DEFAULT_VERSION=$(curl -s https://api.github.com/repos/railwayapp/cli/releases/latest | grep -o '"tag_name":[[:space:]]*"v[^"]*"' | cut -d'"' -f4 | cut -c2-)
if [ -z "$DEFAULT_VERSION" ]; then
error "Failed to fetch latest version from GitHub"
exit 1
fi
# defaults
if [ -z "${RAILWAY_VERSION-}" ]; then
RAILWAY_VERSION="$DEFAULT_VERSION"
fi
if [ -z "${RAILWAY_PLATFORM-}" ]; then
PLATFORM="$(detect_platform)"
fi
if RAILWAY_HOME_DIR="$(default_railway_home)"; then
if [ -n "${RAILWAY_BIN_DIR-}" ]; then
BIN_DIR="${RAILWAY_BIN_DIR}"
else
BIN_DIR="${RAILWAY_HOME_DIR%/}/bin"
fi
else
if [ -n "${RAILWAY_BIN_DIR-}" ]; then
BIN_DIR="${RAILWAY_BIN_DIR}"
else
BIN_DIR=""
fi
fi
if [ -z "${RAILWAY_ARCH-}" ]; then
ARCH="$(detect_arch)"
fi
if [ -z "${RAILWAY_BASE_URL-}" ]; then
BASE_URL="https://github.com/railwayapp/cli/releases"
fi
# parse argv variables
while [ "$#" -gt 0 ]; do
case "$1" in
-p | --platform)
PLATFORM="$2"
shift 2
;;
-b | --bin-dir)
BIN_DIR="$2"
shift 2
;;
-a | --arch)
ARCH="$2"
shift 2
;;
-B | --base-url)
BASE_URL="$2"
shift 2
;;
-V | --verbose)
VERBOSE=1
shift 1
;;
-f | -y | --force | --yes)
FORCE=1
shift 1
;;
-r | --remove | --uninstall)
UNINSTALL=1
shift 1
;;
--agents)
AGENTS=1
shift 1
;;
--remote)
REMOTE=1
shift 1
;;
-h | --help)
HELP=1
shift 1
;;
-p=* | --platform=*)
PLATFORM="${1#*=}"
shift 1
;;
-b=* | --bin-dir=*)
BIN_DIR="${1#*=}"
shift 1
;;
-a=* | --arch=*)
ARCH="${1#*=}"
shift 1
;;
-B=* | --base-url=*)
BASE_URL="${1#*=}"
shift 1
;;
-V=* | --verbose=*)
VERBOSE="${1#*=}"
shift 1
;;
-f=* | -y=* | --force=* | --yes=*)
FORCE="${1#*=}"
shift 1
;;
*)
error "Unknown option: $1"
exit 1
;;
esac
done
# non-empty VERBOSE enables verbose untarring
if [ -n "${VERBOSE-}" ]; then
VERBOSE=v
else
VERBOSE=
fi
# --remote only takes effect alongside --agents (it routes setup agent to the
# hosted MCP server). Warn loudly rather than silently no-op.
if [ "$REMOTE" = 1 ] && [ "$AGENTS" != 1 ]; then
warn "--remote has no effect without --agents. Re-run with both flags to configure remote MCP."
fi
write_env_files() {
local quoted_railway_home
local fish_railway_home
if ! bin_dir_uses_railway_home; then
return 0
fi
if ! mkdir -p "$RAILWAY_HOME_DIR"; then
warn "Could not create $(tildify "$RAILWAY_HOME_DIR"); skipping activation file."
return 0
fi
RAILWAY_ENV_FILE="$RAILWAY_HOME_DIR/env"
RAILWAY_FISH_ENV_FILE="$RAILWAY_HOME_DIR/env.fish"
if [ -e "$RAILWAY_ENV_FILE" ] && { [ ! -f "$RAILWAY_ENV_FILE" ] || [ ! -w "$RAILWAY_ENV_FILE" ]; }; then
warn "Could not write $(tildify "$RAILWAY_ENV_FILE"); skipping activation file."
RAILWAY_ENV_FILE=""
RAILWAY_FISH_ENV_FILE=""
return 0
fi
quoted_railway_home="$(shell_quote "$RAILWAY_HOME_DIR")"
fish_railway_home="$(fish_quote "$RAILWAY_HOME_DIR")"
if ! {
printf 'export RAILWAY_HOME=%s\n' "$quoted_railway_home"
printf 'case ":$PATH:" in\n'
printf ' *":$RAILWAY_HOME/bin:"*) ;;\n'
printf ' *) export PATH="$RAILWAY_HOME/bin:$PATH" ;;\n'
printf 'esac\n'
} > "$RAILWAY_ENV_FILE"; then
warn "Could not write $(tildify "$RAILWAY_ENV_FILE"); skipping activation file."
RAILWAY_ENV_FILE=""
RAILWAY_FISH_ENV_FILE=""
return 0
fi
if [ -e "$RAILWAY_FISH_ENV_FILE" ] && { [ ! -f "$RAILWAY_FISH_ENV_FILE" ] || [ ! -w "$RAILWAY_FISH_ENV_FILE" ]; }; then
warn "Could not write $(tildify "$RAILWAY_FISH_ENV_FILE"); fish users may need to add $(tildify "$BIN_DIR") to PATH manually."
RAILWAY_FISH_ENV_FILE=""
return 0
fi
if ! {
printf 'set -gx RAILWAY_HOME %s\n' "$fish_railway_home"
printf 'if not contains "$RAILWAY_HOME/bin" $PATH\n'
printf ' set -gx PATH "$RAILWAY_HOME/bin" $PATH\n'
printf 'end\n'
} > "$RAILWAY_FISH_ENV_FILE"; then
warn "Could not write $(tildify "$RAILWAY_FISH_ENV_FILE"); fish users may need to add $(tildify "$BIN_DIR") to PATH manually."
RAILWAY_FISH_ENV_FILE=""
fi
}
print_path_commands() {
local commands="$1"
local command
printf '%s\n' "$commands" | while IFS= read -r command; do
printf ' %s\n' "$command"
done
}
activation_command() {
local shell_name=""
local env_file="$RAILWAY_ENV_FILE"
if [ -n "${SHELL-}" ]; then
shell_name="$(basename "$SHELL")"
fi
if [ "$shell_name" = "fish" ]; then
if [ -n "$RAILWAY_FISH_ENV_FILE" ]; then
env_file="$RAILWAY_FISH_ENV_FILE"
else
return 1
fi
printf 'source %s' "$(fish_source_path "$env_file")"
return 0
fi
if [ -n "$env_file" ]; then
printf 'source %s' "$(source_path "$env_file")"
return 0
fi
return 1
}
configure_shell_startup() {
local contents="$1"
local shell_name=""
local rc_file=""
local rc_dir
local tmp_file
SHELL_STARTUP_FILE=""
SHELL_STARTUP_ACTION=""
if [ -z "${HOME-}" ]; then
return 1
fi
if [ -n "${SHELL-}" ]; then
shell_name="$(basename "$SHELL")"
fi
case "$shell_name" in
fish)
rc_file="$HOME/.config/fish/config.fish"
;;
zsh)
rc_file="$HOME/.zshrc"
;;
bash)
if [ -f "$HOME/.bash_profile" ]; then
rc_file="$HOME/.bash_profile"
else
rc_file="$HOME/.bashrc"
fi
;;
*)
return 1
;;
esac
if [ -e "$rc_file" ] && { [ ! -f "$rc_file" ] || [ ! -w "$rc_file" ]; }; then
warn "Could not update $(tildify "$rc_file"); add $(tildify "$BIN_DIR") to PATH manually."
return 1
fi
rc_dir="$(dirname "$rc_file")"
if ! mkdir -p "$rc_dir"; then
warn "Could not create $(tildify "$rc_dir"); add $(tildify "$BIN_DIR") to PATH manually."
return 1
fi
if [ -f "$rc_file" ]; then
if grep -qF "$RAILWAY_PATH_MARKER_BEGIN" "$rc_file"; then
SHELL_STARTUP_ACTION="Updated"
else
SHELL_STARTUP_ACTION="Added"
fi
else
SHELL_STARTUP_ACTION="Created"
fi
tmp_file="$(get_tmpfile shell)"
if [ -f "$rc_file" ]; then
if ! awk -v begin="$RAILWAY_PATH_MARKER_BEGIN" -v end="$RAILWAY_PATH_MARKER_END" '
$0 == begin { skip = 1; next }
$0 == end { skip = 0; next }
!skip { print }
' "$rc_file" > "$tmp_file"; then
rm -f "$tmp_file"
warn "Could not update $(tildify "$rc_file"); add $(tildify "$BIN_DIR") to PATH manually."
return 1
fi
else
: > "$tmp_file"
fi
{
printf '\n%s\n' "$RAILWAY_PATH_MARKER_BEGIN"
printf '%s\n' "$contents"
printf '%s\n' "$RAILWAY_PATH_MARKER_END"
} >> "$tmp_file"
if [ -f "$rc_file" ]; then
if ! cat "$tmp_file" > "$rc_file"; then
rm -f "$tmp_file"
warn "Could not update $(tildify "$rc_file"); add $(tildify "$BIN_DIR") to PATH manually."
return 1
fi
rm -f "$tmp_file"
elif ! mv "$tmp_file" "$rc_file"; then
rm -f "$tmp_file"
warn "Could not create $(tildify "$rc_file"); add $(tildify "$BIN_DIR") to PATH manually."
return 1
fi
SHELL_STARTUP_FILE="$rc_file"
return 0
}
configure_shell_path() {
local quoted_bin_dir
local quoted_railway_home
local fish_bin_dir
local fish_railway_home
local bash_line
local bash_contents
local fish_line
local fish_contents
local path_commands
local startup_contents
local activation
local shell_name
if bin_dir_uses_railway_home; then
quoted_railway_home="$(shell_quote "$RAILWAY_HOME_DIR")"
fish_railway_home="$(fish_quote "$RAILWAY_HOME_DIR")"
bash_line='export PATH="$RAILWAY_HOME/bin:$PATH"'
bash_contents="export RAILWAY_HOME=$quoted_railway_home
$bash_line"
fish_line='set -gx PATH "$RAILWAY_HOME/bin" $PATH'
fish_contents="set -gx RAILWAY_HOME $fish_railway_home
$fish_line"
else
quoted_bin_dir="$(shell_quote "$BIN_DIR")"
fish_bin_dir="$(fish_quote "$BIN_DIR")"
bash_line="export PATH=$quoted_bin_dir:\"\$PATH\""
bash_contents="$bash_line"
fish_line="set -gx PATH $fish_bin_dir \$PATH"
fish_contents="$fish_line"
fi
shell_name=""
if [ -n "${SHELL-}" ]; then
shell_name="$(basename "$SHELL")"
fi
path_commands="$bash_contents"
startup_contents="$bash_contents"
if [ "$shell_name" = "fish" ]; then
path_commands="$fish_contents"
startup_contents="$fish_contents"
fi
if activation="$(activation_command)"; then
path_commands="$activation"
startup_contents="$activation"
fi
warn "Railway was installed to $(tildify "$BIN_DIR"), but this shell does not resolve 'railway' from there yet."
if configure_shell_startup "$startup_contents"; then
info "$SHELL_STARTUP_ACTION Railway PATH setup in $(tildify "$SHELL_STARTUP_FILE")"
info "New terminals will have railway available automatically."
else
info "To make railway available in new terminals, add this command to your shell startup file:"
print_path_commands "$startup_contents"
fi
info "To use railway in this terminal, run:"
print_path_commands "$path_commands"
PATH_ACTIVATION_PRINTED=1
}
installed_railway_is_on_path() {
local railway_on_path
local installed_bin
railway_on_path="$(command -v railway 2>/dev/null || true)"
installed_bin="${BIN_DIR%/}/railway"
[ -n "$railway_on_path" ] || return 1
[ "$railway_on_path" = "$installed_bin" ] && return 0
[ -e "$railway_on_path" ] && [ -e "$installed_bin" ] && [ "$railway_on_path" -ef "$installed_bin" ]
}
extract_railway_version() {
# Pull X.Y.Z (with optional pre-release suffix and leading v) out of
# `--version` output. Echoes empty string on failure so callers can
# treat missing versions as "skip upgrade check".
printf '%s' "$1" | grep -Eo 'v?[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9.-]+)?' | head -1 | sed 's/^v//'
}
version_lt() {
# Returns 0 (true) when $1 is older than $2 by major.minor.patch.
# Pre-release suffixes are stripped before comparison so "4.55.0"
# beats "4.55.0-rc.1". Returns 1 (false) for equal, newer, or
# unparseable inputs — i.e. errs on the side of NOT triggering an
# upgrade when we can't tell.
local a b a1 a2 a3 b1 b2 b3
if [ -z "${1-}" ] || [ -z "${2-}" ]; then return 1; fi
a="${1%%-*}"
b="${2%%-*}"
a1="$(printf '%s' "$a" | cut -d. -f1)"
a2="$(printf '%s' "$a" | cut -d. -f2)"
a3="$(printf '%s' "$a" | cut -d. -f3)"
b1="$(printf '%s' "$b" | cut -d. -f1)"
b2="$(printf '%s' "$b" | cut -d. -f2)"
b3="$(printf '%s' "$b" | cut -d. -f3)"
a1=${a1:-0}; a2=${a2:-0}; a3=${a3:-0}
b1=${b1:-0}; b2=${b2:-0}; b3=${b3:-0}
case "$a1$a2$a3$b1$b2$b3" in *[!0-9]*) return 1 ;; esac
if [ "$a1" -lt "$b1" ]; then return 0; fi
if [ "$a1" -gt "$b1" ]; then return 1; fi
if [ "$a2" -lt "$b2" ]; then return 0; fi
if [ "$a2" -gt "$b2" ]; then return 1; fi
if [ "$a3" -lt "$b3" ]; then return 0; fi
return 1
}
find_railway_bins() {
local seen=":"
local dir bin
local old_ifs="$IFS"
IFS=':'
set -- $PATH
IFS="$old_ifs"
for dir do
[ -n "$dir" ] || continue
bin="$dir/railway"
[ -x "$bin" ] || continue
case "$seen" in
*":$bin:"*) ;;
*)
printf '%s\n' "$bin"
seen="$seen$bin:"
;;
esac
done
}
print_cli_conflicts() {
local bins_file
local bin version
local count=0
bins_file="$(get_tmpfile bins)"
find_railway_bins > "$bins_file"
if [ ! -s "$bins_file" ]; then
rm -f "$bins_file"
return
fi
info "Railway CLI on PATH:"
while IFS= read -r bin; do
count=$((count + 1))
version="$("$bin" --version 2>/dev/null || echo unknown)"
info " $bin ($version)"
done < "$bins_file"
rm -f "$bins_file"
info "Shells use the first entry above."
if [ "$count" -gt 1 ]; then
warn "Multiple Railway CLI installs found. No PATH entries were reordered or removed."
fi
}
warn_existing_path_conflict() {
local railway_on_path
local installed_bin
railway_on_path="$(command -v railway 2>/dev/null || true)"
installed_bin="${BIN_DIR%/}/railway"
[ -n "$railway_on_path" ] || return 0
[ -x "$installed_bin" ] || return 0
if installed_railway_is_on_path; then
return 0
fi
warn "Another Railway CLI is already on PATH: $railway_on_path"
warn "After activation, shells will use $(tildify "$installed_bin") first."
}
run_agent_setup() {
local railway_bin="$1"
local yes=""
local remote=""
if [ -z "${RAILWAY_INSTALL_REQUEST_ID-}" ]; then
RAILWAY_INSTALL_REQUEST_ID="install_$(od -vAn -N16 -tx1 < /dev/urandom | tr -d ' \n')"
export RAILWAY_INSTALL_REQUEST_ID
fi
if [ -n "${FORCE-}" ] || [ ! -t 0 ]; then
yes="-y"
fi
if [ "$REMOTE" = 1 ]; then
remote="--remote"
fi
if [ -t 0 ] && [ -z "${FORCE-}" ]; then
if "$railway_bin" whoami >/dev/null 2>&1; then
info "Already logged in to Railway."
else
info "Logging in to Railway (opens a browser)..."
"$railway_bin" login
fi
fi
"$railway_bin" setup agent $yes $remote
if ! "$railway_bin" whoami >/dev/null 2>&1; then
warn "Next: run '$railway_bin login' to finish setup (opens a browser)."
fi
}
if [ "$UNINSTALL" = 1 ]; then
confirm "Are you sure you want to uninstall railway?"
msg=""
sudo=""
railway_bin="$(command -v railway 2>/dev/null || true)"
if [ -z "$railway_bin" ] && [ -x "$BIN_DIR/railway" ]; then
railway_bin="$BIN_DIR/railway"
fi
if [ -z "$railway_bin" ]; then
error "Could not find railway on PATH or at $BIN_DIR/railway"
exit 1
fi
info "REMOVING railway"
if test_writeable "$(dirname "$railway_bin")"; then
sudo=""
msg="Removing railway, please wait…"
else
warn "Escalated permissions are required to remove ${railway_bin}"
elevate_priv
sudo="sudo"
msg="Removing railway as root, please wait…"
fi
info "$msg"
${sudo} rm "$railway_bin"
${sudo} rm -f /tmp/railway 2>/dev/null || true
info "Removed railway"
exit 0
fi
if [ "$HELP" = 1 ]; then
echo "${help_text}"
exit 0
fi
RAILWAY_UPGRADE_AGENT=0
if [ "$AGENTS" = 1 ] && has railway; then
RAILWAY_BIN="$(command -v railway)"
RAW_VERSION="$("$RAILWAY_BIN" --version 2>/dev/null || true)"
CURRENT_VERSION="$(extract_railway_version "$RAW_VERSION")"
info "Railway CLI already installed: $RAILWAY_BIN (${CURRENT_VERSION:-unknown})"
print_cli_conflicts
NEEDS_UPGRADE=0
if [ -n "$CURRENT_VERSION" ] && version_lt "$CURRENT_VERSION" "$RAILWAY_VERSION"; then
NEEDS_UPGRADE=1
fi
if [ "$NEEDS_UPGRADE" = 0 ]; then
info "No PATH changes were made."
run_agent_setup "$RAILWAY_BIN"
exit 0
fi
# Refuse to clobber package-manager-owned binaries; route the user
# through the right channel and continue with the existing CLI so the
# agent setup still runs. Catches both M-series (/opt/homebrew/*) and