@@ -24,12 +24,9 @@ if [[ "$1" == "-y" ]] || [[ "$CI" == "1" ]] || [[ "$CI" == "true" ]]; then
24
24
CI="1"
25
25
fi
26
26
27
- # Define the GitHub API URL for the latest release
28
- RELEASE_API_URL="https://api.github.com/repos/defang-io/defang/releases/latest"
29
-
30
27
# Use curl to fetch the latest release data
31
28
echo "Fetching the latest release information..."
32
- RELEASE_JSON=$(curl -s -L $RELEASE_API_URL )
29
+ RELEASE_JSON=$(curl -s -L https://api.github.com/repos/defang-io/defang/releases/latest )
33
30
34
31
# Check for curl failure
35
32
if [ $? -ne 0 ]; then
@@ -131,8 +128,8 @@ if ! chmod +x "$INSTALL_DIR/$BINARY_NAME"; then
131
128
return 10
132
129
fi
133
130
134
- # Usage: _prompt_and_append_to_profile "Prompt string" "path/to/.rcfile" "line to add"
135
- _prompt_and_append_to_profile () {
131
+ # Usage: _prompt_and_append_to_file "Prompt string" "path/to/.rcfile" "line to add"
132
+ _prompt_and_append_to_file () {
136
133
local prompt=$1
137
134
local profile_file=$2
138
135
local line=$3
@@ -163,9 +160,6 @@ _prompt_and_append_to_profile() {
163
160
fi
164
161
}
165
162
166
- # Get the name of the current shell
167
- CURRENT_SHELL=$(basename "$SHELL")
168
-
169
163
# Add the installation directory to PATH if not already present
170
164
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
171
165
echo "Adding $INSTALL_DIR to your PATH for this session."
@@ -174,56 +168,59 @@ if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
174
168
echo "Adding $INSTALL_DIR to your PATH for future sessions."
175
169
EXPORT_PATH="export PATH=\"\$PATH:$INSTALL_DIR\""
176
170
177
- # Define the possible shell profile files
178
- PROFILE_FILES=(".bashrc" ".zshrc" ".kshrc")
179
-
180
171
# Loop over the possible profile files
181
172
FOUND_PROFILE_FILE=false
182
- for profile_file in "${PROFILE_FILES[@]}" ; do
173
+ for CURRENT_SHELL in bash zsh ksh ; do
183
174
# If the profile file exists in the user's home directory, add a line to it
184
- if [[ -f "$HOME/$profile_file " ]]; then
175
+ if [[ -f "$HOME/.${CURRENT_SHELL}rc " ]]; then
185
176
FOUND_PROFILE_FILE=true
186
- _prompt_and_append_to_profile "Can we append the necessary line to" "$HOME/$profile_file " "$EXPORT_PATH"
177
+ _prompt_and_append_to_file "Can we append the necessary line to" "$HOME/.${CURRENT_SHELL}rc " "$EXPORT_PATH"
187
178
fi
188
179
done
189
180
190
181
# If no profile file was found
191
182
if [[ $FOUND_PROFILE_FILE == false ]]; then
183
+ # Get the name of the current shell
184
+ CURRENT_SHELL="$(basename "${SHELL:-$0}")"
192
185
# Prompt the user to create a new profile file
193
- CURRENT_RC="$HOME/.${CURRENT_SHELL}rc"
194
- _prompt_and_append_to_profile "No existing profile file found. Can we create" "$CURRENT_RC" "$EXPORT_PATH"
186
+ _prompt_and_append_to_file "No existing profile file found. Can we create" "$HOME/.${CURRENT_SHELL}rc" "$EXPORT_PATH"
195
187
fi
196
188
fi
197
189
198
- # Usage: _generate_completion_script "path/to/completion/_script"
190
+ # Usage: _generate_completion_script "shell" " path/to/completion/_script"
199
191
_generate_completion_script() {
200
- local target=$1
192
+ local shell=$1
193
+ local target=$2
201
194
echo "Generating completion script at $target"
202
195
mkdir -p "$(dirname "$target")"
203
- defang completion $CURRENT_SHELL > "$target"
196
+ defang completion $shell > "$target"
204
197
}
205
198
206
199
# Usage: _install_completion_script "shell"
207
200
_install_completion_script() {
208
- local shell=$1
201
+ local shell=$(basename "$1")
209
202
local profile_file=".${shell}rc"
210
203
case $shell in
211
204
bash)
212
- _generate_completion_script "$HOME/.local/share/bash-completion.d/defang" || return 11
213
- _prompt_and_append_to_profile "Can we add shell completions to" "$HOME/$profile_file" "source \$HOME/.local/share/bash-completion.d/defang"
205
+ _generate_completion_script $shell "$HOME/.local/share/bash-completion.d/defang" || return 11
206
+ if [[ -z "$BASH_COMPLETION_VERSINFO" ]]; then
207
+ echo "Warning: bash completions require package bash-completion to be installed."
208
+ return 14
209
+ fi
210
+ _prompt_and_append_to_file "Can we add shell completions to" "$HOME/$profile_file" "source /etc/bash_completion"
214
211
;;
215
212
zsh)
216
- _generate_completion_script "$HOME/.local/share/zsh/site-functions/_defang" || return 12
213
+ _generate_completion_script $shell "$HOME/.local/share/zsh/site-functions/_defang" || return 12
217
214
if [[ ":$FPATH:" != *":$HOME/.local/share/zsh/site_functions:"* ]]; then
218
- _prompt_and_append_to_profile "Can we add shell completions to" "$HOME/$profile_file" "fpath=(\$HOME/.local/share/zsh/site-functions \$fpath)"
215
+ _prompt_and_append_to_file "Can we add shell completions to" "$HOME/$profile_file" "fpath=(\$HOME/.local/share/zsh/site-functions \$fpath)"
219
216
fi
220
217
;;
221
218
*) return 13 ;;
222
219
esac
223
220
}
224
221
225
- _install_completion_script $CURRENT_SHELL
226
- [[ $0 != $CURRENT_SHELL ]] && _install_completion_script $0
222
+ _install_completion_script $SHELL
223
+ [[ $0 != $SHELL ]] && _install_completion_script $0
227
224
228
225
# Cleanup: Remove the temporary directory
229
226
echo "Cleaning up..."
@@ -233,3 +230,7 @@ rm -r "$EXTRACT_DIR"
233
230
rm "$FILENAME"
234
231
235
232
echo "Installation completed. You can now use defang by typing '$BINARY_NAME' in the terminal."
233
+
234
+ # Unset the variables and functions to avoid polluting the user's environment
235
+ unset EXTRACT_DIR DOWNLOAD_URL RELEASE_JSON ARCH_SUFFIX ARCH OS FILENAME INSTALL_DIR BINARY_NAME REPLY EXPORT_PATH CURRENT_SHELL FOUND_PROFILE_FILE
236
+ unset -f _prompt_and_append_to_file _generate_completion_script _install_completion_script
0 commit comments