跳到主要内容

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

浏览文件

clone.go

157 行 · 4.2 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 "path/filepath"
11
12 "github.com/spf13/cobra"
13
14 "github.com/lima-vm/lima/v2/cmd/limactl/editflags"
15 "github.com/lima-vm/lima/v2/pkg/driver/external/server"
16 "github.com/lima-vm/lima/v2/pkg/driverutil"
17 "github.com/lima-vm/lima/v2/pkg/instance"
18 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
19 "github.com/lima-vm/lima/v2/pkg/limayaml"
20 "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
21 "github.com/lima-vm/lima/v2/pkg/store"
22 "github.com/lima-vm/lima/v2/pkg/yqutil"
23)
24
25func newCloneCommand() *cobra.Command {
26 cloneCommand := &cobra.Command{
27 Use: "clone OLDINST NEWINST",
28 Short: "Clone an instance of Lima",
29 Long: `Clone an instance of Lima.
30
31Not to be confused with 'limactl copy' ('limactl cp').
32`,
33 Args: WrapArgsError(cobra.ExactArgs(2)),
34 RunE: cloneOrRenameAction,
35 ValidArgsFunction: cloneBashComplete,
36 GroupID: advancedCommand,
37 }
38 cloneCommand.Flags().Bool("start", false, "Start the instance after cloning")
39 editflags.RegisterEdit(cloneCommand, "[limactl edit] ")
40 return cloneCommand
41}
42
43func newRenameCommand() *cobra.Command {
44 renameCommand := &cobra.Command{
45 Use: "rename OLDINST NEWINST",
46 // No "mv" alias, to avoid confusion with a theoretical equivalent of `limactl cp` but s/cp/mv/.
47 Short: "Rename an instance of Lima",
48 Args: WrapArgsError(cobra.ExactArgs(2)),
49 RunE: cloneOrRenameAction,
50 ValidArgsFunction: cloneBashComplete,
51 GroupID: advancedCommand,
52 }
53 renameCommand.Flags().Bool("start", false, "Start the instance after renaming")
54 editflags.RegisterEdit(renameCommand, "[limactl edit] ")
55 return renameCommand
56}
57
58func cloneOrRenameAction(cmd *cobra.Command, args []string) error {
59 rename := cmd.Name() == "rename"
60 ctx := cmd.Context()
61 flags := cmd.Flags()
62 tty, err := flags.GetBool("tty")
63 if err != nil {
64 return err
65 }
66
67 oldInstName, newInstName := args[0], args[1]
68 oldInst, err := store.Inspect(ctx, oldInstName)
69 if err != nil {
70 if errors.Is(err, os.ErrNotExist) {
71 return fmt.Errorf("instance %#q not found", oldInstName)
72 }
73 return err
74 }
75
76 newInst, err := instance.CloneOrRename(ctx, oldInst, newInstName, rename)
77 if err != nil {
78 return err
79 }
80
81 yqExprs, err := editflags.YQExpressions(flags, false, newInst.Config.Param)
82 if err != nil {
83 return err
84 }
85 var start bool
86 if len(yqExprs) > 0 {
87 // TODO: reduce duplicated codes across cloneAction and editAction
88 yq := yqutil.Join(yqExprs)
89 filePath := filepath.Join(newInst.Dir, filenames.LimaYAML)
90 yContent, err := os.ReadFile(filePath)
91 if err != nil {
92 return err
93 }
94 yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
95 if err != nil {
96 return err
97 }
98 y, err := limayaml.LoadWithWarnings(ctx, yBytes, filePath)
99 if err != nil {
100 return err
101 }
102 // If VMType is not specified, we go with the default platform driver.
103 if err := driverutil.ResolveVMType(y); err != nil {
104 return err
105 }
106 newInst.Config = y
107 limaDriver, err := driverutil.CreateConfiguredDriver(ctx, newInst, 0)
108 if err != nil {
109 return err
110 }
111 defer func() {
112 if !start {
113 server.Stop(newInst.Dir, true)
114 }
115 }()
116 if err := limaDriver.Validate(ctx); err != nil {
117 return saveRejectedYAML(yBytes, err)
118 }
119 if err := limayaml.Validate(newInst.Config, true); err != nil {
120 return saveRejectedYAML(yBytes, err)
121 }
122 if err := limayaml.ValidateAgainstLatestConfig(ctx, yBytes, yContent); err != nil {
123 return saveRejectedYAML(yBytes, err)
124 }
125 if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
126 return err
127 }
128 newInst, err = store.Inspect(ctx, newInst.Name)
129 if err != nil {
130 return err
131 }
132 }
133
134 start, err = flags.GetBool("start")
135 if err != nil {
136 return err
137 }
138
139 if tty && !flags.Changed("start") {
140 start, err = askWhetherToStart(cmd)
141 if err != nil {
142 return err
143 }
144 }
145 if !start {
146 return nil
147 }
148 err = reconcile.Reconcile(ctx, newInst.Name)
149 if err != nil {
150 return err
151 }
152 return instance.Start(ctx, newInst, false, false)
153}
154
155func cloneBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
156 return bashCompleteInstanceNames(cmd)
157}