2
2
set -euo pipefail
3
3
APP=opencode
4
4
5
- RED=' \033[0;31m'
6
- GREEN=' \033[0;32m'
7
- YELLOW=' \033[1;33m'
8
- ORANGE=' \033[38;2;255;140;0m'
9
- NC=' \033[0m' # No Color
5
+ declare -A colors=(
6
+ [RED]=' \033[0;31m'
7
+ [GREEN]=' \033[0;32m'
8
+ [YELLOW]=' \033[1;33m'
9
+ [ORANGE]=' \033[38;2;255;140;0m'
10
+ [NC]=' \033[0m'
11
+ )
10
12
11
13
requested_version=${VERSION:- }
12
14
17
19
arch=$( uname -m)
18
20
19
21
if [[ " $arch " == " aarch64" ]]; then
20
- arch=" arm64"
22
+ arch=" arm64"
21
23
fi
22
24
23
25
filename=" $APP -$os -$arch .tar.gz"
24
26
25
-
26
27
case " $filename " in
27
- * " -linux-" * )
28
- [[ " $arch " == " x86_64" || " $arch " == " arm64" || " $arch " == " i386" ]] || exit 1
28
+ * " -linux-" * )
29
+ [[ " $arch " == " x86_64" || " $arch " == " arm64" || " $arch " == " i386" ]] || exit 1
29
30
;;
30
- * " -mac-" * )
31
- [[ " $arch " == " x86_64" || " $arch " == " arm64" ]] || exit 1
31
+ * " -mac-" * )
32
+ [[ " $arch " == " x86_64" || " $arch " == " arm64" ]] || exit 1
32
33
;;
33
- * )
34
- echo " ${RED} Unsupported OS/Arch: $os /$arch ${NC} "
35
- exit 1
34
+ * )
35
+ echo " ${RED} Unsupported OS/Arch: $os /$arch ${NC} "
36
+ exit 1
36
37
;;
37
38
esac
38
39
@@ -58,19 +59,18 @@ print_message() {
58
59
local color=" "
59
60
60
61
case $level in
61
- info) color=" ${GREEN} " ;;
62
- warning) color=" ${YELLOW} " ;;
63
- error) color=" ${RED} " ;;
62
+ info) color=" ${colors[ GREEN] } " ;;
63
+ warning) color=" ${colors[ YELLOW] } " ;;
64
+ error) color=" ${colors[ RED] } " ;;
64
65
esac
65
66
66
- echo -e " ${color}${message}${NC } "
67
+ echo -e " ${color}${message}${colors[NC] } "
67
68
}
68
69
69
70
check_version () {
70
71
if command -v opencode > /dev/null 2>&1 ; then
71
72
opencode_path=$( which opencode)
72
73
73
-
74
74
# # TODO: check if version is installed
75
75
# installed_version=$(opencode version)
76
76
installed_version=" 0.0.1"
@@ -87,24 +87,23 @@ check_version() {
87
87
88
88
download_and_install () {
89
89
print_message info " Downloading ${ORANGE} opencode ${GREEN} version: ${YELLOW} $specific_version ${GREEN} ..."
90
- mkdir -p opencodetmp && cd opencodetmp
90
+ temp_dir= $( mktemp -d ) && cd " $temp_dir "
91
91
curl -# -L $url | tar xz
92
92
mv opencode $INSTALL_DIR
93
- cd .. && rm -rf opencodetmp
93
+ cd .. && rm -rf opencodetmp
94
94
}
95
95
96
96
check_version
97
97
download_and_install
98
98
99
-
100
99
add_to_path () {
101
100
local config_file=$1
102
101
local command=$2
103
102
104
103
if [[ -w $config_file ]]; then
105
- echo -e " \n# opencode" >> " $config_file "
106
- echo " $command " >> " $config_file "
107
- print_message info " Successfully added ${ORANGE} opencode ${GREEN} to \$ PATH in $config_file "
104
+ echo -e " \n# opencode" >> " $config_file "
105
+ echo " $command " >> " $config_file "
106
+ print_message info " Successfully added ${colors[ ORANGE] } opencode ${colors[ GREEN] } to \$ PATH in $config_file "
108
107
else
109
108
print_message warning " Manually add the directory to $config_file (or similar):"
110
109
print_message info " $command "
@@ -115,24 +114,24 @@ XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
115
114
116
115
current_shell=$( basename " $SHELL " )
117
116
case $current_shell in
118
- fish)
119
- config_files=" $HOME /.config/fish/config.fish"
117
+ fish)
118
+ config_files=" $HOME /.config/fish/config.fish"
120
119
;;
121
- zsh)
122
- config_files=" $HOME /.zshrc $HOME /.zshenv $XDG_CONFIG_HOME /zsh/.zshrc $XDG_CONFIG_HOME /zsh/.zshenv"
120
+ zsh)
121
+ config_files=" $HOME /.zshrc $HOME /.zshenv $XDG_CONFIG_HOME /zsh/.zshrc $XDG_CONFIG_HOME /zsh/.zshenv"
123
122
;;
124
- bash)
125
- config_files=" $HOME /.bashrc $HOME /.bash_profile $HOME /.profile $XDG_CONFIG_HOME /bash/.bashrc $XDG_CONFIG_HOME /bash/.bash_profile"
123
+ bash)
124
+ config_files=" $HOME /.bashrc $HOME /.bash_profile $HOME /.profile $XDG_CONFIG_HOME /bash/.bashrc $XDG_CONFIG_HOME /bash/.bash_profile"
126
125
;;
127
- ash)
128
- config_files=" $HOME /.ashrc $HOME /.profile /etc/profile"
126
+ ash)
127
+ config_files=" $HOME /.ashrc $HOME /.profile /etc/profile"
129
128
;;
130
- sh)
131
- config_files=" $HOME /.ashrc $HOME /.profile /etc/profile"
129
+ sh)
130
+ config_files=" $HOME /.ashrc $HOME /.profile /etc/profile"
132
131
;;
133
- * )
134
- # Default case if none of the above matches
135
- config_files=" $HOME /.bashrc $HOME /.bash_profile $XDG_CONFIG_HOME /bash/.bashrc $XDG_CONFIG_HOME /bash/.bash_profile"
132
+ * )
133
+ # Default case if none of the above matches
134
+ config_files=" $HOME /.bashrc $HOME /.bash_profile $XDG_CONFIG_HOME /bash/.bashrc $XDG_CONFIG_HOME /bash/.bash_profile"
136
135
;;
137
136
esac
138
137
@@ -149,32 +148,33 @@ if [[ -z $config_file ]]; then
149
148
exit 1
150
149
fi
151
150
152
- if [[ " :$PATH :" != * " :$INSTALL_DIR :" * ]]; then
151
+ if [[ " :$PATH :" == * " :$INSTALL_DIR :" * ]]; then
152
+ print_message info " PATH already contains install directory: $INSTALL_DIR "
153
+ else
153
154
case $current_shell in
154
- fish)
155
- add_to_path " $config_file " " fish_add_path $INSTALL_DIR "
155
+ fish)
156
+ add_to_path " $config_file " " fish_add_path $INSTALL_DIR "
156
157
;;
157
- zsh)
158
- add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
158
+ zsh)
159
+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
159
160
;;
160
- bash)
161
- add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
161
+ bash)
162
+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
162
163
;;
163
- ash)
164
- add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
164
+ ash)
165
+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
165
166
;;
166
- sh)
167
- add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
167
+ sh)
168
+ add_to_path " $config_file " " export PATH=$INSTALL_DIR :\$ PATH"
168
169
;;
169
- * )
170
- print_message warning " Manually add the directory to $config_file (or similar):"
171
- print_message info " export PATH=$INSTALL_DIR :\$ PATH"
170
+ * )
171
+ print_message warning " Manually add the directory to $config_file (or similar):"
172
+ print_message info " export PATH=$INSTALL_DIR :\$ PATH"
172
173
;;
173
174
esac
174
175
fi
175
176
176
177
if [ -n " ${GITHUB_ACTIONS-} " ] && [ " ${GITHUB_ACTIONS} " == " true" ]; then
177
- echo " $INSTALL_DIR " >> $GITHUB_PATH
178
+ echo " $INSTALL_DIR " >> $GITHUB_PATH
178
179
print_message info " Added $INSTALL_DIR to \$ GITHUB_PATH"
179
180
fi
180
-
0 commit comments