跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

delete.go

93 行 · 3.0 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
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/limatype/dirnames"
17 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
18 "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
19 "github.com/lima-vm/lima/v2/pkg/store"
20)
21
22func newDeleteCommand() *cobra.Command {
23 deleteCommand := &cobra.Command{
24 Use: "delete INSTANCE [INSTANCE, ...]",
25 Aliases: []string{"remove", "rm"},
26 Short: "Delete an instance of Lima",
27 Args: WrapArgsError(cobra.MinimumNArgs(1)),
28 RunE: deleteAction,
29 ValidArgsFunction: deleteBashComplete,
30 GroupID: basicCommand,
31 }
32 deleteCommand.Flags().BoolP("force", "f", false, "Forcibly kill the processes")
33 return deleteCommand
34}
35
36func deleteAction(cmd *cobra.Command, args []string) error {
37 ctx := cmd.Context()
38 force, err := cmd.Flags().GetBool("force")
39 if err != nil {
40 return err
41 }
42 var errs []error
43 for _, instName := range args {
44 inst, err := store.Inspect(ctx, instName)
45 if err != nil {
46 if errors.Is(err, os.ErrNotExist) {
47 if err := warnAboutMissingInstance(instName); err != nil {
48 errs = append(errs, err)
49 }
50 continue
51 }
52 return err
53 }
54 if err := instance.Delete(ctx, inst, force); err != nil {
55 return fmt.Errorf("failed to delete instance %#q: %w", instName, err)
56 }
57 if registered, err := autostart.IsRegistered(ctx, inst); err != nil && !errors.Is(err, autostart.ErrNotSupported) {
58 logrus.WithError(err).Warnf("Failed to check if the autostart entry for instance %#q is registered", instName)
59 } else if registered {
60 if err := autostart.UnregisterFromStartAtLogin(ctx, inst); err != nil {
61 logrus.WithError(err).Warnf("Failed to unregister the autostart entry for instance %#q", instName)
62 } else {
63 logrus.Infof("The autostart entry for instance %#q has been unregistered", instName)
64 }
65 }
66 logrus.Infof("Deleted %#q (%#q)", instName, inst.Dir)
67 }
68 if err := reconcile.Reconcile(ctx, ""); err != nil {
69 errs = append(errs, err)
70 }
71 return errors.Join(errs...)
72}
73
74func warnAboutMissingInstance(instName string) error {
75 instDir, err := dirnames.InstanceDir(instName)
76 if err != nil {
77 return err
78 }
79 if _, statErr := os.Stat(instDir); errors.Is(statErr, os.ErrNotExist) {
80 logrus.Warnf("Ignoring non-existent instance %#q", instName)
81 return nil
82 } else if statErr != nil {
83 return statErr
84 }
85 return fmt.Errorf("instance %#q directory %#q exists but its %#q could not be read; "+
86 "it was NOT deleted, in case it is currently being cloned into. "+
87 "If no `limactl` operation is using it, remove it manually",
88 instName, instDir, filenames.LimaYAML)
89}
90
91func deleteBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
92 return bashCompleteInstanceNames(cmd)
93}