v2.31.0-dev8 ec8c5b9

local.go RawHistoryFolder

1// Package cli implements the CLI adapter for Gordon.
2package cli
3
4import (
5	"context"
6	"fmt"
7	"path/filepath"
8	"strings"
9
10	"github.com/bnema/zerowrap"
11	"github.com/spf13/viper"
12
13	"github.com/bnema/gordon/internal/adapters/out/domainsecrets"
14	"github.com/bnema/gordon/internal/app"
15	"github.com/bnema/gordon/internal/boundaries/in"
16	"github.com/bnema/gordon/internal/boundaries/out"
17	"github.com/bnema/gordon/internal/domain"
18	"github.com/bnema/gordon/internal/usecase/config"
19	secretsSvc "github.com/bnema/gordon/internal/usecase/secrets"
20)
21
22// LocalServices provides direct access to local services for CLI operations.
23type LocalServices struct {
24	configSvc in.ConfigService
25	secretSvc in.SecretService
26	dataDir   string
27	tlsPort   int
28}
29
30// GetConfigService returns the config service.
31func (l *LocalServices) GetConfigService() in.ConfigService {
32	return l.configSvc
33}
34
35// GetSecretService returns the secret service.
36func (l *LocalServices) GetSecretService() in.SecretService {
37	return l.secretSvc
38}
39
40// GetDataDir returns the data directory.
41func (l *LocalServices) GetDataDir() string {
42	return l.dataDir
43}
44
45// GetTLSPort returns the configured TLS port (0 means internal TLS is disabled).
46func (l *LocalServices) GetTLSPort() int {
47	return l.tlsPort
48}
49
50// GetLocalServices creates local services for CLI operations.
51// It loads the config and initializes services without starting the server.
52func GetLocalServices(configPath string) (*LocalServices, error) {
53	// Set up viper with defaults
54	v := viper.New()
55	v.SetDefault("server.port", 8088)
56	v.SetDefault("server.registry_port", 5000)
57	v.SetDefault("server.data_dir", app.DefaultDataDir())
58
59	// Configure viper with config file paths
60	app.ConfigureViper(v, configPath)
61
62	// Read config file
63	if err := v.ReadInConfig(); err != nil {
64		if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
65			return nil, fmt.Errorf("failed to read config file: %w", err)
66		}
67		// Config file not found is OK for some operations
68	}
69
70	log := zerowrap.New(cliLogConfig)
71
72	// Create config service (without event bus for CLI operations)
73	configSvc := config.NewService(v, nil)
74	ctx := context.Background()
75	if err := configSvc.Load(ctx); err != nil {
76		return nil, fmt.Errorf("failed to load configuration: %w", err)
77	}
78
79	// Determine data directory and env directory
80	dataDir := v.GetString("server.data_dir")
81	if dataDir == "" {
82		dataDir = app.DefaultDataDir()
83	}
84
85	envDir := v.GetString("env.dir")
86	if envDir == "" {
87		envDir = filepath.Join(dataDir, "env")
88	}
89
90	// Create domain secret store using configured backend (pass or file-based)
91	domainSecretStore, err := createLocalDomainSecretStore(v, envDir, log)
92	if err != nil {
93		return nil, fmt.Errorf("failed to create domain secret store: %w", err)
94	}
95	secretSvc := secretsSvc.NewService(domainSecretStore, log, nil)
96
97	return &LocalServices{
98		configSvc: configSvc,
99		secretSvc: secretSvc,
100		dataDir:   dataDir,
101		tlsPort:   v.GetInt("server.tls_port"),
102	}, nil
103}
104
105func createLocalDomainSecretStore(v *viper.Viper, envDir string, log zerowrap.Logger) (out.DomainSecretStore, error) {
106	backend := resolveLocalSecretsBackend(v)
107	switch backend {
108	case domain.SecretsBackendPass:
109		return domainsecrets.NewPassStore(log)
110	case domain.SecretsBackendSops:
111		return nil, fmt.Errorf("sops backend not yet supported for domain secrets")
112	default:
113		return domainsecrets.NewFileStore(envDir, log)
114	}
115}
116
117func resolveLocalSecretsBackend(v *viper.Viper) domain.SecretsBackend {
118	backend := strings.TrimSpace(v.GetString("auth.secrets_backend"))
119	if backend == "" {
120		// Legacy key support for older configs.
121		backend = strings.TrimSpace(v.GetString("secrets.backend"))
122	}
123
124	switch backend {
125	case "pass":
126		return domain.SecretsBackendPass
127	case "sops":
128		return domain.SecretsBackendSops
129	case "unsafe", "":
130		return domain.SecretsBackendUnsafe
131	default:
132		log := zerowrap.Default()
133		log.Warn().Str("backend", backend).Msg("unrecognized secrets backend, falling back to unsafe")
134		return domain.SecretsBackendUnsafe
135	}
136}

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