Skip to content

Commit b6690ee

Browse files
authored
refactor: improve error handling and code readability across scripts (#374)
- Add `log_error` function for error handling - Simplify the detection of client platform and architecture - Use `log_error` for unsupported platform or architecture handling - Use consistent quoting for variable expansions - Improve readability for `curl` and `chmod` commands - Simplify the commands for running and capturing stdout Signed-off-by: appleboy <[email protected]>
1 parent 52a1840 commit b6690ee

File tree

1 file changed

+27
-51
lines changed

1 file changed

+27
-51
lines changed

entrypoint.sh

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,44 @@ GITHUB_ACTION_PATH="${GITHUB_ACTION_PATH%/}"
1010
DRONE_SSH_RELEASE_URL="${DRONE_SSH_RELEASE_URL:-https://github.com/appleboy/drone-ssh/releases/download}"
1111
DRONE_SSH_VERSION="${DRONE_SSH_VERSION:-1.8.1}"
1212

13+
function log_error() {
14+
echo "$1" >&2
15+
exit "$2"
16+
}
17+
1318
function detect_client_info() {
14-
if [ -n "${SSH_CLIENT_OS-}" ]; then
15-
CLIENT_PLATFORM="${SSH_CLIENT_OS}"
16-
else
17-
local kernel
18-
kernel="$(uname -s)"
19-
case "${kernel}" in
20-
Darwin)
21-
CLIENT_PLATFORM="darwin"
22-
;;
23-
Linux)
24-
CLIENT_PLATFORM="linux"
25-
;;
26-
Windows)
27-
CLIENT_PLATFORM="windows"
28-
;;
29-
*)
30-
echo "Unknown, unsupported platform: ${kernel}." >&2
31-
echo "Supported platforms: Linux, Darwin and Windows." >&2
32-
echo "Bailing out." >&2
33-
exit 2
34-
;;
35-
esac
36-
fi
19+
CLIENT_PLATFORM="${SSH_CLIENT_OS:-$(uname -s | tr '[:upper:]' '[:lower:]')}"
20+
CLIENT_ARCH="${SSH_CLIENT_ARCH:-$(uname -m)}"
21+
22+
case "${CLIENT_PLATFORM}" in
23+
darwin | linux | windows) ;;
24+
*) log_error "Unknown, unsupported platform: ${CLIENT_PLATFORM}. Supported platforms: Linux, Darwin, and Windows." 2 ;;
25+
esac
3726

38-
if [ -n "${SSH_CLIENT_ARCH-}" ]; then
39-
CLIENT_ARCH="${SSH_CLIENT_ARCH}"
40-
else
41-
local machine
42-
machine="$(uname -m)"
43-
case "${machine}" in
44-
x86_64* | i?86_64* | amd64*)
45-
CLIENT_ARCH="amd64"
46-
;;
47-
aarch64* | arm64*)
48-
CLIENT_ARCH="arm64"
49-
;;
50-
*)
51-
echo "Unknown, unsupported architecture (${machine})." >&2
52-
echo "Supported architectures x86_64, i686, arm64." >&2
53-
echo "Bailing out." >&2
54-
exit 3
55-
;;
56-
esac
57-
fi
27+
case "${CLIENT_ARCH}" in
28+
x86_64* | i?86_64* | amd64*) CLIENT_ARCH="amd64" ;;
29+
aarch64* | arm64*) CLIENT_ARCH="arm64" ;;
30+
*) log_error "Unknown, unsupported architecture: ${CLIENT_ARCH}. Supported architectures: x86_64, i686, arm64." 3 ;;
31+
esac
5832
}
5933

6034
detect_client_info
6135
DOWNLOAD_URL_PREFIX="${DRONE_SSH_RELEASE_URL}/v${DRONE_SSH_VERSION}"
6236
CLIENT_BINARY="drone-ssh-${DRONE_SSH_VERSION}-${CLIENT_PLATFORM}-${CLIENT_ARCH}"
6337
TARGET="${GITHUB_ACTION_PATH}/${CLIENT_BINARY}"
6438
echo "Will download ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}"
65-
curl -fsSL --retry 5 --keepalive-time 2 "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o ${TARGET}
66-
chmod +x ${TARGET}
39+
curl -fsSL --retry 5 --keepalive-time 2 "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o "${TARGET}"
40+
chmod +x "${TARGET}"
6741

6842
echo "======= CLI Version ======="
69-
sh -c "${TARGET} --version" # print version
43+
"${TARGET}" --version
7044
echo "==========================="
71-
if [[ "$INPUT_CAPTURE_STDOUT" == 'true' ]]; then
72-
echo 'stdout<<EOF' >>$GITHUB_OUTPUT # use heredoc for multiline output
73-
sh -c "${TARGET} $*" | tee -a $GITHUB_OUTPUT # run the command
74-
echo 'EOF' >>$GITHUB_OUTPUT
45+
if [[ "${INPUT_CAPTURE_STDOUT}" == 'true' ]]; then
46+
{
47+
echo 'stdout<<EOF'
48+
"${TARGET}" "$@" | tee -a "${GITHUB_OUTPUT}"
49+
echo 'EOF'
50+
} >>"${GITHUB_OUTPUT}"
7551
else
76-
sh -c "${TARGET} $*" # run the command
52+
"${TARGET}" "$@"
7753
fi

0 commit comments

Comments
 (0)