|
| 1 | +package koyeb |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/cobra" |
| 5 | + |
| 6 | + "github.com/koyeb/koyeb-api-client-go/api/v1/koyeb" |
| 7 | +) |
| 8 | + |
| 9 | +func NewSnapshotCmd() *cobra.Command { |
| 10 | + h := NewSnapshotHandler() |
| 11 | + _ = h |
| 12 | + |
| 13 | + snapshotCmd := &cobra.Command{ |
| 14 | + Use: "snapshots ACTION", |
| 15 | + Aliases: []string{"vol", "snapshot"}, |
| 16 | + Short: "Manage snapshots", |
| 17 | + } |
| 18 | + |
| 19 | + createSnapshotCmd := &cobra.Command{ |
| 20 | + Use: "create NAME", |
| 21 | + Short: "Create a new snapshot", |
| 22 | + Args: cobra.ExactArgs(2), |
| 23 | + RunE: WithCLIContext(func(ctx *CLIContext, cmd *cobra.Command, args []string) error { |
| 24 | + req := koyeb.NewCreateSnapshotRequestWithDefaults() |
| 25 | + |
| 26 | + parentVolumeID, err := ctx.Mapper.Volume().ResolveID(args[1]) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + req.SetName(args[0]) |
| 32 | + req.SetParentVolumeId(parentVolumeID) |
| 33 | + |
| 34 | + return h.Create(ctx, cmd, args, req) |
| 35 | + }), |
| 36 | + } |
| 37 | + createSnapshotCmd.Flags().String("region", "was", "Region of the snapshot") |
| 38 | + createSnapshotCmd.Flags().Int64("size", 10, "Size of the snapshot in GB") |
| 39 | + createSnapshotCmd.Flags().Bool("read-only", false, "Force the snapshot to be read-only") |
| 40 | + snapshotCmd.AddCommand(createSnapshotCmd) |
| 41 | + |
| 42 | + getSnapshotCmd := &cobra.Command{ |
| 43 | + Use: "get NAME", |
| 44 | + Short: "Get a snapshot", |
| 45 | + Args: cobra.ExactArgs(1), |
| 46 | + RunE: WithCLIContext(func(ctx *CLIContext, cmd *cobra.Command, args []string) error { |
| 47 | + return h.Get(ctx, cmd, args) |
| 48 | + }), |
| 49 | + } |
| 50 | + snapshotCmd.AddCommand(getSnapshotCmd) |
| 51 | + |
| 52 | + listSnapshotCmd := &cobra.Command{ |
| 53 | + Use: "list", |
| 54 | + Short: "List snapshots", |
| 55 | + RunE: WithCLIContext(func(ctx *CLIContext, cmd *cobra.Command, args []string) error { |
| 56 | + return h.List(ctx, cmd, args) |
| 57 | + }), |
| 58 | + } |
| 59 | + snapshotCmd.AddCommand(listSnapshotCmd) |
| 60 | + |
| 61 | + updateSnapshotCmd := &cobra.Command{ |
| 62 | + Use: "update NAME", |
| 63 | + Short: "Update a snapshot", |
| 64 | + Args: cobra.ExactArgs(1), |
| 65 | + RunE: WithCLIContext(func(ctx *CLIContext, cmd *cobra.Command, args []string) error { |
| 66 | + req := koyeb.NewUpdateSnapshotRequestWithDefaults() |
| 67 | + |
| 68 | + name, _ := cmd.Flags().GetString("name") |
| 69 | + if name != "" { |
| 70 | + req.SetName(name) |
| 71 | + } |
| 72 | + |
| 73 | + return h.Update(ctx, cmd, args, req) |
| 74 | + }), |
| 75 | + } |
| 76 | + updateSnapshotCmd.Flags().String("name", "", "Change the snapshot name") |
| 77 | + updateSnapshotCmd.Flags().Int64("size", -1, "Increase the snapshot size") |
| 78 | + snapshotCmd.AddCommand(updateSnapshotCmd) |
| 79 | + |
| 80 | + deleteSnapshotCmd := &cobra.Command{ |
| 81 | + Use: "delete NAME", |
| 82 | + Short: "Delete a snapshot", |
| 83 | + Args: cobra.ExactArgs(1), |
| 84 | + RunE: WithCLIContext(func(ctx *CLIContext, cmd *cobra.Command, args []string) error { |
| 85 | + return h.Delete(ctx, cmd, args) |
| 86 | + }), |
| 87 | + } |
| 88 | + snapshotCmd.AddCommand(deleteSnapshotCmd) |
| 89 | + |
| 90 | + return snapshotCmd |
| 91 | +} |
| 92 | + |
| 93 | +func NewSnapshotHandler() *SnapshotHandler { |
| 94 | + return &SnapshotHandler{} |
| 95 | +} |
| 96 | + |
| 97 | +type SnapshotHandler struct { |
| 98 | +} |
| 99 | + |
| 100 | +func ResolveSnapshotArgs(ctx *CLIContext, val string) (string, error) { |
| 101 | + snapshotMapper := ctx.Mapper.Snapshot() |
| 102 | + id, err := snapshotMapper.ResolveID(val) |
| 103 | + if err != nil { |
| 104 | + return "", err |
| 105 | + } |
| 106 | + return id, nil |
| 107 | +} |
0 commit comments