Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/cmd/limactl/delete.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
"errors"
8
"fmt"
9
"os"
10
"runtime"
11
12
"github.com/sirupsen/logrus"
13
"github.com/spf13/cobra"
14
15
"github.com/lima-vm/lima/v2/pkg/autostart"
16
"github.com/lima-vm/lima/v2/pkg/instance"
17
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
18
"github.com/lima-vm/lima/v2/pkg/store"
19
)
20
21
func newDeleteCommand() *cobra.Command {
22
deleteCommand := &cobra.Command{
23
Use: "delete INSTANCE [INSTANCE, ...]",
24
Aliases: []string{"remove", "rm"},
25
Short: "Delete an instance of Lima",
26
Args: WrapArgsError(cobra.MinimumNArgs(1)),
27
RunE: deleteAction,
28
ValidArgsFunction: deleteBashComplete,
29
GroupID: basicCommand,
30
}
31
deleteCommand.Flags().BoolP("force", "f", false, "Forcibly kill the processes")
32
return deleteCommand
33
}
34
35
func deleteAction(cmd *cobra.Command, args []string) error {
36
ctx := cmd.Context()
37
force, err := cmd.Flags().GetBool("force")
38
if err != nil {
39
return err
40
}
41
for _, instName := range args {
42
inst, err := store.Inspect(ctx, instName)
43
if err != nil {
44
if errors.Is(err, os.ErrNotExist) {
45
logrus.Warnf("Ignoring non-existent instance %q", instName)
46
continue
47
}
48
return err
49
}
50
if err := instance.Delete(cmd.Context(), inst, force); err != nil {
51
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
52
}
53
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
54
deleted, err := autostart.DeleteStartAtLoginEntry(ctx, runtime.GOOS, instName)
55
if err != nil && !errors.Is(err, os.ErrNotExist) {
56
logrus.WithError(err).Warnf("The autostart file for instance %q does not exist", instName)
57
} else if deleted {
58
logrus.Infof("The autostart file %q has been deleted", autostart.GetFilePath(runtime.GOOS, instName))
59
}
60
}
61
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
62
}
63
return networks.Reconcile(cmd.Context(), "")
64
}
65
66
func deleteBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
67
return bashCompleteInstanceNames(cmd)
68
}
69
70