Skip to content

Commit 90514e7

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 90514e7

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-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/managers.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ 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
)
@@ -46,26 +44,6 @@ func (*notSupportedManager) RequestStop(_ context.Context, _ *limatype.Instance)
4644
return false, ErrNotSupported
4745
}
4846

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-
6947
// TemplateFileBasedManager is an autostart manager that uses a template file to create the autostart entry.
7048
type TemplateFileBasedManager struct {
7149
enabler func(ctx context.Context, enable bool, instName string) error

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)