跳到主要内容

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

浏览文件

store.go

131 行 · 3.0 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package store
5
6import (
7 "context"
8 "errors"
9 "os"
10 "path/filepath"
11 "strings"
12
13 "github.com/lima-vm/lima/v2/pkg/driverutil"
14 "github.com/lima-vm/lima/v2/pkg/identifiers"
15 "github.com/lima-vm/lima/v2/pkg/limatype"
16 "github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
17 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
18 "github.com/lima-vm/lima/v2/pkg/limayaml"
19)
20
21// Directory returns the LimaDir.
22func Directory() string {
23 limaDir, err := dirnames.LimaDir()
24 if err != nil {
25 return ""
26 }
27 return limaDir
28}
29
30// Validate checks the LimaDir.
31func Validate() error {
32 limaDir, err := dirnames.LimaDir()
33 if err != nil {
34 return err
35 }
36 names, err := Instances()
37 if err != nil {
38 return err
39 }
40 for _, name := range names {
41 // Each instance directory needs to have limayaml
42 instDir := filepath.Join(limaDir, name)
43 yamlPath := filepath.Join(instDir, filenames.LimaYAML)
44 if _, err := os.Stat(yamlPath); err != nil {
45 return err
46 }
47 }
48 return nil
49}
50
51// Instances returns the names of the instances under LimaDir.
52func Instances() ([]string, error) {
53 limaDir, err := dirnames.LimaDir()
54 if err != nil {
55 return nil, err
56 }
57 limaDirList, err := os.ReadDir(limaDir)
58 if err != nil {
59 if errors.Is(err, os.ErrNotExist) {
60 return nil, nil
61 }
62 return nil, err
63 }
64 var names []string
65 for _, f := range limaDirList {
66 if strings.HasPrefix(f.Name(), ".") || strings.HasPrefix(f.Name(), "_") {
67 continue
68 }
69 if !f.IsDir() {
70 continue
71 }
72 names = append(names, f.Name())
73 }
74 return names, nil
75}
76
77func Disks() ([]string, error) {
78 limaDiskDir, err := dirnames.LimaDisksDir()
79 if err != nil {
80 return nil, err
81 }
82 limaDiskDirList, err := os.ReadDir(limaDiskDir)
83 if err != nil {
84 if errors.Is(err, os.ErrNotExist) {
85 return nil, nil
86 }
87 return nil, err
88 }
89 var names []string
90 for _, f := range limaDiskDirList {
91 names = append(names, f.Name())
92 }
93 return names, nil
94}
95
96func DiskDir(name string) (string, error) {
97 if err := identifiers.Validate(name); err != nil {
98 return "", err
99 }
100 limaDisksDir, err := dirnames.LimaDisksDir()
101 if err != nil {
102 return "", err
103 }
104 dir := filepath.Join(limaDisksDir, name)
105 return dir, nil
106}
107
108// LoadYAMLByFilePath loads and validates the yaml.
109func LoadYAMLByFilePath(ctx context.Context, filePath string) (*limatype.LimaYAML, error) {
110 // We need to use the absolute path because it may be used to determine hostSocket locations.
111 absPath, err := filepath.Abs(filePath)
112 if err != nil {
113 return nil, err
114 }
115 yContent, err := os.ReadFile(absPath)
116 if err != nil {
117 return nil, err
118 }
119 y, err := limayaml.Load(ctx, yContent, absPath)
120 if err != nil {
121 return nil, err
122 }
123 // If VMType is not specified, we go with the default platform driver.
124 if err := driverutil.ResolveVMType(y); err != nil {
125 return nil, errors.Join(errors.New("failed to resolve vm for "+absPath), err)
126 }
127 if err := limayaml.Validate(y, false); err != nil {
128 return nil, err
129 }
130 return y, nil
131}