forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list-snapshots.go
44 lines (41 loc) · 1.18 KB
/
list-snapshots.go
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
package list
import (
"github.com/ara-reddeer/proxmox-api-go/cli"
"github.com/ara-reddeer/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)
var (
// flag needs to be reset, as this value will persist during tests
noTree bool
list_snapshotsCmd = &cobra.Command{
Use: "snapshots GuestID",
Short: "Prints a list of QemuSnapshots in json format",
TraverseChildren: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateExistingGuestID(args, 0)
jBody, err := proxmox.ListSnapshots(cli.NewClient(), proxmox.NewVmRef(id))
if err != nil {
noTree = false
return
}
var list []*proxmox.Snapshot
if noTree {
noTree = false
list = proxmox.FormatSnapshotsList(jBody)
} else {
list = proxmox.FormatSnapshotsTree(jBody)
}
if len(list) == 0 {
listCmd.Printf("Guest with ID (%d) has no snapshots", id)
} else {
cli.PrintFormattedJson(listCmd.OutOrStdout(), list)
}
return
},
}
)
func init() {
listCmd.AddCommand(list_snapshotsCmd)
list_snapshotsCmd.Flags().BoolVar(&noTree, "no-tree", false, "Format output as list instead of a tree.")
}