|
3 | 3 | # Recursively unwrap itself from the path, eventually calling the target binary's |
4 | 4 | # `run-<target>.sh` script, which will inject a feed authentication token. |
5 | 5 |
|
6 | | -TARGET_BIN_NAME=TARGET_BIN_NAME |
| 6 | +TARGET_BIN_NAME=SUBST_TARGET_BIN_NAME |
| 7 | +TARGET_WRAPPER_PATH=SUBST_TARGET_WRAPPER_PATH |
7 | 8 |
|
8 | 9 | log() { |
9 | | - # Print to stderr to avoid interfering with scripts that capture stdout. |
10 | | - # Prefix error messages so the user can quickly determine that it's from our |
11 | | - # artifacts-helper wrapper, not an error originating from the wrapped binary. |
12 | | - printf >&2 "[codespace-features/artifacts-helper] %s" "$1" |
| 10 | + # Print to stderr to avoid interfering with scripts that capture stdout. |
| 11 | + # Prefix error messages so the user can quickly determine that it's from our |
| 12 | + # artifacts-helper wrapper, not an error originating from the wrapped binary. |
| 13 | + printf >&2 "[codespace-features/artifacts-helper] %s" "$1" |
13 | 14 | } |
14 | 15 |
|
15 | 16 | log_line() { |
16 | | - log "$1 |
| 17 | + log "$1 |
17 | 18 | " |
18 | 19 | } |
19 | 20 |
|
| 21 | +log_debug_line() { |
| 22 | + if [ "$ARTIFACTS_HELPER_DEBUG" == "1" ]; then |
| 23 | + log_line "$1" |
| 24 | + fi |
| 25 | +} |
| 26 | + |
20 | 27 | call_wrapped_bin() { |
21 | | - CURRENT_SCRIPT_PATH=$(realpath "${BASH_SOURCE[0]}") |
22 | | - if [ -z "$CURRENT_SCRIPT_PATH" ]; then |
23 | | - log_line "Error: Current script path could not be determined! This will result in infinite recursion. Bailing early." |
24 | | - exit 1 |
25 | | - fi |
26 | | - |
27 | | - # Find the next executable from the PATH order, which would be shadowed by this wrapper |
28 | | - if [ -z "$ARTIFACTS_HELPER_TARGET_BIN_PATHS" ]; then |
29 | | - ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(which -a "$TARGET_BIN_NAME") |
30 | | - fi |
31 | | - |
32 | | - ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(sed '/^[[:space:]]*$/d' <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS") |
33 | | - ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(grep --color=never -Fve "$CURRENT_SCRIPT_PATH" <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS") |
34 | | - NEXT_SHADOWED_BIN=$(head -n 1 <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS") |
35 | | - if [ -z "$NEXT_SHADOWED_BIN" ]; then |
36 | | - log_line "Error: The real $TARGET_BIN_NAME could not be found on PATH." |
37 | | - log_line "which -a $TARGET_BIN_NAME:\n%s\n" "$(which -a $TARGET_BIN_NAME)" |
38 | | - log_line "CC_SHADOWED_BINS:\n%s\n" "$ARTIFACTS_HELPER_TARGET_BIN_PATHS" |
39 | | - log_line "CURRENT_SCRIPT_PATH: %s\n" "$CURRENT_SCRIPT_PATH" |
40 | | - exit 1 |
41 | | - fi |
42 | | - |
43 | | - # Recursively removing the wrapper script(s) from the shadowed bin paths will |
44 | | - # allow us to account for circumstances where the wrapper script appears multiple |
45 | | - # times on the PATH. We will eventually descend to the real target binary. |
46 | | - export ARTIFACTS_HELPER_TARGET_BIN_PATHS |
47 | | - |
48 | | - log_line "Running: %s\n" "$NEXT_SHADOWED_BIN" |
49 | | - ${NEXT_SHADOWED_BIN} "$@" |
50 | | - return "$?" |
| 28 | + CURRENT_SCRIPT_PATH=$(realpath "${BASH_SOURCE[0]}") |
| 29 | + if [ -z "$CURRENT_SCRIPT_PATH" ]; then |
| 30 | + log_line "Error: Current script path could not be determined! This will result in infinite recursion. Bailing early." |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | + |
| 34 | + # Find the next executable from the PATH order, which would be shadowed by this wrapper |
| 35 | + if [ -z "$ARTIFACTS_HELPER_TARGET_BIN_PATHS" ]; then |
| 36 | + ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(which -a "$TARGET_BIN_NAME") |
| 37 | + fi |
| 38 | + |
| 39 | + ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(sed '/^[[:space:]]*$/d' <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS") |
| 40 | + ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(grep --color=never -Fve "$CURRENT_SCRIPT_PATH" <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS") |
| 41 | + NEXT_SHADOWED_BIN=$(head -n 1 <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS") |
| 42 | + |
| 43 | + log_debug_line "CURRENT_SCRIPT_PATH=$CURRENT_SCRIPT_PATH" |
| 44 | + log_debug_line "TARGET_BIN_NAME=$TARGET_BIN_NAME" |
| 45 | + log_debug_line "TARGET_WRAPPER_PATH=$TARGET_WRAPPER_PATH" |
| 46 | + log_debug_line "ARTIFACTS_HELPER_TARGET_BIN_PATHS=$ARTIFACTS_HELPER_TARGET_BIN_PATHS" |
| 47 | + log_debug_line "NEXT_SHADOWED_BIN=$NEXT_SHADOWED_BIN" |
| 48 | + |
| 49 | + if [ -z "$NEXT_SHADOWED_BIN" ]; then |
| 50 | + log_line "Error: The real $TARGET_BIN_NAME could not be found on PATH." |
| 51 | + log_line "which -a $TARGET_BIN_NAME:\n%s\n" "$(which -a $TARGET_BIN_NAME)" |
| 52 | + log_line "CC_SHADOWED_BINS:\n%s\n" "$ARTIFACTS_HELPER_TARGET_BIN_PATHS" |
| 53 | + log_line "CURRENT_SCRIPT_PATH: %s\n" "$CURRENT_SCRIPT_PATH" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + |
| 57 | + # Recursively removing the wrapper script(s) from the shadowed bin paths will |
| 58 | + # allow us to account for circumstances where the wrapper script appears multiple |
| 59 | + # times on the PATH. We will eventually descend to the real target binary. |
| 60 | + export ARTIFACTS_HELPER_TARGET_BIN_PATHS |
| 61 | + |
| 62 | + log_debug_line "Running: SUBST_EXE_ENV_VAR=$NEXT_SHADOWED_BIN $TARGET_WRAPPER_PATH" |
| 63 | + SUBST_EXE_ENV_VAR="$NEXT_SHADOWED_BIN" "$TARGET_WRAPPER_PATH" "$@" |
| 64 | + return "$?" |
51 | 65 | } |
52 | 66 |
|
53 | 67 | call_wrapped_bin "$@" |
54 | | -exit $? |
| 68 | +EXIT_CODE=$? |
| 69 | +exit $EXIT_CODE |
0 commit comments