Skip to content

Commit 7bb2add

Browse files
committed
fixes for bash
1 parent a817b07 commit 7bb2add

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

src/bin/install

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ if [[ "$1" == "-y" ]] || [[ "$CI" == "1" ]] || [[ "$CI" == "true" ]]; then
2424
CI="1"
2525
fi
2626

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-
3027
# Use curl to fetch the latest release data
3128
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)
3330

3431
# Check for curl failure
3532
if [ $? -ne 0 ]; then
@@ -131,8 +128,8 @@ if ! chmod +x "$INSTALL_DIR/$BINARY_NAME"; then
131128
return 10
132129
fi
133130

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() {
136133
local prompt=$1
137134
local profile_file=$2
138135
local line=$3
@@ -163,9 +160,6 @@ _prompt_and_append_to_profile() {
163160
fi
164161
}
165162

166-
# Get the name of the current shell
167-
CURRENT_SHELL=$(basename "$SHELL")
168-
169163
# Add the installation directory to PATH if not already present
170164
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
171165
echo "Adding $INSTALL_DIR to your PATH for this session."
@@ -174,56 +168,59 @@ if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
174168
echo "Adding $INSTALL_DIR to your PATH for future sessions."
175169
EXPORT_PATH="export PATH=\"\$PATH:$INSTALL_DIR\""
176170

177-
# Define the possible shell profile files
178-
PROFILE_FILES=(".bashrc" ".zshrc" ".kshrc")
179-
180171
# Loop over the possible profile files
181172
FOUND_PROFILE_FILE=false
182-
for profile_file in "${PROFILE_FILES[@]}"; do
173+
for CURRENT_SHELL in bash zsh ksh; do
183174
# 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
185176
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"
187178
fi
188179
done
189180

190181
# If no profile file was found
191182
if [[ $FOUND_PROFILE_FILE == false ]]; then
183+
# Get the name of the current shell
184+
CURRENT_SHELL="$(basename "${SHELL:-$0}")"
192185
# 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"
195187
fi
196188
fi
197189

198-
# Usage: _generate_completion_script "path/to/completion/_script"
190+
# Usage: _generate_completion_script "shell" "path/to/completion/_script"
199191
_generate_completion_script() {
200-
local target=$1
192+
local shell=$1
193+
local target=$2
201194
echo "Generating completion script at $target"
202195
mkdir -p "$(dirname "$target")"
203-
defang completion $CURRENT_SHELL > "$target"
196+
defang completion $shell > "$target"
204197
}
205198

206199
# Usage: _install_completion_script "shell"
207200
_install_completion_script() {
208-
local shell=$1
201+
local shell=$(basename "$1")
209202
local profile_file=".${shell}rc"
210203
case $shell in
211204
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"
214211
;;
215212
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
217214
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)"
219216
fi
220217
;;
221218
*) return 13 ;;
222219
esac
223220
}
224221

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
227224

228225
# Cleanup: Remove the temporary directory
229226
echo "Cleaning up..."
@@ -233,3 +230,7 @@ rm -r "$EXTRACT_DIR"
233230
rm "$FILENAME"
234231

235232
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

Comments
 (0)