Skip to content

Commit

Permalink
feat: use shell_wrapper for all v2 tools (#354)
Browse files Browse the repository at this point in the history
* feat: use shell_wrapper for all v2 tools

* fix: only change permissions if we own the file
  • Loading branch information
Chumper authored Apr 5, 2022
1 parent 37610e3 commit d7b0d29
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/tools/v2/flux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ function link_tool () {
local versioned_tool_path
versioned_tool_path=$(find_versioned_tool_path)

link_wrapper "${TOOL_NAME}" "${versioned_tool_path}"
shell_wrapper "${TOOL_NAME}" "${versioned_tool_path}"
flux -v
}
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/tools/v2/helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ function link_tool () {
local versioned_tool_path
versioned_tool_path=$(find_versioned_tool_path)

link_wrapper "${TOOL_NAME}" "${versioned_tool_path}/bin"
shell_wrapper "${TOOL_NAME}" "${versioned_tool_path}/bin"
helm version
}
30 changes: 18 additions & 12 deletions src/usr/local/buildpack/utils/linking.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

# use this if custom env is required, creates a shell wrapper to /usr/local/bin
function shell_wrapper () {
local FILE
FILE="$(get_bin_path)/${1}"
check_command "$1"
cat > "$FILE" <<- EOM
local TARGET
local SOURCE=$2
TARGET="$(get_bin_path)/${1}"
if [[ -z "$SOURCE" ]]; then
SOURCE=$(command -v "${1}")
fi
if [[ -d "$SOURCE" ]]; then
SOURCE=$SOURCE/${1}
fi
check SOURCE true
check_command "$SOURCE"

cat > "$TARGET" <<- EOM
#!/bin/bash
if [[ -r "$ENV_FILE" && -z "${BUILDPACK+x}" ]]; then
. $ENV_FILE
fi
if [[ "\$(command -v ${1})" == "$FILE" ]]; then
echo Could not forward ${1}, probably wrong PATH variable. >&2
echo PATH=\$PATH
exit 1
fi
${1} "\$@"
${SOURCE} "\$@"
EOM
# make it writable for the owner and the group
chmod 775 "$FILE"
if [[ -O "$TARGET" ]] && [ "$(stat --format '%a' "${TARGET}")" -ne 775 ] ; then
# make it writable for the owner and the group only if we are the owner
chmod 775 "$TARGET"
fi
}

# use this for simple symlink to /usr/local/bin
Expand Down
4 changes: 2 additions & 2 deletions test/bash/linking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ teardown() {

run shell_wrapper
assert_failure
assert_output --partial "No defined"
assert_output --partial "param SOURCE is set but empty"

run shell_wrapper foo
assert_failure
assert_output --partial "No foo defined"
assert_output --partial "param SOURCE is set but empty"

run shell_wrapper ls
assert_success
Expand Down

0 comments on commit d7b0d29

Please sign in to comment.