跳到主要内容

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

浏览文件

managers.go

161 行 · 5.3 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4// Package autostart manage start at login unit files for darwin/linux
5package autostart
6
7import (
8 "context"
9 "errors"
10 "fmt"
11 "maps"
12 "os"
13 "path/filepath"
14 "runtime"
15
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/textutil"
19)
20
21type notSupportedManager struct{}
22
23var _ autoStartManager = (*notSupportedManager)(nil)
24
25var ErrNotSupported = fmt.Errorf("autostart is not supported on %s", runtime.GOOS)
26
27func (*notSupportedManager) IsRegistered(_ context.Context, _ *limatype.Instance) (bool, error) {
28 return false, ErrNotSupported
29}
30
31func (*notSupportedManager) RegisterToStartAtLogin(_ context.Context, _ *limatype.Instance) error {
32 return ErrNotSupported
33}
34
35func (*notSupportedManager) UnregisterFromStartAtLogin(_ context.Context, _ *limatype.Instance) error {
36 return ErrNotSupported
37}
38
39func (*notSupportedManager) AutoStartedIdentifier() string {
40 return ""
41}
42
43func (*notSupportedManager) RequestStart(_ context.Context, _ *limatype.Instance) error {
44 return ErrNotSupported
45}
46
47func (*notSupportedManager) RequestStop(_ context.Context, _ *limatype.Instance) (bool, error) {
48 return false, ErrNotSupported
49}
50
51// TemplateFileBasedManager is an autostart manager that uses a template file to create the autostart entry.
52type TemplateFileBasedManager struct {
53 enabler func(ctx context.Context, enable bool, instName string) error
54 filePath func(instName string) string
55 template string
56 extraTemplateVars map[string]string
57 autoStartedIdentifier func() string
58 requestStart func(ctx context.Context, inst *limatype.Instance) error
59 requestStop func(ctx context.Context, inst *limatype.Instance) (bool, error)
60}
61
62var _ autoStartManager = (*TemplateFileBasedManager)(nil)
63
64func (t *TemplateFileBasedManager) IsRegistered(_ context.Context, inst *limatype.Instance) (bool, error) {
65 if t.filePath == nil {
66 return false, errors.New("no filePath function available")
67 }
68 autostartFilePath := t.filePath(inst.Name)
69 if _, err := os.Stat(autostartFilePath); err != nil {
70 if os.IsNotExist(err) {
71 return false, nil
72 }
73 return false, err
74 }
75 return true, nil
76}
77
78func (t *TemplateFileBasedManager) RegisterToStartAtLogin(ctx context.Context, inst *limatype.Instance) error {
79 if _, err := t.IsRegistered(ctx, inst); err != nil {
80 return fmt.Errorf("failed to check if the autostart entry for instance %#q is registered: %w", inst.Name, err)
81 }
82 content, err := t.renderTemplate(inst.Name, inst.Dir, os.Executable)
83 if err != nil {
84 return fmt.Errorf("failed to render the autostart entry for instance %#q: %w", inst.Name, err)
85 }
86 entryFilePath := t.filePath(inst.Name)
87 if err := os.MkdirAll(filepath.Dir(entryFilePath), os.ModePerm); err != nil {
88 return fmt.Errorf("failed to create the directory for the autostart entry for instance %#q: %w", inst.Name, err)
89 }
90 if err := os.WriteFile(entryFilePath, content, 0o644); err != nil {
91 return fmt.Errorf("failed to write the autostart entry for instance %#q: %w", inst.Name, err)
92 }
93 if t.enabler != nil {
94 return t.enabler(ctx, true, inst.Name)
95 }
96 return nil
97}
98
99func (t *TemplateFileBasedManager) UnregisterFromStartAtLogin(ctx context.Context, inst *limatype.Instance) error {
100 if registered, err := t.IsRegistered(ctx, inst); err != nil {
101 return fmt.Errorf("failed to check if the autostart entry for instance %#q is registered: %w", inst.Name, err)
102 } else if !registered {
103 return nil
104 }
105 if t.enabler != nil {
106 if err := t.enabler(ctx, false, inst.Name); err != nil {
107 return fmt.Errorf("failed to disable the autostart entry for instance %#q: %w", inst.Name, err)
108 }
109 }
110 if err := os.Remove(t.filePath(inst.Name)); err != nil {
111 return fmt.Errorf("failed to remove the autostart entry for instance %#q: %w", inst.Name, err)
112 }
113 return nil
114}
115
116func (t *TemplateFileBasedManager) renderTemplate(instName, workDir string, getExecutable func() (string, error)) ([]byte, error) {
117 if t.template == "" {
118 return nil, errors.New("no template available")
119 }
120 selfExeAbs, err := getExecutable()
121 if err != nil {
122 return nil, err
123 }
124 // The generated unit must carry LIMA_HOME explicitly: neither launchd nor systemd
125 // inherits the environment of the shell that registered the instance, so without it
126 // the autostarted `limactl start` would look for the instance under the default
127 // ~/.lima and not find one that lives anywhere else.
128 limaHome, err := dirnames.LimaDir()
129 if err != nil {
130 return nil, err
131 }
132 data := map[string]string{
133 "Binary": selfExeAbs,
134 "Instance": instName,
135 "LimaHome": limaHome,
136 "WorkDir": workDir,
137 }
138 maps.Copy(data, t.extraTemplateVars)
139 return textutil.ExecuteTemplate(t.template, data)
140}
141
142func (t *TemplateFileBasedManager) AutoStartedIdentifier() string {
143 if t.autoStartedIdentifier != nil {
144 return t.autoStartedIdentifier()
145 }
146 return ""
147}
148
149func (t *TemplateFileBasedManager) RequestStart(ctx context.Context, inst *limatype.Instance) error {
150 if t.requestStart == nil {
151 return errors.New("no RequestStart function available")
152 }
153 return t.requestStart(ctx, inst)
154}
155
156func (t *TemplateFileBasedManager) RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error) {
157 if t.requestStop == nil {
158 return false, errors.New("no RequestStop function available")
159 }
160 return t.requestStop(ctx, inst)
161}