-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathstop.go
53 lines (46 loc) · 1.25 KB
/
stop.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
package main
import (
"github.com/lima-vm/lima/pkg/instance"
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
"github.com/lima-vm/lima/pkg/store"
"github.com/spf13/cobra"
)
func newStopCommand() *cobra.Command {
stopCmd := &cobra.Command{
Use: "stop INSTANCE",
Short: "Stop an instance",
Args: WrapArgsError(cobra.MaximumNArgs(1)),
RunE: stopAction,
ValidArgsFunction: stopBashComplete,
GroupID: basicCommand,
}
stopCmd.Flags().BoolP("force", "f", false, "force stop the instance")
return stopCmd
}
func stopAction(cmd *cobra.Command, args []string) error {
instName := DefaultInstanceName
if len(args) > 0 {
instName = args[0]
}
inst, err := store.Inspect(instName)
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if force {
instance.StopForcibly(inst)
} else {
err = instance.StopGracefully(inst, false)
}
// TODO: should we also reconcile networks if graceful stop returned an error?
if err == nil {
err = networks.Reconcile(cmd.Context(), "")
}
return err
}
func stopBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return bashCompleteInstanceNames(cmd)
}