(cmd *cobra.Command)
| 70 | } |
| 71 | |
| 72 | func runSnapshotCreate(cmd *cobra.Command) error { |
| 73 | client, err := getAuthenticatedClient() |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | isInteractive := !cmd.Flags().Changed("instance-id") |
| 79 | |
| 80 | var instanceID, name string |
| 81 | |
| 82 | if isInteractive { |
| 83 | // Run interactive flow |
| 84 | createConfig, err := tui.RunSnapshotCreateInteractive(client) |
| 85 | if err != nil { |
| 86 | if errors.Is(err, tui.ErrCancelled) { |
| 87 | PrintWarningSimple("User cancelled snapshot creation") |
| 88 | return nil |
| 89 | } |
| 90 | if errors.Is(err, tui.ErrNoRunningInstances) { |
| 91 | PrintWarningSimple("No running instances found. Snapshots can only be created from instances in RUNNING state.") |
| 92 | return nil |
| 93 | } |
| 94 | return err |
| 95 | } |
| 96 | instanceID = createConfig.InstanceID |
| 97 | name = createConfig.Name |
| 98 | } else { |
| 99 | // Non-interactive mode: validate flags |
| 100 | if snapshotInstanceID == "" { |
| 101 | return usageErr("--instance-id is required") |
| 102 | } |
| 103 | if snapshotName == "" { |
| 104 | return usageErr("--name is required") |
| 105 | } |
| 106 | instanceID = snapshotInstanceID |
| 107 | name = snapshotName |
| 108 | |
| 109 | // Validate instance exists and is in RUNNING state |
| 110 | var instances []api.Instance |
| 111 | if err := tui.RunWithBusySpinner("Validating instance...", os.Stdout, func() error { |
| 112 | var e error |
| 113 | instances, e = client.ListInstances() |
| 114 | return e |
| 115 | }); err != nil { |
| 116 | return fmt.Errorf("failed to fetch instances: %w", err) |
| 117 | } |
| 118 | |
| 119 | foundInstance := findInstance(instances, instanceID) |
| 120 | |
| 121 | if foundInstance == nil { |
| 122 | return usageErr("instance '%s' not found", instanceID) |
| 123 | } |
| 124 | |
| 125 | if foundInstance.Status != "RUNNING" { |
| 126 | return usageErr("instance must be in RUNNING state to create snapshot (current state: %s)", foundInstance.Status) |
| 127 | } |
| 128 | |
| 129 | // Use UUID for the API call |
no test coverage detected