|
| 1 | +package sandbox |
| 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/output" |
| 11 | +) |
| 12 | + |
| 13 | +// newShapesCommand lists the static VM size catalog. |
| 14 | +func newShapesCommand() *cli.Command { |
| 15 | + return &cli.Command{ |
| 16 | + Name: "shapes", |
| 17 | + Usage: "List the available sandbox sizes (vCPU / RAM / disk)", |
| 18 | + Action: runShapes, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func runShapes(c *cli.Context) error { |
| 23 | + client, ok := c.App.Metadata[api.SandboxClientKey].(*api.SandboxClient) |
| 24 | + if !ok { |
| 25 | + return fmt.Errorf("you're not signed in — run 'createos login' to get started") |
| 26 | + } |
| 27 | + shapes, err := client.ListShapes(c.Context) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + output.Render(c, shapes, func() { |
| 32 | + if len(shapes) == 0 { |
| 33 | + fmt.Println("No sizes available.") |
| 34 | + return |
| 35 | + } |
| 36 | + table := pterm.TableData{{"ID", "vCPU", "RAM", "Default disk"}} |
| 37 | + for _, s := range shapes { |
| 38 | + table = append(table, []string{ |
| 39 | + s.ID, |
| 40 | + fmt.Sprintf("%d", s.VCPU), |
| 41 | + fmt.Sprintf("%d MB", s.MemMib), |
| 42 | + fmt.Sprintf("%d MB", s.DefaultDiskMib), |
| 43 | + }) |
| 44 | + } |
| 45 | + _ = pterm.DefaultTable.WithHasHeader().WithData(table).Render() |
| 46 | + pterm.Println() |
| 47 | + pterm.Println(pterm.Gray(" Pick one when creating: createos sandbox create --shape <id>")) |
| 48 | + }) |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +// newRootfsCommand lists the built-in rootfs images that any sandbox |
| 53 | +// can boot from. User-built templates are not included here — see |
| 54 | +// `sandbox template ls`. |
| 55 | +func newRootfsCommand() *cli.Command { |
| 56 | + return &cli.Command{ |
| 57 | + Name: "rootfs", |
| 58 | + Aliases: []string{"images"}, |
| 59 | + Usage: "List the built-in OS images you can boot a sandbox from", |
| 60 | + Action: runRootfs, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func runRootfs(c *cli.Context) error { |
| 65 | + client, ok := c.App.Metadata[api.SandboxClientKey].(*api.SandboxClient) |
| 66 | + if !ok { |
| 67 | + return fmt.Errorf("you're not signed in — run 'createos login' to get started") |
| 68 | + } |
| 69 | + cat, err := client.ListRootfs(c.Context) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + output.Render(c, cat, func() { |
| 74 | + if cat == nil || len(cat.Rootfs) == 0 { |
| 75 | + fmt.Println("No built-in images available.") |
| 76 | + return |
| 77 | + } |
| 78 | + // Use the per-entry view when the server provides it; otherwise |
| 79 | + // just the names. |
| 80 | + hasEntries := len(cat.Entries) > 0 |
| 81 | + if hasEntries { |
| 82 | + table := pterm.TableData{{"Name", "Description", "Status"}} |
| 83 | + for _, e := range cat.Entries { |
| 84 | + status := "" |
| 85 | + switch { |
| 86 | + case e.Name == cat.Default: |
| 87 | + status = "default" |
| 88 | + case e.Deprecated: |
| 89 | + status = "deprecated" |
| 90 | + if e.Successor != "" { |
| 91 | + status += " → " + e.Successor |
| 92 | + } |
| 93 | + } |
| 94 | + table = append(table, []string{e.Name, e.Description, status}) |
| 95 | + } |
| 96 | + _ = pterm.DefaultTable.WithHasHeader().WithData(table).Render() |
| 97 | + } else { |
| 98 | + table := pterm.TableData{{"Name", "Default"}} |
| 99 | + for _, name := range cat.Rootfs { |
| 100 | + def := "" |
| 101 | + if name == cat.Default { |
| 102 | + def = "yes" |
| 103 | + } |
| 104 | + table = append(table, []string{name, def}) |
| 105 | + } |
| 106 | + _ = pterm.DefaultTable.WithHasHeader().WithData(table).Render() |
| 107 | + } |
| 108 | + pterm.Println() |
| 109 | + pterm.Println(pterm.Gray(" Pick one when creating: createos sandbox create --rootfs <name>")) |
| 110 | + pterm.Println(pterm.Gray(" To list your own custom templates: createos sandbox template ls")) |
| 111 | + }) |
| 112 | + return nil |
| 113 | +} |
0 commit comments