|
| 1 | +package deployments |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/pterm/pterm" |
| 7 | + |
| 8 | + "github.com/NodeOps-app/createos-cli/internal/api" |
| 9 | + "github.com/NodeOps-app/createos-cli/internal/cmdutil" |
| 10 | +) |
| 11 | + |
| 12 | +// resolveDeployment resolves projectID and deploymentID from args or interactively. |
| 13 | +// Args can be: |
| 14 | +// - <deployment-id> (project resolved from .createos.json) |
| 15 | +// - <project-id> <deployment-id> (explicit) |
| 16 | +// - (none) → project from config, deployment from interactive select |
| 17 | +func resolveDeployment(args []string, client *api.APIClient) (string, string, error) { |
| 18 | + switch len(args) { |
| 19 | + case 0: |
| 20 | + // No args — resolve project from config, then prompt for deployment |
| 21 | + projectID, err := cmdutil.ResolveProjectID("") |
| 22 | + if err != nil { |
| 23 | + return "", "", err |
| 24 | + } |
| 25 | + deploymentID, err := pickDeployment(client, projectID) |
| 26 | + if err != nil { |
| 27 | + return "", "", err |
| 28 | + } |
| 29 | + return projectID, deploymentID, nil |
| 30 | + case 1: |
| 31 | + // One arg — could be deployment ID (project from config) |
| 32 | + projectID, err := cmdutil.ResolveProjectID("") |
| 33 | + if err != nil { |
| 34 | + return "", "", err |
| 35 | + } |
| 36 | + return projectID, args[0], nil |
| 37 | + default: |
| 38 | + // Two args — project ID + deployment ID |
| 39 | + return args[0], args[1], nil |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func pickDeployment(client *api.APIClient, projectID string) (string, error) { |
| 44 | + deployments, err := client.ListDeployments(projectID) |
| 45 | + if err != nil { |
| 46 | + return "", err |
| 47 | + } |
| 48 | + if len(deployments) == 0 { |
| 49 | + return "", fmt.Errorf("no deployments found for this project") |
| 50 | + } |
| 51 | + if len(deployments) == 1 { |
| 52 | + pterm.Println(pterm.Gray(fmt.Sprintf(" Using deployment: v%d (%s)", deployments[0].VersionNumber, deployments[0].Status))) |
| 53 | + return deployments[0].ID, nil |
| 54 | + } |
| 55 | + |
| 56 | + options := make([]string, len(deployments)) |
| 57 | + for i, d := range deployments { |
| 58 | + label := fmt.Sprintf("%s %s %s", d.CreatedAt.Format("Jan 02 15:04"), d.Status, d.ID[:8]) |
| 59 | + if d.Source != nil && d.Source.Commit != "" { |
| 60 | + commit := d.Source.Commit |
| 61 | + if len(commit) > 7 { |
| 62 | + commit = commit[:7] |
| 63 | + } |
| 64 | + msg := d.Source.CommitMessage |
| 65 | + if len(msg) > 50 { |
| 66 | + msg = msg[:50] + "…" |
| 67 | + } |
| 68 | + label = fmt.Sprintf("%s %s %s %s %s", d.CreatedAt.Format("Jan 02 15:04"), d.Status, d.ID[:8], commit, msg) |
| 69 | + } |
| 70 | + options[i] = label |
| 71 | + } |
| 72 | + selected, err := pterm.DefaultInteractiveSelect. |
| 73 | + WithOptions(options). |
| 74 | + WithDefaultText("Select a deployment"). |
| 75 | + Show() |
| 76 | + if err != nil { |
| 77 | + return "", fmt.Errorf("could not read selection: %w", err) |
| 78 | + } |
| 79 | + for i, opt := range options { |
| 80 | + if opt == selected { |
| 81 | + return deployments[i].ID, nil |
| 82 | + } |
| 83 | + } |
| 84 | + return "", fmt.Errorf("no deployment selected") |
| 85 | +} |
0 commit comments