跳到主要内容

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

浏览文件

copytool.go

172 行 · 4.7 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package copytool
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "runtime"
14 "strings"
15
16 "github.com/sirupsen/logrus"
17
18 "github.com/lima-vm/lima/v2/pkg/fsutil"
19 "github.com/lima-vm/lima/v2/pkg/limatype"
20 "github.com/lima-vm/lima/v2/pkg/store"
21)
22
23type Backend string
24
25const (
26 BackendAuto Backend = "auto"
27 BackendRsync Backend = "rsync"
28 BackendSCP Backend = "scp"
29)
30
31type Path struct {
32 InstanceName string
33 Path string
34 IsRemote bool
35 Instance *limatype.Instance
36}
37
38// Options contains common options for copy operations. This might not be a complete list; more options can be added as needed.
39type Options struct {
40 Recursive bool
41 Verbose bool
42 AdditionalArgs []string // Make sure that the additional args are valid for a specific tool and escaped before passing them here.
43}
44
45// CopyTool is the interface for copy tool implementations.
46type CopyTool interface {
47 // Name returns the name of the copy tool.
48 Name() string
49 // Command builds and returns the exec.Cmd for the copy operation. If opts is set, it is used for this invocation instead of the tool's Options set during initialization, without modifying the stored Options.
50 Command(ctx context.Context, paths []string, opts *Options) (*exec.Cmd, error)
51 // IsAvailableOnGuest checks if the tool is available on the specified guest instance.
52 IsAvailableOnGuest(ctx context.Context, paths []string) bool
53}
54
55// New creates a new CopyTool based on the specified backend.
56func New(ctx context.Context, backend string, paths []string, opts *Options) (CopyTool, error) {
57 switch Backend(backend) {
58 case BackendSCP:
59 return newSCPTool(opts)
60 case BackendRsync:
61 rsync, err := newRsyncTool(opts)
62 if err != nil {
63 return nil, err
64 }
65 // Report a bad path as itself; IsAvailableOnGuest would reduce it to
66 // "rsync not available on guest(s)".
67 if _, err := parseCopyPaths(ctx, paths); err != nil {
68 return nil, err
69 }
70 if !rsync.IsAvailableOnGuest(ctx, paths) {
71 return nil, errors.New("rsync not available on guest(s)")
72 }
73 return rsync, nil
74 case BackendAuto:
75 // For rsync, the source and destination cannot both be remote
76 bothRemote, err := hasRemoteSourceAndDestination(ctx, paths)
77 if err != nil {
78 // A bad path is fatal for every backend, so report it here. Falling
79 // through to scp would replace it with "scp not found on host".
80 return nil, err
81 }
82 if !bothRemote {
83 rsync, err := newRsyncTool(opts)
84 if err == nil {
85 if rsync.IsAvailableOnGuest(ctx, paths) {
86 return rsync, nil
87 }
88 logrus.Debugf("rsync not available on guest(s), falling back to scp")
89 } else {
90 logrus.Debugf("rsync not found on host, falling back to scp: %v", err)
91 }
92 }
93
94 tool, err := newSCPTool(opts)
95 if err != nil {
96 // rsync may well have been found and rejected above, so name the
97 // outcome rather than guessing which tool is missing.
98 return nil, fmt.Errorf("no usable copy tool on host: %w", err)
99 }
100 return tool, nil
101 default:
102 return nil, fmt.Errorf("invalid backend %#q, must be one of: scp, rsync, auto", backend)
103 }
104}
105
106func hasRemoteSourceAndDestination(ctx context.Context, paths []string) (bool, error) {
107 copyPaths, err := parseCopyPaths(ctx, paths)
108 if err != nil {
109 return false, err
110 }
111
112 var hasRemoteSource, hasRemoteDestination bool
113 for _, cp := range copyPaths {
114 if cp.IsRemote {
115 if hasRemoteSource {
116 hasRemoteDestination = true
117 } else {
118 hasRemoteSource = true
119 }
120 }
121 }
122
123 return hasRemoteSource && hasRemoteDestination, nil
124}
125
126func parseCopyPaths(ctx context.Context, paths []string) ([]*Path, error) {
127 var copyPaths []*Path
128
129 for _, path := range paths {
130 cp := &Path{}
131 if runtime.GOOS == "windows" {
132 if filepath.IsAbs(path) {
133 var err error
134 path, err = fsutil.WindowsSubsystemPath(ctx, path)
135 if err != nil {
136 return nil, err
137 }
138 } else {
139 path = filepath.ToSlash(path)
140 }
141 }
142
143 parts := strings.SplitN(path, ":", 2)
144 switch len(parts) {
145 case 1:
146 cp.Path = path
147 cp.IsRemote = false
148 case 2:
149 cp.InstanceName = parts[0]
150 cp.Path = parts[1]
151 cp.IsRemote = true
152
153 inst, err := store.Inspect(ctx, cp.InstanceName)
154 if err != nil {
155 if errors.Is(err, os.ErrNotExist) {
156 return nil, fmt.Errorf("instance %#q does not exist, run `limactl create %s` to create a new instance", cp.InstanceName, cp.InstanceName)
157 }
158 return nil, err
159 }
160 if inst.Status == limatype.StatusStopped {
161 return nil, fmt.Errorf("instance %#q is stopped, run `limactl start %s` to start the instance", cp.InstanceName, cp.InstanceName)
162 }
163 cp.Instance = inst
164 default:
165 return nil, fmt.Errorf("path %#q contains multiple colons", path)
166 }
167
168 copyPaths = append(copyPaths, cp)
169 }
170
171 return copyPaths, nil
172}