/
snapshot.go
190 lines (158 loc) · 4.32 KB
/
snapshot.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"strings"
"github.com/lima-vm/lima/pkg/snapshot"
"github.com/lima-vm/lima/pkg/store"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func newSnapshotCommand() *cobra.Command {
snapshotCmd := &cobra.Command{
Use: "snapshot",
Short: "Manage instance snapshots",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logrus.Warn("`limactl snapshot` is experimental")
},
GroupID: advancedCommand,
}
snapshotCmd.AddCommand(newSnapshotApplyCommand())
snapshotCmd.AddCommand(newSnapshotCreateCommand())
snapshotCmd.AddCommand(newSnapshotDeleteCommand())
snapshotCmd.AddCommand(newSnapshotListCommand())
return snapshotCmd
}
func newSnapshotCreateCommand() *cobra.Command {
createCmd := &cobra.Command{
Use: "create INSTANCE",
Aliases: []string{"save"},
Short: "Create (save) a snapshot",
Args: cobra.MinimumNArgs(1),
RunE: snapshotCreateAction,
ValidArgsFunction: snapshotBashComplete,
}
createCmd.Flags().String("tag", "", "name of the snapshot")
return createCmd
}
func snapshotCreateAction(cmd *cobra.Command, args []string) error {
instName := args[0]
inst, err := store.Inspect(instName)
if err != nil {
return err
}
tag, err := cmd.Flags().GetString("tag")
if err != nil {
return err
}
if tag == "" {
return fmt.Errorf("expected tag")
}
ctx := cmd.Context()
return snapshot.Save(ctx, inst, tag)
}
func newSnapshotDeleteCommand() *cobra.Command {
deleteCmd := &cobra.Command{
Use: "delete INSTANCE",
Aliases: []string{"del"},
Short: "Delete (del) a snapshot",
Args: cobra.MinimumNArgs(1),
RunE: snapshotDeleteAction,
ValidArgsFunction: snapshotBashComplete,
}
deleteCmd.Flags().String("tag", "", "name of the snapshot")
return deleteCmd
}
func snapshotDeleteAction(cmd *cobra.Command, args []string) error {
instName := args[0]
inst, err := store.Inspect(instName)
if err != nil {
return err
}
tag, err := cmd.Flags().GetString("tag")
if err != nil {
return err
}
if tag == "" {
return fmt.Errorf("expected tag")
}
ctx := cmd.Context()
return snapshot.Del(ctx, inst, tag)
}
func newSnapshotApplyCommand() *cobra.Command {
applyCmd := &cobra.Command{
Use: "apply INSTANCE",
Aliases: []string{"load"},
Short: "Apply (load) a snapshot",
Args: cobra.MinimumNArgs(1),
RunE: snapshotApplyAction,
ValidArgsFunction: snapshotBashComplete,
}
applyCmd.Flags().String("tag", "", "name of the snapshot")
return applyCmd
}
func snapshotApplyAction(cmd *cobra.Command, args []string) error {
instName := args[0]
inst, err := store.Inspect(instName)
if err != nil {
return err
}
tag, err := cmd.Flags().GetString("tag")
if err != nil {
return err
}
if tag == "" {
return fmt.Errorf("expected tag")
}
ctx := cmd.Context()
return snapshot.Load(ctx, inst, tag)
}
func newSnapshotListCommand() *cobra.Command {
listCmd := &cobra.Command{
Use: "list INSTANCE",
Aliases: []string{"ls"},
Short: "List existing snapshots",
Args: cobra.MinimumNArgs(1),
RunE: snapshotListAction,
ValidArgsFunction: snapshotBashComplete,
}
listCmd.Flags().BoolP("quiet", "q", false, "Only show tags")
return listCmd
}
func snapshotListAction(cmd *cobra.Command, args []string) error {
instName := args[0]
inst, err := store.Inspect(instName)
if err != nil {
return err
}
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
}
ctx := cmd.Context()
out, err := snapshot.List(ctx, inst)
if err != nil {
return err
}
if quiet {
for i, line := range strings.Split(out, "\n") {
// "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT"
fields := strings.Fields(line)
if i == 0 && len(fields) > 1 && fields[1] != "TAG" {
// make sure that output matches the expected
return fmt.Errorf("unknown header: %s", line)
}
if i == 0 || line == "" {
// skip header and empty line after using split
continue
}
tag := fields[1]
fmt.Printf("%s\n", tag)
}
return nil
}
fmt.Print(out)
return nil
}
func snapshotBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return bashCompleteInstanceNames(cmd)
}