local.go RawHistoryFolder
1// Package cli implements the CLI adapter for Gordon.
2package cli
3
4import (
5 "context"
6 "fmt"
7 "strings"
8
9 "github.com/spf13/viper"
10
11 "github.com/bnema/gordon/internal/app"
12 "github.com/bnema/gordon/internal/boundaries/in"
13 "github.com/bnema/gordon/internal/usecase/config"
14)
15
16// LocalServices provides direct access to local services for CLI operations.
17type LocalServices struct {
18 configSvc in.ConfigService
19 dataDir string
20 tlsEnabled bool
21 configFile string
22}
23
24// GetConfigService returns the config service.
25func (l *LocalServices) GetConfigService() in.ConfigService {
26 return l.configSvc
27}
28
29// GetDataDir returns the data directory.
30func (l *LocalServices) GetDataDir() string {
31 return l.dataDir
32}
33
34// GetConfigFile returns the config file that was read, or "" when none was found.
35func (l *LocalServices) GetConfigFile() string {
36 return l.configFile
37}
38
39// HasInternalTLS reports whether a TLS-capable entrypoint enables the internal CA.
40func (l *LocalServices) HasInternalTLS() bool {
41 return l.tlsEnabled
42}
43
44// GetLocalServices creates local services for CLI operations.
45// It loads the config and initializes services without starting the server.
46func GetLocalServices(cliConfigPath string) (*LocalServices, error) {
47 // Set up viper with defaults
48 v := viper.New()
49 v.SetDefault("server.port", 8088)
50 v.SetDefault("server.registry_port", 5000)
51 v.SetDefault("server.data_dir", app.DefaultDataDir())
52
53 // Configure viper with config file paths
54 app.ConfigureViper(v, cliConfigPath)
55
56 // Read config file
57 if err := v.ReadInConfig(); err != nil {
58 if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
59 return nil, fmt.Errorf("failed to read config file: %w", err)
60 }
61 // Config file not found is OK for some operations
62 }
63
64 // Create config service (without event bus for CLI operations)
65 configSvc := config.NewService(v, nil)
66 ctx := context.Background()
67 if err := configSvc.Load(ctx); err != nil {
68 return nil, fmt.Errorf("failed to load configuration: %w", err)
69 }
70
71 dataDir := v.GetString("server.data_dir")
72 if dataDir == "" {
73 dataDir = app.DefaultDataDir()
74 }
75
76 return &LocalServices{
77 configSvc: configSvc,
78 dataDir: dataDir,
79 tlsEnabled: hasLocalTLSCapableEntrypoint(v),
80 configFile: v.ConfigFileUsed(),
81 }, nil
82}
83
84func hasLocalTLSCapableEntrypoint(v *viper.Viper) bool {
85 for _, rawEntryPoint := range v.GetStringMap("entrypoints") {
86 entryPoint, ok := rawEntryPoint.(map[string]any)
87 if !ok {
88 continue
89 }
90 switch strings.TrimSpace(fmt.Sprint(entryPoint["protocol"])) {
91 case "smart_tcp", "tls_mux":
92 return true
93 }
94 }
95 return false
96}