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}
22
23// GetConfigService returns the config service.
24func (l *LocalServices) GetConfigService() in.ConfigService {
25 return l.configSvc
26}
27
28// GetDataDir returns the data directory.
29func (l *LocalServices) GetDataDir() string {
30 return l.dataDir
31}
32
33// HasInternalTLS reports whether a TLS-capable entrypoint enables the internal CA.
34func (l *LocalServices) HasInternalTLS() bool {
35 return l.tlsEnabled
36}
37
38// GetLocalServices creates local services for CLI operations.
39// It loads the config and initializes services without starting the server.
40func GetLocalServices(cliConfigPath string) (*LocalServices, error) {
41 // Set up viper with defaults
42 v := viper.New()
43 v.SetDefault("server.port", 8088)
44 v.SetDefault("server.registry_port", 5000)
45 v.SetDefault("server.data_dir", app.DefaultDataDir())
46
47 // Configure viper with config file paths
48 app.ConfigureViper(v, cliConfigPath)
49
50 // Read config file
51 if err := v.ReadInConfig(); err != nil {
52 if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
53 return nil, fmt.Errorf("failed to read config file: %w", err)
54 }
55 // Config file not found is OK for some operations
56 }
57
58 // Create config service (without event bus for CLI operations)
59 configSvc := config.NewService(v, nil)
60 ctx := context.Background()
61 if err := configSvc.Load(ctx); err != nil {
62 return nil, fmt.Errorf("failed to load configuration: %w", err)
63 }
64
65 dataDir := v.GetString("server.data_dir")
66 if dataDir == "" {
67 dataDir = app.DefaultDataDir()
68 }
69
70 return &LocalServices{
71 configSvc: configSvc,
72 dataDir: dataDir,
73 tlsEnabled: hasLocalTLSCapableEntrypoint(v),
74 }, nil
75}
76
77func hasLocalTLSCapableEntrypoint(v *viper.Viper) bool {
78 for _, rawEntryPoint := range v.GetStringMap("entrypoints") {
79 entryPoint, ok := rawEntryPoint.(map[string]any)
80 if !ok {
81 continue
82 }
83 switch strings.TrimSpace(fmt.Sprint(entryPoint["protocol"])) {
84 case "smart_tcp", "tls_mux":
85 return true
86 }
87 }
88 return false
89}