v2.31.0-dev18 7b9923f

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