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