|
| 1 | +package projects |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/pterm/pterm" |
| 7 | + "github.com/urfave/cli/v2" |
| 8 | + |
| 9 | + "github.com/NodeOps-app/createos-cli/internal/api" |
| 10 | + "github.com/NodeOps-app/createos-cli/internal/config" |
| 11 | + "github.com/NodeOps-app/createos-cli/internal/terminal" |
| 12 | +) |
| 13 | + |
| 14 | +func newUnsuspendCommand() *cli.Command { |
| 15 | + return &cli.Command{ |
| 16 | + Name: "unsuspend", |
| 17 | + Usage: "Resume a suspended project", |
| 18 | + Flags: []cli.Flag{ |
| 19 | + &cli.StringFlag{Name: "project", Usage: "Project ID"}, |
| 20 | + }, |
| 21 | + Action: func(c *cli.Context) error { |
| 22 | + client, ok := c.App.Metadata[api.ClientKey].(*api.APIClient) |
| 23 | + if !ok { |
| 24 | + return fmt.Errorf("you're not signed in — run 'createos login' to get started") |
| 25 | + } |
| 26 | + |
| 27 | + projectID := c.String("project") |
| 28 | + |
| 29 | + // Try linked project config |
| 30 | + if projectID == "" { |
| 31 | + cfg, _ := config.FindProjectConfig() |
| 32 | + if cfg != nil { |
| 33 | + projectID = cfg.ProjectID |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Interactive picker filtered to suspended projects |
| 38 | + if projectID == "" && terminal.IsInteractive() { |
| 39 | + projects, err := client.ListProjects() |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + suspended := make([]api.Project, 0, len(projects)) |
| 45 | + for _, p := range projects { |
| 46 | + if p.Status == "suspended" { |
| 47 | + suspended = append(suspended, p) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if len(suspended) == 0 { |
| 52 | + return fmt.Errorf("you don't have any suspended projects to resume") |
| 53 | + } |
| 54 | + |
| 55 | + options := make([]string, len(suspended)) |
| 56 | + for i, p := range suspended { |
| 57 | + options[i] = fmt.Sprintf("%s (%s)", p.DisplayName, p.ID) |
| 58 | + } |
| 59 | + |
| 60 | + selected, err := pterm.DefaultInteractiveSelect. |
| 61 | + WithDefaultText("Select a project to resume"). |
| 62 | + WithOptions(options). |
| 63 | + WithFilter(true). |
| 64 | + Show() |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("selection cancelled") |
| 67 | + } |
| 68 | + |
| 69 | + for i, opt := range options { |
| 70 | + if opt == selected { |
| 71 | + projectID = suspended[i].ID |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if projectID == "" { |
| 78 | + return fmt.Errorf("please provide a project ID\n\n To see your projects, run:\n createos projects list") |
| 79 | + } |
| 80 | + |
| 81 | + // Validate status when project was explicitly provided (not from picker) |
| 82 | + project, err := client.GetProject(projectID) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + if project.Status != "suspended" { |
| 88 | + switch project.Status { |
| 89 | + case "active": |
| 90 | + return fmt.Errorf("this project is already running") |
| 91 | + case "suspending": |
| 92 | + return fmt.Errorf("this project is currently being suspended — wait for it to finish before unsuspending") |
| 93 | + default: |
| 94 | + return fmt.Errorf("this project can't be unsuspended right now because it is %s\n\n Only suspended projects can be resumed. Run 'createos projects get %s' to check its status", project.Status, projectID) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if terminal.IsInteractive() { |
| 99 | + confirm, err := pterm.DefaultInteractiveConfirm. |
| 100 | + WithDefaultText(fmt.Sprintf("Are you sure you want to resume project %q?", project.DisplayName)). |
| 101 | + WithDefaultValue(false). |
| 102 | + Show() |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("could not read confirmation: %w", err) |
| 105 | + } |
| 106 | + if !confirm { |
| 107 | + fmt.Println("Cancelled. Your project was not resumed.") |
| 108 | + return nil |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if err := client.UnsuspendProject(projectID); err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + pterm.Success.Println("Project resumed.") |
| 117 | + return nil |
| 118 | + }, |
| 119 | + } |
| 120 | +} |
0 commit comments