v2.31.0-dev22 8d342c4

kernel.go RawHistoryFolder

1package app
2
3import (
4	"context"
5	"fmt"
6	"io"
7	"time"
8
9	"github.com/bnema/zerowrap"
10
11	"github.com/bnema/gordon/internal/boundaries/in"
12	configusecase "github.com/bnema/gordon/internal/usecase/config"
13	secretsusecase "github.com/bnema/gordon/internal/usecase/secrets"
14)
15
16// Kernel provides in-process service access for local CLI execution.
17//
18// It intentionally does not start HTTP servers or register signal handlers.
19type Kernel struct {
20	authEnabled     bool
21	configSvc       in.ConfigService
22	secretSvc       in.SecretService
23	containerSvc    in.ContainerService
24	backupSvc       in.BackupService
25	volumeBackupSvc in.VolumeBackupService
26	registrySvc     in.RegistryService
27	healthSvc       in.HealthService
28	logSvc          in.LogService
29	volumeSvc       in.VolumeService
30	publicTLSSvc    in.PublicTLSService
31	cleanup         func()
32}
33
34// NewKernel initializes local services without starting server listeners.
35func NewKernel(configPath string) (*Kernel, error) {
36	return newKernel(configPath, initLogger)
37}
38
39// NewKernelQuiet initializes local services without emitting console logs.
40func NewKernelQuiet(configPath string) (*Kernel, error) {
41	return newKernel(configPath, quietInitLogger)
42}
43
44type kernelLoggerInit func(cfg Config) (zerowrap.Logger, func(), error)
45
46func newKernel(configPath string, initLog kernelLoggerInit) (*Kernel, error) {
47	ctx := context.Background()
48	v, cfg, err := initConfig(configPath)
49	if err != nil {
50		return nil, err
51	}
52
53	log, cleanup, err := initLog(cfg)
54	if err != nil {
55		return nil, err
56	}
57	if cleanup == nil {
58		cleanup = func() {}
59	}
60
61	ctx = zerowrap.WithCtx(ctx, log)
62
63	// Prefer full service wiring so local CLI can execute the same operations
64	// as remote mode without going through HTTP admin endpoints.
65	// ACME Reconcile and renewal loop are only started from runServers,
66	// so there are no side effects for read-only CLI commands.
67	if svc, fullErr := createServicesWithOptions(ctx, v, cfg, log); fullErr == nil {
68		// Wrap cleanup to stop public TLS service (with its renewal loop)
69		// before the logger is cleaned up.
70		wrappedCleanup := func() {
71			if svc.publicTLSSvc != nil {
72				ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
73				defer cancel()
74				if err := svc.publicTLSSvc.Stop(ctx); err != nil {
75					log.Warn().Err(err).Msg("failed to stop public TLS service")
76				}
77			}
78			cleanup()
79		}
80
81		return &Kernel{
82			authEnabled:     cfg.Auth.Enabled,
83			configSvc:       svc.configSvc,
84			secretSvc:       svc.secretSvc,
85			containerSvc:    svc.containerSvc,
86			backupSvc:       svc.backupSvc,
87			volumeBackupSvc: svc.volumeBackupSvc,
88			registrySvc:     svc.registrySvc,
89			healthSvc:       svc.healthSvc,
90			logSvc:          svc.logSvc,
91			volumeSvc:       svc.volumeSvc,
92			publicTLSSvc:    svc.publicTLSSvc,
93			cleanup:         wrappedCleanup,
94		}, nil
95	} else {
96		log.Warn().Err(fullErr).Msg("local kernel running in minimal mode")
97	}
98
99	configSvc := configusecase.NewService(v, nil)
100	if err := configSvc.Load(ctx); err != nil {
101		cleanup()
102		return nil, fmt.Errorf("failed to load configuration: %w", err)
103	}
104
105	_, _, _, domainSecretStore, err := createDomainSecretStore(cfg, log)
106	if err != nil {
107		cleanup()
108		return nil, fmt.Errorf("failed to create local secret store: %w", err)
109	}
110
111	secretSvc := secretsusecase.NewService(domainSecretStore, log, nil)
112
113	return &Kernel{
114		authEnabled: cfg.Auth.Enabled,
115		configSvc:   configSvc,
116		secretSvc:   secretSvc,
117		cleanup:     cleanup,
118	}, nil
119}
120
121func quietInitLogger(Config) (zerowrap.Logger, func(), error) {
122	return zerowrap.New(zerowrap.Config{Level: "disabled", Output: io.Discard}), func() {}, nil
123}
124
125func (k *Kernel) Close() error {
126	if k == nil || k.cleanup == nil {
127		return nil
128	}
129	k.cleanup()
130	return nil
131}
132
133func (k *Kernel) Config() in.ConfigService { return k.configSvc }
134
135func (k *Kernel) Secrets() in.SecretService { return k.secretSvc }
136
137func (k *Kernel) Container() in.ContainerService { return k.containerSvc }
138
139func (k *Kernel) Backup() in.BackupService { return k.backupSvc }
140
141func (k *Kernel) VolumeBackup() in.VolumeBackupService { return k.volumeBackupSvc }
142
143func (k *Kernel) Registry() in.RegistryService { return k.registrySvc }
144
145func (k *Kernel) Health() in.HealthService { return k.healthSvc }
146
147func (k *Kernel) Logs() in.LogService { return k.logSvc }
148
149func (k *Kernel) Volumes() in.VolumeService { return k.volumeSvc }
150
151func (k *Kernel) PublicTLS() in.PublicTLSService { return k.publicTLSSvc }
152
153func (k *Kernel) AuthEnabled() bool { return k != nil && k.authEnabled }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close