Skip to content

Commit 1613e09

Browse files
committed
pkg/autostart: Avoid importing code that is not used during runtime.
Signed-off-by: Norio Nomura <[email protected]>
1 parent 3eb1197 commit 1613e09

File tree

6 files changed

+78
-36
lines changed

6 files changed

+78
-36
lines changed

pkg/autostart/autostart.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ package autostart
66

77
import (
88
"context"
9-
"runtime"
109
"sync"
1110

12-
"github.com/lima-vm/lima/v2/pkg/autostart/systemd"
1311
"github.com/lima-vm/lima/v2/pkg/limatype"
1412
)
1513

@@ -59,15 +57,4 @@ type autoStartManager interface {
5957
RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error)
6058
}
6159

62-
var manager = sync.OnceValue(func() autoStartManager {
63-
switch runtime.GOOS {
64-
case "darwin":
65-
return Launchd
66-
case "linux":
67-
if systemd.IsRunningSystemd() {
68-
return Systemd
69-
}
70-
// TODO: support other init systems
71-
}
72-
return &notSupportedManager{}
73-
})
60+
var manager = sync.OnceValue(Manager)

pkg/autostart/autostart_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ import (
88
"testing"
99

1010
"gotest.tools/v3/assert"
11+
12+
"github.com/lima-vm/lima/v2/pkg/autostart/launchd"
13+
"github.com/lima-vm/lima/v2/pkg/autostart/systemd"
14+
)
15+
16+
var (
17+
Launchd = &TemplateFileBasedManager{
18+
filePath: launchd.GetPlistPath,
19+
template: launchd.Template,
20+
enabler: launchd.EnableDisableService,
21+
autoStartedIdentifier: launchd.AutoStartedServiceName,
22+
requestStart: launchd.RequestStart,
23+
requestStop: launchd.RequestStop,
24+
}
25+
Systemd = &TemplateFileBasedManager{
26+
filePath: systemd.GetUnitPath,
27+
template: systemd.Template,
28+
enabler: systemd.EnableDisableUnit,
29+
autoStartedIdentifier: systemd.AutoStartedUnitName,
30+
requestStart: systemd.RequestStart,
31+
requestStop: systemd.RequestStop,
32+
}
1133
)
1234

1335
func TestRenderTemplate(t *testing.T) {

pkg/autostart/managers.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
"path/filepath"
1313
"runtime"
1414

15-
"github.com/lima-vm/lima/v2/pkg/autostart/launchd"
16-
"github.com/lima-vm/lima/v2/pkg/autostart/systemd"
1715
"github.com/lima-vm/lima/v2/pkg/limatype"
1816
"github.com/lima-vm/lima/v2/pkg/textutil"
1917
)
2018

2119
type notSupportedManager struct{}
2220

21+
var _ autoStartManager = (*notSupportedManager)(nil)
22+
2323
var ErrNotSupported = fmt.Errorf("autostart is not supported on %s", runtime.GOOS)
2424

2525
func (*notSupportedManager) IsRegistered(_ context.Context, _ *limatype.Instance) (bool, error) {
@@ -46,26 +46,6 @@ func (*notSupportedManager) RequestStop(_ context.Context, _ *limatype.Instance)
4646
return false, ErrNotSupported
4747
}
4848

49-
// Launchd is the autostart manager for macOS.
50-
var Launchd = &TemplateFileBasedManager{
51-
filePath: launchd.GetPlistPath,
52-
template: launchd.Template,
53-
enabler: launchd.EnableDisableService,
54-
autoStartedIdentifier: launchd.AutoStartedServiceName,
55-
requestStart: launchd.RequestStart,
56-
requestStop: launchd.RequestStop,
57-
}
58-
59-
// Systemd is the autostart manager for Linux.
60-
var Systemd = &TemplateFileBasedManager{
61-
filePath: systemd.GetUnitPath,
62-
template: systemd.Template,
63-
enabler: systemd.EnableDisableUnit,
64-
autoStartedIdentifier: systemd.AutoStartedUnitName,
65-
requestStart: systemd.RequestStart,
66-
requestStop: systemd.RequestStop,
67-
}
68-
6949
// TemplateFileBasedManager is an autostart manager that uses a template file to create the autostart entry.
7050
type TemplateFileBasedManager struct {
7151
enabler func(ctx context.Context, enable bool, instName string) error
@@ -76,6 +56,8 @@ type TemplateFileBasedManager struct {
7656
requestStop func(ctx context.Context, inst *limatype.Instance) (bool, error)
7757
}
7858

59+
var _ autoStartManager = (*TemplateFileBasedManager)(nil)
60+
7961
func (t *TemplateFileBasedManager) IsRegistered(_ context.Context, inst *limatype.Instance) (bool, error) {
8062
if t.filePath == nil {
8163
return false, errors.New("no filePath function available")

pkg/autostart/managers_darwin.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package autostart
5+
6+
import "github.com/lima-vm/lima/v2/pkg/autostart/launchd"
7+
8+
// Manager returns the autostart manager for Darwin.
9+
func Manager() autoStartManager {
10+
return &TemplateFileBasedManager{
11+
filePath: launchd.GetPlistPath,
12+
template: launchd.Template,
13+
enabler: launchd.EnableDisableService,
14+
autoStartedIdentifier: launchd.AutoStartedServiceName,
15+
requestStart: launchd.RequestStart,
16+
requestStop: launchd.RequestStop,
17+
}
18+
}

pkg/autostart/managers_linux.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package autostart
5+
6+
import "github.com/lima-vm/lima/v2/pkg/autostart/systemd"
7+
8+
// Manager returns the autostart manager for Linux.
9+
func Manager() autoStartManager {
10+
if systemd.IsRunningSystemd() {
11+
return &TemplateFileBasedManager{
12+
filePath: systemd.GetUnitPath,
13+
template: systemd.Template,
14+
enabler: systemd.EnableDisableUnit,
15+
autoStartedIdentifier: systemd.AutoStartedUnitName,
16+
requestStart: systemd.RequestStart,
17+
requestStop: systemd.RequestStop,
18+
}
19+
}
20+
// TODO: add support for non-systemd Linux distros
21+
return &notSupportedManager{}
22+
}

pkg/autostart/managers_others.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !darwin && !linux
2+
3+
// SPDX-FileCopyrightText: Copyright The Lima Authors
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
package autostart
7+
8+
// Manager returns a notSupportedManager for unsupported OSes.
9+
func Manager() autoStartManager {
10+
return &notSupportedManager{}
11+
}

0 commit comments

Comments
 (0)