refactor: check exit codes directly instead of using $?

Fixed SC2181 warnings by refactoring indirect exit code checks
($? -ne 0) to direct command checks (if ! command). This improves
readability and follows shellcheck best practices.

Changes:
- aws-lightsail/lib/common.sh: Refactored aws lightsail create-instances check
- gcp/lib/common.sh: Refactored gcloud compute instances create check

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Sprite 2026-02-08 01:28:27 +00:00
parent 39ee858943
commit f883ff42e8
2 changed files with 8 additions and 9 deletions

View file

@ -96,16 +96,14 @@ create_server() {
local userdata
userdata=$(get_cloud_init_userdata)
aws lightsail create-instances \
if ! aws lightsail create-instances \
--instance-names "$name" \
--availability-zone "$az" \
--blueprint-id "$blueprint" \
--bundle-id "$bundle" \
--key-pair-name "spawn-key" \
--user-data "$userdata" \
>/dev/null
if [[ $? -ne 0 ]]; then
>/dev/null; then
log_error "Failed to create Lightsail instance"
return 1
fi

View file

@ -92,7 +92,7 @@ create_server() {
local username
username=$(whoami)
gcloud compute instances create "$name" \
if ! gcloud compute instances create "$name" \
--zone="$zone" \
--machine-type="$machine_type" \
--image-family="$image_family" \
@ -100,9 +100,7 @@ create_server() {
--metadata="startup-script=$userdata,ssh-keys=${username}:${pub_key}" \
--project="$GCP_PROJECT" \
--quiet \
>/dev/null 2>&1
if [[ $? -ne 0 ]]; then
>/dev/null 2>&1; then
log_error "Failed to create GCP instance"
return 1
fi
@ -127,7 +125,7 @@ verify_server_connectivity() {
log_warn "Waiting for SSH connectivity to $ip..."
while [[ $attempt -le $max_attempts ]]; do
# SSH_OPTS is defined in shared/common.sh
# shellcheck disable=SC2154
# shellcheck disable=SC2154,SC2086
if ssh $SSH_OPTS -o ConnectTimeout=5 "${username}@$ip" "echo ok" >/dev/null 2>&1; then
log_info "SSH connection established"; return 0
fi
@ -142,6 +140,7 @@ wait_for_cloud_init() {
username=$(whoami)
log_warn "Waiting for startup script to complete..."
while [[ $attempt -le $max_attempts ]]; do
# shellcheck disable=SC2086
if ssh $SSH_OPTS "${username}@$ip" "test -f /tmp/.cloud-init-complete" >/dev/null 2>&1; then
log_info "Startup script completed"; return 0
fi
@ -155,6 +154,7 @@ run_server() {
local ip="$1" cmd="$2"
local username
username=$(whoami)
# shellcheck disable=SC2086
ssh $SSH_OPTS "${username}@$ip" "$cmd"
}
@ -162,6 +162,7 @@ upload_file() {
local ip="$1" local_path="$2" remote_path="$3"
local username
username=$(whoami)
# shellcheck disable=SC2086
scp $SSH_OPTS "$local_path" "${username}@$ip:$remote_path"
}