diff --git a/aws/lib/common.sh b/aws/lib/common.sh index 97e27d56..f8c804c2 100644 --- a/aws/lib/common.sh +++ b/aws/lib/common.sh @@ -266,7 +266,7 @@ list_servers() { # Cloud adapter interface # ============================================================ -cloud_authenticate() { ensure_aws_cli; ensure_ssh_key; } +cloud_authenticate() { prompt_spawn_name; ensure_aws_cli; ensure_ssh_key; } cloud_provision() { create_server "$1"; } cloud_wait_ready() { verify_server_connectivity "${LIGHTSAIL_SERVER_IP}"; wait_for_cloud_init "${LIGHTSAIL_SERVER_IP}" 60; } cloud_run() { run_server "${LIGHTSAIL_SERVER_IP}" "$1"; } diff --git a/daytona/lib/common.sh b/daytona/lib/common.sh index 7f83778f..3dc49131 100644 --- a/daytona/lib/common.sh +++ b/daytona/lib/common.sh @@ -325,7 +325,7 @@ list_servers() { # Cloud adapter interface # ============================================================ -cloud_authenticate() { ensure_daytona_cli; ensure_daytona_token; } +cloud_authenticate() { prompt_spawn_name; ensure_daytona_cli; ensure_daytona_token; } cloud_provision() { create_server "$1"; } cloud_wait_ready() { wait_for_cloud_init; } cloud_run() { run_server "$1"; } diff --git a/digitalocean/lib/common.sh b/digitalocean/lib/common.sh index 5e5c3868..9f134b84 100755 --- a/digitalocean/lib/common.sh +++ b/digitalocean/lib/common.sh @@ -259,7 +259,7 @@ for d in droplets: # Cloud adapter interface # ============================================================ -cloud_authenticate() { register_cleanup_trap; ensure_do_token; ensure_ssh_key; } +cloud_authenticate() { prompt_spawn_name; register_cleanup_trap; ensure_do_token; ensure_ssh_key; } cloud_provision() { create_server "$1"; } cloud_wait_ready() { verify_server_connectivity "${DO_SERVER_IP}"; wait_for_cloud_init "${DO_SERVER_IP}" 60; } cloud_run() { run_server "${DO_SERVER_IP}" "$1"; } diff --git a/fly/lib/common.sh b/fly/lib/common.sh index ba490b71..3fc8b3a4 100644 --- a/fly/lib/common.sh +++ b/fly/lib/common.sh @@ -486,7 +486,7 @@ for a in apps: # Cloud adapter interface # ============================================================ -cloud_authenticate() { ensure_fly_cli; ensure_fly_token; } +cloud_authenticate() { prompt_spawn_name; ensure_fly_cli; ensure_fly_token; } cloud_provision() { create_server "$1"; } cloud_wait_ready() { wait_for_cloud_init; } cloud_run() { run_server "$1"; } diff --git a/gcp/lib/common.sh b/gcp/lib/common.sh index 08578cd9..bc9a4599 100644 --- a/gcp/lib/common.sh +++ b/gcp/lib/common.sh @@ -145,15 +145,25 @@ _gcp_pick_project() { interactive_pick "GCP_PROJECT" "" "GCP projects" _gcp_project_options } -# Resolve and export GCP_PROJECT — prompt interactively if not already set +# Resolve and export GCP_PROJECT — confirm existing or pick interactively _gcp_resolve_project() { # Check env var and gcloud config local project="${GCP_PROJECT:-$(gcloud config get-value project 2>/dev/null)}" if [[ "${project}" == "(unset)" ]]; then project=""; fi - # If not set, offer an interactive project picker + # When a project is already set, confirm before using it + if [[ -n "${project}" && "${SPAWN_NON_INTERACTIVE:-}" != "1" ]]; then + local confirm + confirm=$(safe_read "Use project '${project}'? [Y/n]: ") || confirm="" + confirm="${confirm:-y}" + if [[ "${confirm}" =~ ^[nN] ]]; then + project="" + fi + fi + + # If not set (or user chose to change), offer an interactive project picker if [[ -z "${project}" ]]; then - log_info "No GCP project configured — fetching your projects..." + log_info "Fetching your GCP projects..." project=$(_gcp_pick_project) fi @@ -373,7 +383,7 @@ list_servers() { # Cloud adapter interface # ============================================================ -cloud_authenticate() { ensure_gcloud; ensure_ssh_key; } +cloud_authenticate() { prompt_spawn_name; ensure_gcloud; ensure_ssh_key; } cloud_provision() { create_server "$1"; } cloud_wait_ready() { verify_server_connectivity "${GCP_SERVER_IP}"; wait_for_cloud_init "${GCP_SERVER_IP}" 60; } cloud_run() { run_server "${GCP_SERVER_IP}" "$1"; } diff --git a/hetzner/lib/common.sh b/hetzner/lib/common.sh index 8229eaf4..ebdfd54f 100755 --- a/hetzner/lib/common.sh +++ b/hetzner/lib/common.sh @@ -650,7 +650,7 @@ list_servers() { # Cloud adapter interface # ============================================================ -cloud_authenticate() { ensure_hcloud_token; ensure_ssh_key; } +cloud_authenticate() { prompt_spawn_name; ensure_hcloud_token; ensure_ssh_key; } cloud_provision() { local exit_code=0 create_server "$1" || exit_code=$? diff --git a/shared/common.sh b/shared/common.sh index 905fecc7..d2ef4855 100644 --- a/shared/common.sh +++ b/shared/common.sh @@ -511,6 +511,46 @@ validated_read() { done } +# Convert a display name to a valid kebab-case resource identifier. +# "My Dev Box" → "my-dev-box" "Claude 2024!" → "claude-2024" +_to_kebab_case() { + printf '%s' "${1}" \ + | tr '[:upper:]' '[:lower:]' \ + | sed 's/[^a-z0-9-]/-/g' \ + | sed 's/-\{2,\}/-/g' \ + | sed 's/^-//;s/-$//' +} + +# Ask for a human-readable spawn name upfront, then derive a kebab-case +# default used for resource naming on every cloud. +# Idempotent — safe to call multiple times; skips prompt if already done. +# Respects SPAWN_NAME when set by the CLI (e.g. spawn gcp claude --name "My Box"). +# Exports: SPAWN_NAME_DISPLAY, SPAWN_NAME_KEBAB +prompt_spawn_name() { + # Already prompted this session — nothing to do + if [[ -n "${SPAWN_NAME_KEBAB:-}" ]]; then + return 0 + fi + + local display_name + if [[ -n "${SPAWN_NAME:-}" ]]; then + display_name="${SPAWN_NAME}" + log_info "Spawn name: ${display_name}" + else + echo "" >&2 + display_name=$(safe_read 'Spawn name (e.g. "My Dev Box"): ') || display_name="" + [[ -z "${display_name}" ]] && display_name="spawn" + fi + + local kebab + kebab=$(_to_kebab_case "${display_name}") + [[ -z "${kebab}" ]] && kebab="spawn" + + export SPAWN_NAME_DISPLAY="${display_name}" + export SPAWN_NAME_KEBAB="${kebab}" + log_info "Resource name: ${kebab}" +} + # Generic function to get resource name from environment or prompt # Usage: get_resource_name ENV_VAR_NAME PROMPT_TEXT # Returns: Resource name via stdout @@ -520,22 +560,24 @@ get_resource_name() { local prompt_text="${2}" local resource_value="${!env_var_name}" - # First check platform-specific env var + # Platform-specific env var takes absolute precedence if [[ -n "${resource_value}" ]]; then log_info "Using ${prompt_text%:*} from environment: ${resource_value}" echo "${resource_value}" return 0 fi - # Then check for SPAWN_NAME (set by CLI) - if [[ -n "${SPAWN_NAME:-}" ]]; then - log_info "Using spawn name: ${SPAWN_NAME}" - echo "${SPAWN_NAME}" - return 0 + # Show spawn name kebab as a pre-filled default (press Enter to accept) + local default_name="${SPAWN_NAME_KEBAB:-}" + local effective_prompt="${prompt_text}" + if [[ -n "${default_name}" ]]; then + effective_prompt="${prompt_text%:*} [${default_name}]: " fi local name - name=$(safe_read "${prompt_text}") + name=$(safe_read "${effective_prompt}") || name="" + [[ -z "${name}" && -n "${default_name}" ]] && name="${default_name}" + if [[ -z "${name}" ]]; then log_error "${prompt_text%:*} is required but not provided" log_error "" diff --git a/sprite/lib/common.sh b/sprite/lib/common.sh index 3dfc8367..32da0095 100644 --- a/sprite/lib/common.sh +++ b/sprite/lib/common.sh @@ -358,7 +358,7 @@ destroy_server() { # Wrapper for spawn_agent compatibility (sprite uses get_sprite_name) get_server_name() { get_sprite_name; } -cloud_authenticate() { ensure_sprite_installed; ensure_sprite_authenticated; } +cloud_authenticate() { prompt_spawn_name; ensure_sprite_installed; ensure_sprite_authenticated; } cloud_provision() { SPRITE_NAME="$1" ensure_sprite_exists "${SPRITE_NAME}"