跳到主要内容

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

浏览文件

limainfo.go

138 行 · 4.4 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package limainfo
5
6import (
7 "context"
8 "errors"
9 "io/fs"
10 "path/filepath"
11 "runtime"
12
13 "github.com/sirupsen/logrus"
14
15 "github.com/lima-vm/lima/v2/pkg/envutil"
16 "github.com/lima-vm/lima/v2/pkg/limatype"
17 "github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
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/plugins"
21 "github.com/lima-vm/lima/v2/pkg/registry"
22 "github.com/lima-vm/lima/v2/pkg/templatestore"
23 "github.com/lima-vm/lima/v2/pkg/usrlocal"
24 "github.com/lima-vm/lima/v2/pkg/version"
25)
26
27type LimaInfo struct {
28 Version string `json:"version"`
29 Templates []templatestore.Template `json:"templates"`
30 DefaultTemplate *limatype.LimaYAML `json:"defaultTemplate"`
31 LimaHome string `json:"limaHome"`
32 VMTypes []string `json:"vmTypes"` // since Lima v0.14.2
33 VMTypesEx map[string]DriverExt `json:"vmTypesEx"` // since Lima v2.0.0
34 GuestAgents map[limatype.Arch]GuestAgent `json:"guestAgents"` // since Lima v1.1.0
35 ShellEnvBlock []string `json:"shellEnvBlock"` // since Lima v2.0.0
36 HostOS string `json:"hostOS"` // since Lima v2.0.0
37 HostArch string `json:"hostArch"` // since Lima v2.0.0
38 IdentityFile string `json:"identityFile"` // since Lima v2.0.0
39 Plugins []plugins.Plugin `json:"plugins"` // since Lima v2.0.0
40 LibexecPaths []string `json:"libexecPaths"` // since Lima v2.0.0
41 SharePaths []string `json:"sharePaths"` // since Lima v2.0.0
42}
43
44type DriverExt struct {
45 Location string `json:"location,omitempty"` // since Lima v2.0.0
46}
47
48type GuestAgent struct {
49 Location string `json:"location"` // since Lima v1.1.0
50}
51
52// New returns a LimaInfo object with the Lima version, a list of all Templates and their location,
53// the DefaultTemplate corresponding to template:default with all defaults filled in, the
54// LimaHome location, a list of all supported VMTypes, and a map of GuestAgents for each architecture.
55func New(ctx context.Context) (*LimaInfo, error) {
56 b, err := templatestore.Read(templatestore.Default)
57 if err != nil {
58 return nil, err
59 }
60 y, err := limayaml.Load(ctx, b, "")
61 if err != nil {
62 return nil, err
63 }
64
65 reg := registry.List()
66 if len(reg) == 0 {
67 return nil, errors.New("no VM types found; ensure that the drivers are properly registered")
68 }
69 vmTypesEx := make(map[string]DriverExt)
70 var vmTypes []string
71 for name, path := range reg {
72 vmTypesEx[name] = DriverExt{
73 Location: path,
74 }
75 vmTypes = append(vmTypes, name)
76 }
77
78 info := &LimaInfo{
79 Version: version.Version,
80 DefaultTemplate: y,
81 VMTypes: vmTypes,
82 VMTypesEx: vmTypesEx,
83 GuestAgents: make(map[limatype.Arch]GuestAgent),
84 ShellEnvBlock: envutil.GetDefaultBlockList(),
85 HostOS: runtime.GOOS,
86 HostArch: limatype.NewArch(runtime.GOARCH),
87 }
88 info.Templates, err = templatestore.Templates()
89 if err != nil {
90 return nil, err
91 }
92 info.LimaHome, err = dirnames.LimaDir()
93 if err != nil {
94 return nil, err
95 }
96 configDir, err := dirnames.LimaConfigDir()
97 if err != nil {
98 return nil, err
99 }
100 info.LibexecPaths, err = usrlocal.LibexecLima()
101 if err != nil {
102 return nil, err
103 }
104 info.SharePaths, err = usrlocal.ShareLima()
105 if err != nil {
106 return nil, err
107 }
108 info.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
109 for _, arch := range limatype.ArchTypes {
110 for _, os := range limatype.OSTypes {
111 bin, err := usrlocal.GuestAgentBinary(os, arch)
112 if err != nil {
113 if errors.Is(err, fs.ErrNotExist) {
114 logrus.WithError(err).Debugf("Failed to resolve the guest agent binary for %#q-%#q", os, arch)
115 } else {
116 logrus.WithError(err).Warnf("Failed to resolve the guest agent binary for %#q-%#q", os, arch)
117 }
118 continue
119 }
120 key := arch
121 // For the historical reason, the key does not have "Linux-" prefix
122 if os != limatype.LINUX {
123 key = os + "-" + arch
124 }
125 info.GuestAgents[key] = GuestAgent{
126 Location: bin,
127 }
128 }
129 }
130
131 info.Plugins, err = plugins.Discover()
132 if err != nil {
133 // Don't fail the entire info command if plugin discovery fails.
134 logrus.WithError(err).Warn("Failed to discover plugins")
135 }
136
137 return info, nil
138}