refactor: extract resolveLogFilePath to reduce cyclomatic complexity
Extracts the log file path resolution logic into a separate helper function to bring createServices below the complexity threshold.
- commit
- 9ed3e413dcf90b09a38b662101c479387275791d
- author
- brice · · 2026-01-17 09:20 UTC
- parent
- 25f0ae5
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +12 -6
M internal/app/run.go +12 -6
| @@ -322,12 +322,7 @@ func createServices(ctx context.Context, v *viper.Viper, cfg Config, log zerowra | |||
| 322 | 322 | healthSvc := health.NewService(svc.configSvc, svc.containerSvc, prober, log) | |
| 323 | 323 | ||
| 324 | 324 | // Create log service for accessing logs via admin API | |
| 325 | - | logFilePath := cfg.Logging.File.Path | |
| 326 | - | if logFilePath == "" && cfg.Logging.File.Enabled { | |
| 327 | - | // Default to {data_dir}/logs/gordon.log when file logging enabled but no path specified | |
| 328 | - | logFilePath = filepath.Join(cfg.Server.DataDir, "logs", "gordon.log") | |
| 329 | - | } | |
| 330 | - | logSvc := logs.NewService(logFilePath, svc.containerSvc, svc.runtime, log) | |
| 325 | + | logSvc := logs.NewService(resolveLogFilePath(cfg), svc.containerSvc, svc.runtime, log) | |
| 331 | 326 | ||
| 332 | 327 | // Create admin handler for admin API | |
| 333 | 328 | svc.adminHandler = admin.NewHandler(svc.configSvc, svc.authSvc, svc.containerSvc, healthSvc, secretSvc, logSvc, svc.eventBus, log) | |
| @@ -335,6 +330,17 @@ func createServices(ctx context.Context, v *viper.Viper, cfg Config, log zerowra | |||
| 335 | 330 | return svc, nil | |
| 336 | 331 | } | |
| 337 | 332 | ||
| 333 | + | // resolveLogFilePath returns the configured log file path or a default. | |
| 334 | + | func resolveLogFilePath(cfg Config) string { | |
| 335 | + | if cfg.Logging.File.Path != "" { | |
| 336 | + | return cfg.Logging.File.Path | |
| 337 | + | } | |
| 338 | + | if cfg.Logging.File.Enabled { | |
| 339 | + | return filepath.Join(cfg.Server.DataDir, "logs", "gordon.log") | |
| 340 | + | } | |
| 341 | + | return "" | |
| 342 | + | } | |
| 343 | + | ||
| 338 | 344 | // createOutputAdapters creates the Docker runtime and event bus. | |
| 339 | 345 | func createOutputAdapters(ctx context.Context, log zerowrap.Logger) (*docker.Runtime, *eventbus.InMemory, error) { | |
| 340 | 346 | runtime, err := docker.NewRuntime() | |
| @@ -322,12 +322,7 @@ func createServices(ctx context.Context, v *viper.Viper, cfg Config, log zerowra | |||||
| 322 | healthSvc := health.NewService(svc.configSvc, svc.containerSvc, prober, log) | 322 | healthSvc := health.NewService(svc.configSvc, svc.containerSvc, prober, log) | ||
| 323 | 323 | ||||
| 324 | // Create log service for accessing logs via admin API | 324 | // Create log service for accessing logs via admin API | ||
| 325 | - | logFilePath := cfg.Logging.File.Path | 325 | + | logSvc := logs.NewService(resolveLogFilePath(cfg), svc.containerSvc, svc.runtime, log) |
| 326 | - | if logFilePath == "" && cfg.Logging.File.Enabled { | |||
| 327 | - | // Default to {data_dir}/logs/gordon.log when file logging enabled but no path specified | |||
| 328 | - | logFilePath = filepath.Join(cfg.Server.DataDir, "logs", "gordon.log") | |||
| 329 | - | } | |||
| 330 | - | logSvc := logs.NewService(logFilePath, svc.containerSvc, svc.runtime, log) | |||
| 331 | 326 | ||||
| 332 | // Create admin handler for admin API | 327 | // Create admin handler for admin API | ||
| 333 | svc.adminHandler = admin.NewHandler(svc.configSvc, svc.authSvc, svc.containerSvc, healthSvc, secretSvc, logSvc, svc.eventBus, log) | 328 | svc.adminHandler = admin.NewHandler(svc.configSvc, svc.authSvc, svc.containerSvc, healthSvc, secretSvc, logSvc, svc.eventBus, log) | ||
| @@ -335,6 +330,17 @@ func createServices(ctx context.Context, v *viper.Viper, cfg Config, log zerowra | |||||
| 335 | return svc, nil | 330 | return svc, nil | ||
| 336 | } | 331 | } | ||
| 337 | 332 | ||||
| 333 | + | // resolveLogFilePath returns the configured log file path or a default. | |||
| 334 | + | func resolveLogFilePath(cfg Config) string { | |||
| 335 | + | if cfg.Logging.File.Path != "" { | |||
| 336 | + | return cfg.Logging.File.Path | |||
| 337 | + | } | |||
| 338 | + | if cfg.Logging.File.Enabled { | |||
| 339 | + | return filepath.Join(cfg.Server.DataDir, "logs", "gordon.log") | |||
| 340 | + | } | |||
| 341 | + | return "" | |||
| 342 | + | } | |||
| 343 | + | ||||
| 338 | // createOutputAdapters creates the Docker runtime and event bus. | 344 | // createOutputAdapters creates the Docker runtime and event bus. | ||
| 339 | func createOutputAdapters(ctx context.Context, log zerowrap.Logger) (*docker.Runtime, *eventbus.InMemory, error) { | 345 | func createOutputAdapters(ctx context.Context, log zerowrap.Logger) (*docker.Runtime, *eventbus.InMemory, error) { | ||
| 340 | runtime, err := docker.NewRuntime() | 346 | runtime, err := docker.NewRuntime() | ||