mirror of
				https://github.com/appleboy/ssh-action.git
				synced 2025-10-31 16:23:52 +00:00 
			
		
		
		
	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 <appleboy.tw@gmail.com>
This commit is contained in:
		
							parent
							
								
									52a1840ca6
								
							
						
					
					
						commit
						b6690ee817
					
				| @ -10,51 +10,25 @@ GITHUB_ACTION_PATH="${GITHUB_ACTION_PATH%/}" | |||||||
| DRONE_SSH_RELEASE_URL="${DRONE_SSH_RELEASE_URL:-https://github.com/appleboy/drone-ssh/releases/download}" | DRONE_SSH_RELEASE_URL="${DRONE_SSH_RELEASE_URL:-https://github.com/appleboy/drone-ssh/releases/download}" | ||||||
| DRONE_SSH_VERSION="${DRONE_SSH_VERSION:-1.8.1}" | DRONE_SSH_VERSION="${DRONE_SSH_VERSION:-1.8.1}" | ||||||
| 
 | 
 | ||||||
| function detect_client_info() { | function log_error() { | ||||||
|   if [ -n "${SSH_CLIENT_OS-}" ]; then |   echo "$1" >&2 | ||||||
|     CLIENT_PLATFORM="${SSH_CLIENT_OS}" |   exit "$2" | ||||||
|   else | } | ||||||
|     local kernel |  | ||||||
|     kernel="$(uname -s)" |  | ||||||
|     case "${kernel}" in |  | ||||||
|     Darwin) |  | ||||||
|       CLIENT_PLATFORM="darwin" |  | ||||||
|       ;; |  | ||||||
|     Linux) |  | ||||||
|       CLIENT_PLATFORM="linux" |  | ||||||
|       ;; |  | ||||||
|     Windows) |  | ||||||
|       CLIENT_PLATFORM="windows" |  | ||||||
|       ;; |  | ||||||
|     *) |  | ||||||
|       echo "Unknown, unsupported platform: ${kernel}." >&2 |  | ||||||
|       echo "Supported platforms: Linux, Darwin and Windows." >&2 |  | ||||||
|       echo "Bailing out." >&2 |  | ||||||
|       exit 2 |  | ||||||
|       ;; |  | ||||||
|     esac |  | ||||||
|   fi |  | ||||||
| 
 | 
 | ||||||
|   if [ -n "${SSH_CLIENT_ARCH-}" ]; then | function detect_client_info() { | ||||||
|     CLIENT_ARCH="${SSH_CLIENT_ARCH}" |   CLIENT_PLATFORM="${SSH_CLIENT_OS:-$(uname -s | tr '[:upper:]' '[:lower:]')}" | ||||||
|   else |   CLIENT_ARCH="${SSH_CLIENT_ARCH:-$(uname -m)}" | ||||||
|     local machine | 
 | ||||||
|     machine="$(uname -m)" |   case "${CLIENT_PLATFORM}" in | ||||||
|     case "${machine}" in |   darwin | linux | windows) ;; | ||||||
|     x86_64* | i?86_64* | amd64*) |   *) log_error "Unknown, unsupported platform: ${CLIENT_PLATFORM}. Supported platforms: Linux, Darwin, and Windows." 2 ;; | ||||||
|       CLIENT_ARCH="amd64" |   esac | ||||||
|       ;; | 
 | ||||||
|     aarch64* | arm64*) |   case "${CLIENT_ARCH}" in | ||||||
|       CLIENT_ARCH="arm64" |   x86_64* | i?86_64* | amd64*) CLIENT_ARCH="amd64" ;; | ||||||
|       ;; |   aarch64* | arm64*) CLIENT_ARCH="arm64" ;; | ||||||
|     *) |   *) log_error "Unknown, unsupported architecture: ${CLIENT_ARCH}. Supported architectures: x86_64, i686, arm64." 3 ;; | ||||||
|       echo "Unknown, unsupported architecture (${machine})." >&2 |   esac | ||||||
|       echo "Supported architectures x86_64, i686, arm64." >&2 |  | ||||||
|       echo "Bailing out." >&2 |  | ||||||
|       exit 3 |  | ||||||
|       ;; |  | ||||||
|     esac |  | ||||||
|   fi |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| detect_client_info | detect_client_info | ||||||
| @ -62,16 +36,18 @@ DOWNLOAD_URL_PREFIX="${DRONE_SSH_RELEASE_URL}/v${DRONE_SSH_VERSION}" | |||||||
| CLIENT_BINARY="drone-ssh-${DRONE_SSH_VERSION}-${CLIENT_PLATFORM}-${CLIENT_ARCH}" | CLIENT_BINARY="drone-ssh-${DRONE_SSH_VERSION}-${CLIENT_PLATFORM}-${CLIENT_ARCH}" | ||||||
| TARGET="${GITHUB_ACTION_PATH}/${CLIENT_BINARY}" | TARGET="${GITHUB_ACTION_PATH}/${CLIENT_BINARY}" | ||||||
| echo "Will download ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}" | echo "Will download ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}" | ||||||
| curl -fsSL --retry 5 --keepalive-time 2 "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o ${TARGET} | curl -fsSL --retry 5 --keepalive-time 2 "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o "${TARGET}" | ||||||
| chmod +x ${TARGET} | chmod +x "${TARGET}" | ||||||
| 
 | 
 | ||||||
| echo "======= CLI Version =======" | echo "======= CLI Version =======" | ||||||
| sh -c "${TARGET} --version" # print version | "${TARGET}" --version | ||||||
| echo "===========================" | echo "===========================" | ||||||
| if [[ "$INPUT_CAPTURE_STDOUT" == 'true' ]]; then | if [[ "${INPUT_CAPTURE_STDOUT}" == 'true' ]]; then | ||||||
|   echo 'stdout<<EOF' >>$GITHUB_OUTPUT          # use heredoc for multiline output |   { | ||||||
|   sh -c "${TARGET} $*" | tee -a $GITHUB_OUTPUT # run the command |     echo 'stdout<<EOF' | ||||||
|   echo 'EOF' >>$GITHUB_OUTPUT |     "${TARGET}" "$@" | tee -a "${GITHUB_OUTPUT}" | ||||||
|  |     echo 'EOF' | ||||||
|  |   } >>"${GITHUB_OUTPUT}" | ||||||
| else | else | ||||||
|   sh -c "${TARGET} $*" # run the command |   "${TARGET}" "$@" | ||||||
| fi | fi | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user