Skip to content

Commit a2c6da7

Browse files
committed
Add limactl save
```console $ limactl save --help Save an instance Usage: limactl save INSTANCE [flags] Flags: -h, --help help for save Global Flags: --debug debug mode --log-format string Set the logging format [text, json] (default "text") --log-level string Set the logging level [trace, debug, info, warn, error] --tty Enable TUI interactions such as opening an editor. Defaults to true when stdout is a terminal. Set to false for automation. (default true) ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent 260c8a2 commit a2c6da7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

cmd/limactl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func newApp() *cobra.Command {
155155
newProtectCommand(),
156156
newUnprotectCommand(),
157157
newTunnelCommand(),
158+
newSaveCommand(),
158159
)
159160
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
160161
rootCmd.AddCommand(startAtLoginCommand())

cmd/limactl/save.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"github.com/lima-vm/lima/pkg/instance"
5+
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
6+
"github.com/lima-vm/lima/pkg/store"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func newSaveCommand() *cobra.Command {
11+
saveCmd := &cobra.Command{
12+
Use: "save INSTANCE",
13+
Short: "Save an instance",
14+
Args: WrapArgsError(cobra.MaximumNArgs(1)),
15+
RunE: saveAction,
16+
ValidArgsFunction: saveBashComplete,
17+
GroupID: basicCommand,
18+
}
19+
20+
return saveCmd
21+
}
22+
23+
func saveAction(cmd *cobra.Command, args []string) error {
24+
instName := DefaultInstanceName
25+
if len(args) > 0 {
26+
instName = args[0]
27+
}
28+
29+
inst, err := store.Inspect(instName)
30+
if err != nil {
31+
return err
32+
}
33+
34+
if err != nil {
35+
return err
36+
}
37+
err = instance.StopGracefully(inst, true)
38+
// TODO: should we also reconcile networks if graceful save returned an error?
39+
if err == nil {
40+
err = networks.Reconcile(cmd.Context(), "")
41+
}
42+
return err
43+
}
44+
45+
func saveBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
46+
return bashCompleteInstanceNames(cmd)
47+
}

0 commit comments

Comments
 (0)