跳到主要内容

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

浏览文件

dirnames.go

110 行 · 3.2 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package dirnames
5
6import (
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "strings"
12
13 "github.com/lima-vm/lima/v2/pkg/identifiers"
14 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
15)
16
17// DotLima is a directory that appears under the home directory.
18const DotLima = ".lima"
19
20// LimaDir returns the absolute path of `~/.lima` (or $LIMA_HOME, if set).
21//
22// NOTE: We do not use `~/Library/Application Support/Lima` on macOS.
23// We use `~/.lima` so that we can have enough space for the length of the socket path,
24// which can be only 104 characters on macOS.
25func LimaDir() (string, error) {
26 dir := os.Getenv("LIMA_HOME")
27 if dir == "" {
28 homeDir, err := os.UserHomeDir()
29 if err != nil {
30 return "", err
31 }
32 dir = filepath.Join(homeDir, DotLima)
33 }
34 // Cage keeps a stable logical path for QEMU backing files, SSH and sockets
35 // when the profile's private VM storage is moved to another volume.
36 if stable := os.Getenv("CAGE_LOGICAL_LIMA_HOME"); stable != "" && filepath.Clean(dir) == filepath.Clean(stable) {
37 return filepath.Clean(dir), nil
38 }
39 if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
40 return dir, nil
41 }
42 realdir, err := filepath.EvalSymlinks(dir)
43 if err != nil {
44 return "", fmt.Errorf("cannot evaluate symlinks in %#q: %w", dir, err)
45 }
46 return realdir, nil
47}
48
49// LimaConfigDir returns the path of the config directory, $LIMA_HOME/_config.
50func LimaConfigDir() (string, error) {
51 limaDir, err := LimaDir()
52 if err != nil {
53 return "", err
54 }
55 return filepath.Join(limaDir, filenames.ConfigDir), nil
56}
57
58// LimaNetworksDir returns the path of the networks log directory, $LIMA_HOME/_networks.
59func LimaNetworksDir() (string, error) {
60 limaDir, err := LimaDir()
61 if err != nil {
62 return "", err
63 }
64 return filepath.Join(limaDir, filenames.NetworksDir), nil
65}
66
67// LimaDisksDir returns the path of the disks directory, $LIMA_HOME/_disks.
68func LimaDisksDir() (string, error) {
69 limaDir, err := LimaDir()
70 if err != nil {
71 return "", err
72 }
73 return filepath.Join(limaDir, filenames.DisksDir), nil
74}
75
76// LimaTemplatesDir returns the path of the templates directory, $LIMA_HOME/_templates.
77func LimaTemplatesDir() (string, error) {
78 limaDir, err := LimaDir()
79 if err != nil {
80 return "", err
81 }
82 return filepath.Join(limaDir, filenames.TemplatesDir), nil
83}
84
85// InstanceDir returns the instance dir.
86// InstanceDir does not check whether the instance exists.
87func InstanceDir(name string) (string, error) {
88 if err := ValidateInstName(name); err != nil {
89 return "", err
90 }
91 limaDir, err := LimaDir()
92 if err != nil {
93 return "", err
94 }
95 dir := filepath.Join(limaDir, name)
96 return dir, nil
97}
98
99// ValidateInstName checks if the name is a valid instance name. For this it needs to
100// be a valid identifier, and not end in .yml or .yaml (case insensitively).
101func ValidateInstName(name string) error {
102 if err := identifiers.Validate(name); err != nil {
103 return fmt.Errorf("instance name %#q is not a valid identifier: %w", name, err)
104 }
105 lower := strings.ToLower(name)
106 if strings.HasSuffix(lower, ".yml") || strings.HasSuffix(lower, ".yaml") {
107 return fmt.Errorf("instance name %#q must not end with .yml or .yaml suffix", name)
108 }
109 return nil
110}