跳到主要内容

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

浏览文件

abs.go

150 行 · 5.5 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package limatmpl
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "net/url"
11 "path"
12 "path/filepath"
13 "runtime"
14 "strings"
15
16 "github.com/lima-vm/lima/v2/pkg/localpathutil"
17)
18
19// UseAbsLocators will replace all relative template locators with absolute ones, so this template
20// can be stored anywhere and still reference the same base templates and files.
21func (tmpl *Template) UseAbsLocators(ctx context.Context) error {
22 err := tmpl.useAbsLocators(ctx)
23 return tmpl.ClearOnError(err)
24}
25
26func (tmpl *Template) useAbsLocators(ctx context.Context) error {
27 if err := tmpl.Unmarshal(); err != nil {
28 return err
29 }
30 basePath, err := basePath(tmpl.Locator)
31 if err != nil {
32 return err
33 }
34 for i, baseLocator := range tmpl.Config.Base {
35 absLocator, err := absPath(baseLocator.URL, basePath)
36 if err != nil {
37 return err
38 }
39 if i == 0 {
40 // base can be either a single string (URL), or a single locator object, or a list whose first element can be either a string or an object
41 fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!str\")) |= %q\n", absLocator)
42 fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!map\") | .url) |= %q\n", absLocator)
43 fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!seq\" and (.[0] | type) == \"!!str\") | .[0]) |= %q\n", absLocator)
44 fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!seq\" and (.[0] | type) == \"!!map\") | .[0].url) |= %q\n", absLocator)
45 } else {
46 fmt.Fprintf(&tmpl.expr, "| ($a.base[%d] | select(type == \"!!str\")) |= %q\n", i, absLocator)
47 fmt.Fprintf(&tmpl.expr, "| ($a.base[%d] | select(type == \"!!map\") | .url) |= %q\n", i, absLocator)
48 }
49 }
50 for i, p := range tmpl.Config.Probes {
51 if p.File != nil {
52 absLocator, err := absPath(p.File.URL, basePath)
53 if err != nil {
54 return err
55 }
56 fmt.Fprintf(&tmpl.expr, "| ($a.probes[%d].file | select(type == \"!!str\")) = %q\n", i, absLocator)
57 fmt.Fprintf(&tmpl.expr, "| ($a.probes[%d].file | select(type == \"!!map\") | .url) = %q\n", i, absLocator)
58 }
59 }
60 for i, p := range tmpl.Config.Provision {
61 if p.File != nil {
62 absLocator, err := absPath(p.File.URL, basePath)
63 if err != nil {
64 return err
65 }
66 fmt.Fprintf(&tmpl.expr, "| ($a.provision[%d].file | select(type == \"!!str\")) = %q\n", i, absLocator)
67 fmt.Fprintf(&tmpl.expr, "| ($a.provision[%d].file | select(type == \"!!map\") | .url) = %q\n", i, absLocator)
68 }
69 }
70 return tmpl.evalExpr(ctx)
71}
72
73// withVolume adds the volume name of the current working directory to a path without volume name.
74// On Windows filepath.Abs() only returns a "rooted" name, but does not add the volume name.
75// withVolume also normalizes all path separators to the platform native one.
76func withVolume(path string) (string, error) {
77 if runtime.GOOS == "windows" && filepath.VolumeName(path) == "" {
78 root, err := filepath.Abs("/")
79 if err != nil {
80 return "", err
81 }
82 path = filepath.VolumeName(root) + path
83 }
84 return filepath.Clean(path), nil
85}
86
87// basePath returns the locator in absolute format, but without the filename part.
88func basePath(locator string) (string, error) {
89 u, err := url.Parse(locator)
90 // Single-letter schemes will be drive names on Windows, e.g. "c:/foo"
91 if err == nil && len(u.Scheme) > 1 {
92 // path.Dir("") returns ".", which must be removed for url.JoinPath() to do the right thing later
93 if u.Opaque != "" {
94 return u.Scheme + ":" + strings.TrimSuffix(path.Dir(u.Opaque), "."), nil
95 }
96 return u.Scheme + "://" + strings.TrimSuffix(path.Dir(path.Join(u.Host, u.Path)), "."), nil
97 }
98 base, err := filepath.Abs(filepath.Dir(locator))
99 if err != nil {
100 return "", err
101 }
102 return withVolume(base)
103}
104
105// absPath either returns the locator directly, or combines it with the basePath if the locator is a relative path.
106func absPath(locator, basePath string) (string, error) {
107 if locator == "" {
108 return "", errors.New("locator is empty")
109 }
110 u, err := url.Parse(locator)
111 if err == nil && len(u.Scheme) > 1 {
112 return locator, nil
113 }
114 // Don't expand relative path to absolute. Tilde paths however are absolute paths already.
115 if localpathutil.IsTildePath(locator) {
116 locator, err = localpathutil.Expand(locator)
117 if err != nil {
118 return "", err
119 }
120 }
121 // Check for rooted locator; filepath.IsAbs() returns false on Windows when the volume name is missing
122 volumeLen := len(filepath.VolumeName(locator))
123 if locator[volumeLen] != '/' && locator[volumeLen] != filepath.Separator {
124 switch {
125 case basePath == "":
126 return "", errors.New("basePath is empty")
127 case basePath == "-":
128 return "", errors.New("can't use relative paths when reading template from STDIN")
129 case strings.Contains(locator, "../") || strings.Contains(locator, `..\`):
130 // filepath.Join treats a backslash as a separator on Windows, so `..\` escapes the
131 // base directory just like `../`; reject both so the guard holds on every platform.
132 return "", fmt.Errorf("relative locator path %#q must not contain '..' path segments", locator)
133 case volumeLen != 0:
134 return "", fmt.Errorf("relative locator path %#q must not include a volume name", locator)
135 }
136 u, err = url.Parse(basePath)
137 if err != nil {
138 return "", err
139 }
140 if len(u.Scheme) > 1 {
141 // Treat empty "template:" URL as opaque
142 if u.Opaque != "" || (u.Scheme == "template" && u.Host == "") {
143 return u.Scheme + ":" + path.Join(u.Opaque, locator), nil
144 }
145 return u.JoinPath(locator).String(), nil
146 }
147 locator = filepath.Join(basePath, locator)
148 }
149 return withVolume(locator)
150}