File: commands_snapshots.go

package info (click to toggle)
golang-github-jamesclonk-vultr 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 560 kB
  • sloc: makefile: 35
file content (59 lines) | stat: -rw-r--r-- 1,406 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cmd

import (
	"fmt"
	"log"

	"github.com/jawher/mow.cli"
)

func snapshotsCreate(cmd *cli.Cmd) {
	cmd.Spec = "SUBID [-d]"

	id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see <servers>)")
	description := cmd.StringOpt("d description", "", "Description of snapshot")

	cmd.Action = func() {
		snapshot, err := GetClient().CreateSnapshot(*id, *description)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("Snapshot created\n\n")
		lengths := []int{16, 48}
		tabsPrint(columns{"SNAPSHOTID", "DESCRIPTION"}, lengths)
		tabsPrint(columns{snapshot.ID, snapshot.Description}, lengths)
		tabsFlush()
	}
}

func snapshotsDelete(cmd *cli.Cmd) {
	id := cmd.StringArg("SNAPSHOTID", "", "SNAPSHOTID of snapshot to delete (see <snapshots>)")
	cmd.Action = func() {
		if err := GetClient().DeleteSnapshot(*id); err != nil {
			log.Fatal(err)
		}
		fmt.Println("Snapshot deleted")
	}
}

func snapshotsList(cmd *cli.Cmd) {
	cmd.Action = func() {
		snapshots, err := GetClient().GetSnapshots()
		if err != nil {
			log.Fatal(err)
		}

		if len(snapshots) == 0 {
			fmt.Println()
			return
		}

		lengths := []int{16, 40, 16, 16, 24}
		tabsPrint(columns{"SNAPSHOTID", "DESCRIPTION", "SIZE", "STATUS", "DATE"}, lengths)
		for _, snapshot := range snapshots {
			tabsPrint(columns{snapshot.ID, snapshot.Description, snapshot.Size, snapshot.Status, snapshot.Created}, lengths)
		}
		tabsFlush()
	}
}