-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
install.sh
191 lines (178 loc) · 4.76 KB
/
install.sh
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
abort() {
printf "%s\n" "$@" >&2
exit 1
}
if [ -z "${BASH_VERSION:-}" ]
then
abort "Bash is required to run this script."
fi
if [[ -t 1 ]]
then
escape() { printf "\033[%sm" "$1"; }
else
escape() { :; }
fi
latest_download_url="https://github.com/vibovenkat123/review-gpt/releases/latest/download"
man_file_path="rgpt.1"
target_man_file_path="rgpt.1"
declare -a binaries=("rgpt-linux-mips" "rgpt-linux-mips64" "rgpt-linux-mips64le" "rgpt-linux-mipsle" "rgpt-linux-ppc64" "rgpt-linux-ppc64le" "rgpt-linux-386" "rgpt-linux-amd64" "rgpt-linux-arm" "rgpt-linux-arm64" "rgpt-macos-arm64" "rgpt-macos-amd64" "rgpt-windows-386" "rgpt-windows-amd64" "rgpt-windows-arm64")
mkcolor() { escape "1;$1"; }
mksecondarycolor() { escape "0;$1"; }
underline="$(escape "4;39")"
blue="$(mkcolor 34)"
red="$(mkcolor 31)"
orange="$(mksecondarycolor 33)"
green="$(mkcolor 32)"
yellow=$(mkcolor 33)
bold="$(mkcolor 39)"
reset="$(escape 0)"
cyan="$(mkcolor 36)"
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"
do
printf " "
printf "%s" "${arg// /\ }"
done
}
print_same_line() {
printf "%s" "${1/"$'\n'"/}"
}
arrow() {
printf "${blue}==>${bold} %s${reset}\n" "$(shell_join "$@")"
}
change() {
printf "${yellow}==>${bold} %s${reset}\n" "$(shell_join "$@")"
}
ok() {
printf "${green}==>${bold} %s${reset}\n" "$(shell_join "$@")"
}
good() {
printf "${green}$1${reset}"
}
warn() {
printf "${yellow}Warning${reset}: %s\n" "$(print_same_line "$1")"
}
error() {
printf "${red}ERROR${reset}: %s\n" "$(print_same_line "$1")"
}
info() {
printf "${blue}Info${reset}: %s\n" "$(print_same_line "$1")"
}
action_required() {
printf "${orange}Action Required${reset}: %s\n" "$(print_same_line "$1")"
}
print_steps() {
printf "${bold}These are the steps the installation will do:${reset}:\n"
declare -a steps=("1. Set up the environment variables" "2. Download the binaries" "3. Add the binaries to your path")
for step in "${steps[@]}"
do
arrow $step
done
echo
}
copy_manual() {
echo
info "Installing man page"
echo
info "The following steps requires sudo/root permissions. It will copy the file to your bin."
wait_for_quit
info "Going to man directory"
cd /usr/local/share/man/man1
if [[ -f "$man_file_path" || -f "$target_man_file_path" ]]
then
change "Deleting already existing manual files"
sudo rm -f $man_file_path
sudo rm -f $target_man_file_path
fi
change "Downloading manual"
sudo curl -LJO $latest_download_url/$man_file_path
change "Moving manual file"
sudo mv $man_file_path $target_man_file_path
arrow "Going back to previous directory"
arrow "$(cd -)"
ok "Copied manual"
}
download_binaries() {
echo
info "Installing binaries"
echo
info "Select the binary for your correct machine. rgpt-x-y (x = os, y = architecture)"
echo
select binary in "${binaries[@]}"
do
if [[ -z "$binary" ]]
then
error "$REPLY is not a valid choice"
echo
exit 1
fi
binary_input="$(( $REPLY - 1 ))"
break
done
echo
binary_name="${binaries[$binary_input]}"
info "The following steps requires sudo/root permissions. It will copy the file to your bin."
wait_for_quit
arrow "Going to bin directory"
cd /usr/local/bin
change "Downloading binaries"
sudo curl -LJO $latest_download_url/$binary_name
change "Moving the binary to the correct name"
sudo mv $binary_name rgpt
change "Giving the file executing permissions"
sudo chmod +x /usr/local/bin/rgpt
arrow "Going back to previous directory"
arrow "$(cd -)"
ok "Copied binaries"
}
wait_for_quit() {
echo -n "$(action_required "Press ${bold}RETURN${reset}/${bold}ENTER${reset} to continue with the installation or any other key to quit:")"
while read -r -n 1 -s answer
do
if [ -z "$answer" ]
then
printf "\n\n"
break
else
abort
fi
done
}
greet() {
printf "${cyan}Welcome to the review-gpt install/update script\n${reset}"
}
success_end() {
echo
echo
good "You have successfully installed/updated review-gpt!"
echo
good "Whenever you want to update, run the same script."
echo
echo
}
ask_for_env() {
read -p "Enter API Key for openai:" key
echo
if ! [[ $key == sk-* ]]
then
error "The openai key you entered is not in the right format"
exit 1
fi
change "Copying API Key to ~/.rgpt.env..."
file_content="OPENAI_KEY=\"${key}\""
echo $file_content > ~/.rgpt.env
ok "Copied env file"
}
greet
echo
wait_for_quit
print_steps
wait_for_quit
ask_for_env
download_binaries
copy_manual
success_end
exit 0