refactor: extract ensure_multi_credentials to reduce duplication across 5 providers (#468)

Add a generic ensure_multi_credentials() helper to shared/common.sh that
handles the env-var/config-file/prompt/test/save flow for providers needing
multiple credentials. This eliminates ~270 lines of duplicated logic across
contabo, netcup, ramnode, ionos, and upcloud, replacing it with single
function calls.

Each provider's ensure_*_credentials() function is now 3-8 lines instead
of 30-65 lines.

Agent: complexity-hunter

Co-authored-by: A <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
A 2026-02-11 07:48:32 -08:00 committed by GitHub
parent e38a75ea17
commit 79e3b887c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 144 additions and 273 deletions

View file

@ -124,51 +124,11 @@ test_netcup_credentials() {
# Ensure Netcup credentials are available
ensure_netcup_credentials() {
local config_file="$HOME/.config/spawn/netcup.json"
# Try loading from env vars first
if [[ -n "${NETCUP_CUSTOMER_NUMBER:-}" && -n "${NETCUP_API_KEY:-}" && -n "${NETCUP_API_PASSWORD:-}" ]]; then
if test_netcup_credentials; then
return 0
fi
fi
# Try loading from config file (single python3 call instead of 3)
local creds
if creds=$(_load_json_config_fields "$config_file" customer_number api_key api_password); then
local saved_num saved_key saved_pass
{ read -r saved_num; read -r saved_key; read -r saved_pass; } <<< "${creds}"
if [[ -n "$saved_num" ]] && [[ -n "$saved_key" ]] && [[ -n "$saved_pass" ]]; then
log_info "Loading Netcup credentials from $config_file"
export NETCUP_CUSTOMER_NUMBER="$saved_num" NETCUP_API_KEY="$saved_key" NETCUP_API_PASSWORD="$saved_pass"
if test_netcup_credentials; then
return 0
fi
fi
fi
# Prompt for credentials
log_info "Netcup credentials not found"
log_info "Get your API credentials at: https://ccp.netcup.net/ → Settings → API"
log_info ""
NETCUP_CUSTOMER_NUMBER=$(safe_read "Enter Netcup customer number: ") || return 1
NETCUP_API_KEY=$(safe_read "Enter Netcup API key: ") || return 1
NETCUP_API_PASSWORD=$(safe_read "Enter Netcup API password: ") || return 1
export NETCUP_CUSTOMER_NUMBER NETCUP_API_KEY NETCUP_API_PASSWORD
# Test credentials
if ! test_netcup_credentials; then
log_error "Invalid Netcup credentials"
return 1
fi
_save_json_config "$config_file" \
customer_number "$NETCUP_CUSTOMER_NUMBER" \
api_key "$NETCUP_API_KEY" \
api_password "$NETCUP_API_PASSWORD"
return 0
ensure_multi_credentials "Netcup" "$HOME/.config/spawn/netcup.json" \
"https://ccp.netcup.net/ -> Settings -> API" test_netcup_credentials \
"NETCUP_CUSTOMER_NUMBER:customer_number:Customer Number" \
"NETCUP_API_KEY:api_key:API Key" \
"NETCUP_API_PASSWORD:api_password:API Password"
}
# Check if SSH key is registered with Netcup