Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/cmd/limactl/stop.go
1645 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package main
5
6
import (
7
"github.com/spf13/cobra"
8
9
"github.com/lima-vm/lima/v2/pkg/instance"
10
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
11
"github.com/lima-vm/lima/v2/pkg/store"
12
)
13
14
func newStopCommand() *cobra.Command {
15
stopCmd := &cobra.Command{
16
Use: "stop INSTANCE",
17
Short: "Stop an instance",
18
Args: WrapArgsError(cobra.MaximumNArgs(1)),
19
RunE: stopAction,
20
ValidArgsFunction: stopBashComplete,
21
GroupID: basicCommand,
22
}
23
24
stopCmd.Flags().BoolP("force", "f", false, "Force stop the instance")
25
return stopCmd
26
}
27
28
func stopAction(cmd *cobra.Command, args []string) error {
29
ctx := cmd.Context()
30
instName := DefaultInstanceName
31
if len(args) > 0 {
32
instName = args[0]
33
}
34
35
inst, err := store.Inspect(ctx, instName)
36
if err != nil {
37
return err
38
}
39
40
force, err := cmd.Flags().GetBool("force")
41
if err != nil {
42
return err
43
}
44
if force {
45
instance.StopForcibly(inst)
46
} else {
47
err = instance.StopGracefully(ctx, inst, false)
48
}
49
// TODO: should we also reconcile networks if graceful stop returned an error?
50
if err == nil {
51
err = networks.Reconcile(ctx, "")
52
}
53
return err
54
}
55
56
func stopBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
57
return bashCompleteInstanceNames(cmd)
58
}
59
60