跳到主要内容

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

浏览文件

factory-reset.go

91 行 · 2.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
7 "errors"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "github.com/sirupsen/logrus"
13 "github.com/spf13/cobra"
14
15 "github.com/lima-vm/lima/v2/pkg/cidata"
16 "github.com/lima-vm/lima/v2/pkg/driver/external/server"
17 "github.com/lima-vm/lima/v2/pkg/driverutil"
18 "github.com/lima-vm/lima/v2/pkg/instance"
19 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
20 "github.com/lima-vm/lima/v2/pkg/store"
21)
22
23func newFactoryResetCommand() *cobra.Command {
24 resetCommand := &cobra.Command{
25 Use: "factory-reset INSTANCE",
26 Short: "Factory reset an instance of Lima",
27 Args: WrapArgsError(cobra.MaximumNArgs(1)),
28 RunE: factoryResetAction,
29 ValidArgsFunction: factoryResetBashComplete,
30 GroupID: advancedCommand,
31 }
32 return resetCommand
33}
34
35func factoryResetAction(cmd *cobra.Command, args []string) error {
36 ctx := cmd.Context()
37 instName := DefaultInstanceName
38 if len(args) > 0 {
39 instName = args[0]
40 }
41
42 inst, err := store.Inspect(ctx, instName)
43 if err != nil {
44 if errors.Is(err, os.ErrNotExist) {
45 logrus.Infof("Instance %#q not found", instName)
46 return nil
47 }
48 return err
49 }
50 if inst.Protected {
51 return errors.New("instance is protected to prohibit accidental factory-reset (Hint: use `limactl unprotect`)")
52 }
53
54 instance.StopForcibly(inst)
55
56 fi, err := os.ReadDir(inst.Dir)
57 if err != nil {
58 return err
59 }
60 retain := map[string]struct{}{
61 filenames.LimaVersion: {},
62 filenames.Protected: {},
63 filenames.VzIdentifier: {},
64 }
65 for _, f := range fi {
66 path := filepath.Join(inst.Dir, f.Name())
67 if _, ok := retain[f.Name()]; !ok && !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") {
68 logrus.Infof("Removing %#q", path)
69 if err := os.Remove(path); err != nil {
70 logrus.Error(err)
71 }
72 }
73 }
74 if _, err := driverutil.CreateConfiguredDriver(ctx, inst, 0); err != nil {
75 logrus.WithError(err).Error("failed to configure driver for regenerating cloud-config")
76 }
77 // Regenerate the cloud-config.yaml, to reflect any changes to the global _config
78 if err := cidata.GenerateCloudConfig(ctx, inst.Dir, instName, inst.Config); err != nil {
79 logrus.Error(err)
80 }
81 defer func() {
82 server.Stop(inst.Dir, true)
83 }()
84
85 logrus.Infof("Instance %#q has been factory reset", instName)
86 return nil
87}
88
89func factoryResetBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
90 return bashCompleteInstanceNames(cmd)
91}