|
| 1 | +package volume |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/charmbracelet/lipgloss" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + cmdutil "github/verda-cloud/verda-cli/internal/verda-cli/cmd/util" |
| 11 | +) |
| 12 | + |
| 13 | +// NewCmdTrash creates the volume trash command. |
| 14 | +func NewCmdTrash(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { |
| 15 | + return &cobra.Command{ |
| 16 | + Use: "trash", |
| 17 | + Short: "List volumes in trash", |
| 18 | + Long: cmdutil.LongDesc(` |
| 19 | + List deleted volumes that can still be restored (within 96 hours). |
| 20 | + `), |
| 21 | + Example: cmdutil.Examples(` |
| 22 | + verda volume trash |
| 23 | + verda vol trash |
| 24 | + `), |
| 25 | + Args: cobra.NoArgs, |
| 26 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 27 | + return runTrash(cmd, f, ioStreams) |
| 28 | + }, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func runTrash(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams) error { |
| 33 | + client, err := f.VerdaClient() |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) |
| 39 | + defer cancel() |
| 40 | + |
| 41 | + var sp interface{ Stop(string) } |
| 42 | + if status := f.Status(); status != nil { |
| 43 | + sp, _ = status.Spinner(ctx, "Loading trash...") |
| 44 | + } |
| 45 | + volumes, err := client.Volumes.ListVolumesByStatus(ctx, "deleted") |
| 46 | + if sp != nil { |
| 47 | + sp.Stop("") |
| 48 | + } |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + if len(volumes) == 0 { |
| 54 | + _, _ = fmt.Fprintln(ioStreams.Out, "Trash is empty.") |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + dim := lipgloss.NewStyle().Foreground(lipgloss.Color("8")) |
| 59 | + bold := lipgloss.NewStyle().Bold(true) |
| 60 | + |
| 61 | + _, _ = fmt.Fprintf(ioStreams.Out, " %d volume(s) in trash\n\n", len(volumes)) |
| 62 | + |
| 63 | + for _, v := range volumes { |
| 64 | + _, _ = fmt.Fprintf(ioStreams.Out, " %s\n", bold.Render(v.Name)) |
| 65 | + _, _ = fmt.Fprintf(ioStreams.Out, " %s %s\n", dim.Render("ID: "), v.ID) |
| 66 | + _, _ = fmt.Fprintf(ioStreams.Out, " %s %dGB\n", dim.Render("Size: "), v.Size) |
| 67 | + _, _ = fmt.Fprintf(ioStreams.Out, " %s %s\n", dim.Render("Type: "), v.Type) |
| 68 | + _, _ = fmt.Fprintf(ioStreams.Out, " %s %s\n", dim.Render("Location:"), v.Location) |
| 69 | + _, _ = fmt.Fprintf(ioStreams.Out, " %s %s\n\n", dim.Render("Deleted: "), v.CreatedAt.Format("2 Jan 2006, 15:04")) |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
0 commit comments