-
Notifications
You must be signed in to change notification settings - Fork 884
Expand file tree
/
Copy pathmanagers.go
More file actions
151 lines (130 loc) · 4.88 KB
/
managers.go
File metadata and controls
151 lines (130 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0
// Package autostart manage start at login unit files for darwin/linux
package autostart
import (
"context"
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"runtime"
"github.com/lima-vm/lima/v2/pkg/limatype"
"github.com/lima-vm/lima/v2/pkg/textutil"
)
type notSupportedManager struct{}
var _ autoStartManager = (*notSupportedManager)(nil)
var ErrNotSupported = fmt.Errorf("autostart is not supported on %s", runtime.GOOS)
func (*notSupportedManager) IsRegistered(_ context.Context, _ *limatype.Instance) (bool, error) {
return false, ErrNotSupported
}
func (*notSupportedManager) RegisterToStartAtLogin(_ context.Context, _ *limatype.Instance) error {
return ErrNotSupported
}
func (*notSupportedManager) UnregisterFromStartAtLogin(_ context.Context, _ *limatype.Instance) error {
return ErrNotSupported
}
func (*notSupportedManager) AutoStartedIdentifier() string {
return ""
}
func (*notSupportedManager) RequestStart(_ context.Context, _ *limatype.Instance) error {
return ErrNotSupported
}
func (*notSupportedManager) RequestStop(_ context.Context, _ *limatype.Instance) (bool, error) {
return false, ErrNotSupported
}
// TemplateFileBasedManager is an autostart manager that uses a template file to create the autostart entry.
type TemplateFileBasedManager struct {
enabler func(ctx context.Context, enable bool, instName string) error
filePath func(instName string) string
template string
extraTemplateVars map[string]string
autoStartedIdentifier func() string
requestStart func(ctx context.Context, inst *limatype.Instance) error
requestStop func(ctx context.Context, inst *limatype.Instance) (bool, error)
}
var _ autoStartManager = (*TemplateFileBasedManager)(nil)
func (t *TemplateFileBasedManager) IsRegistered(_ context.Context, inst *limatype.Instance) (bool, error) {
if t.filePath == nil {
return false, errors.New("no filePath function available")
}
autostartFilePath := t.filePath(inst.Name)
if _, err := os.Stat(autostartFilePath); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func (t *TemplateFileBasedManager) RegisterToStartAtLogin(ctx context.Context, inst *limatype.Instance) error {
if _, err := t.IsRegistered(ctx, inst); err != nil {
return fmt.Errorf("failed to check if the autostart entry for instance %q is registered: %w", inst.Name, err)
}
content, err := t.renderTemplate(inst.Name, inst.Dir, os.Executable)
if err != nil {
return fmt.Errorf("failed to render the autostart entry for instance %q: %w", inst.Name, err)
}
entryFilePath := t.filePath(inst.Name)
if err := os.MkdirAll(filepath.Dir(entryFilePath), os.ModePerm); err != nil {
return fmt.Errorf("failed to create the directory for the autostart entry for instance %q: %w", inst.Name, err)
}
if err := os.WriteFile(entryFilePath, content, 0o644); err != nil {
return fmt.Errorf("failed to write the autostart entry for instance %q: %w", inst.Name, err)
}
if t.enabler != nil {
return t.enabler(ctx, true, inst.Name)
}
return nil
}
func (t *TemplateFileBasedManager) UnregisterFromStartAtLogin(ctx context.Context, inst *limatype.Instance) error {
if registered, err := t.IsRegistered(ctx, inst); err != nil {
return fmt.Errorf("failed to check if the autostart entry for instance %q is registered: %w", inst.Name, err)
} else if !registered {
return nil
}
if t.enabler != nil {
if err := t.enabler(ctx, false, inst.Name); err != nil {
return fmt.Errorf("failed to disable the autostart entry for instance %q: %w", inst.Name, err)
}
}
if err := os.Remove(t.filePath(inst.Name)); err != nil {
return fmt.Errorf("failed to remove the autostart entry for instance %q: %w", inst.Name, err)
}
return nil
}
func (t *TemplateFileBasedManager) renderTemplate(instName, workDir string, getExecutable func() (string, error)) ([]byte, error) {
if t.template == "" {
return nil, errors.New("no template available")
}
selfExeAbs, err := getExecutable()
if err != nil {
return nil, err
}
data := map[string]string{
"Binary": selfExeAbs,
"Instance": instName,
"WorkDir": workDir,
}
maps.Copy(data, t.extraTemplateVars)
return textutil.ExecuteTemplate(t.template, data)
}
func (t *TemplateFileBasedManager) AutoStartedIdentifier() string {
if t.autoStartedIdentifier != nil {
return t.autoStartedIdentifier()
}
return ""
}
func (t *TemplateFileBasedManager) RequestStart(ctx context.Context, inst *limatype.Instance) error {
if t.requestStart == nil {
return errors.New("no RequestStart function available")
}
return t.requestStart(ctx, inst)
}
func (t *TemplateFileBasedManager) RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error) {
if t.requestStop == nil {
return false, errors.New("no RequestStop function available")
}
return t.requestStop(ctx, inst)
}