Skip to content

Commit e164f0f

Browse files
committed
fix: refactor package parsing and fix command execution
**Problem:** - Package installation failed with "invalid char ' '" errors due to improper whitespace handling - Complex sed regex patterns were fragile and hard to maintain - Duplicated environment setup code for go install vs go get **Solution:** - Simplified comment parsing using bash pattern matching for full-line comments - Added explicit whitespace trimming with xargs after removing inline comments - Consolidated go command execution using array for proper argument handling - Use separate variable for package installation to avoid mutating display name **Changes:** - Replace complex sed patterns with simpler, more robust parsing logic - Use bash array for go_cmd to properly handle "get -u" as separate arguments - Add stderr redirection (2>&1) for proper error capture - Fix typo: "donwload" -> "download" - Consolidate duplicate GOROOT/GOPATH/PATH setup **Impact:** - Fixes installation failures for packages in .default-golang-pkgs - Supports comments and various whitespace formatting in package files - More maintainable code
1 parent 50c8f58 commit e164f0f

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

bin/install

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,36 @@ install_default_go_pkgs() {
3434
if [ ! -f "$default_go_pkgs" ]; then return; fi
3535

3636
while read -r line; do
37-
name=$(echo "$line" |
38-
sed 's|\(.*\) //.*$|\1|' |
39-
sed -E 's|^[[:space:]]*//.*||') # the first sed is for comments after package names, the second for full line comments
40-
41-
# Skip empty lines
42-
if [ -z "$name" ]; then continue; fi
37+
# Skip empty lines and comments
38+
[[ -z "$line" || "$line" =~ ^[[:space:]]*// ]] && continue
39+
40+
# Remove inline comments and trim whitespace
41+
name=$(echo "$line" | sed 's|//.*||' | xargs)
42+
43+
# Skip if empty after processing
44+
[ -z "$name" ] && continue
4345

4446
echo -ne "\nInstalling \033[33m${name}\033[39m go pkg... " >&2
4547

46-
# if using go > 1.16 then use go install as the preferred donwload path
48+
# if using go > 1.16 then use go install as the preferred download path
4749
if [ "$go_major_version" -ge 2 ] || [ "${go_minor_version//[!0-9]*/}" -ge 16 ]; then
50+
go_cmd=(install)
4851
if [[ $name != *"@"* ]]; then
49-
name="${name}@latest"
52+
pkg_to_install="${name}@latest"
53+
else
54+
pkg_to_install="${name}"
5055
fi
51-
52-
GOROOT="$ASDF_INSTALL_PATH/go" \
53-
GOPATH="$ASDF_INSTALL_PATH/packages" \
54-
GOBIN="$ASDF_INSTALL_PATH/bin" \
55-
PATH="$go_path:$PATH" \
56-
go install "$name" >/dev/null && rc=$? || rc=$?
5756
else
58-
GOROOT="$ASDF_INSTALL_PATH/go" \
59-
GOPATH="$ASDF_INSTALL_PATH/packages" \
60-
PATH="$go_path:$PATH" \
61-
GOBIN="$ASDF_INSTALL_PATH/bin" \
62-
go get -u "$name" >/dev/null && rc=$? || rc=$?
57+
go_cmd=(get -u)
58+
pkg_to_install="${name}"
6359
fi
6460

65-
if [[ $rc -eq 0 ]]; then
61+
# Execute the go command with common environment
62+
if GOROOT="$ASDF_INSTALL_PATH/go" \
63+
GOPATH="$ASDF_INSTALL_PATH/packages" \
64+
GOBIN="$ASDF_INSTALL_PATH/bin" \
65+
PATH="$go_path:$PATH" \
66+
go "${go_cmd[@]}" "$pkg_to_install" >/dev/null 2>&1; then
6667
msg "SUCCESS"
6768
else
6869
err "FAIL"
@@ -71,4 +72,4 @@ install_default_go_pkgs() {
7172
}
7273

7374
install_golang "$ASDF_INSTALL_VERSION" "${ASDF_DOWNLOAD_PATH:-}" "$ASDF_INSTALL_PATH"
74-
install_default_go_pkgs "$ASDF_INSTALL_PATH" "$ASDF_INSTALL_VERSION"
75+
install_default_go_pkgs "$ASDF_INSTALL_PATH" "$ASDF_INSTALL_VERSION"

0 commit comments

Comments
 (0)