Skip to content

refactor: decompose god files, add DI constructors, migrate to slog#251

Merged
jongio merged 1 commit into
mainfrom
patrol/refactoring
May 18, 2026
Merged

refactor: decompose god files, add DI constructors, migrate to slog#251
jongio merged 1 commit into
mainfrom
patrol/refactoring

Conversation

@jongio
Copy link
Copy Markdown
Owner

@jongio jongio commented May 18, 2026

Summary

Refactoring PR addressing 7 issues from the backlog. Decomposes god files, introduces DI patterns, extracts handler boilerplate, clarifies naming, and migrates legacy logging.

Changes

Issue #233 - Split logs.go god file

  • Before: logs.go at 1906 lines - a single file doing parsing, formatting, streaming, and filtering
  • After: 808 lines in logs.go (types, constructors, command, execute, collect) + 4 focused modules:
    • logs_parsing.go (151 lines) - file reading, log line parsing
    • logs_formatting.go (152 lines) - text/JSON display output
    • logs_streaming.go (538 lines) - all follow* streaming methods
    • logs_filtering.go (307 lines) - filter logic, level parsing, validation

Issue #234 - Decompose checker.go

  • Before: checker.go at 1077 lines mixing HTTP checks, command checks, process checks, and orchestration
  • After: 394 lines in checker.go (struct, CheckService, performServiceCheck) + 3 strategy modules:
    • checker_http.go (253 lines) - HTTP health check strategy
    • checker_command.go (134 lines) - command/shell health check strategy
    • checker_process.go (305 lines) - process checks, port checks, error suggestions

Issue #236 - DI for global registries

  • Added wellknown.NewServiceRegistry() for dependency injection (instead of global map)
  • Added service.NewOperationManager(timeout) alongside existing singleton
  • Backward compatibility preserved - existing code unchanged

Issue #191 - Clarify executor naming

  • Renamed service/executor.go to service/service_process.go
  • Added comprehensive doc comments explaining the distinction from internal/executor
  • Added service/doc.go documenting the package's internal domains

Issue #185 - Extract MCP handler boilerplate

  • Created mcp_helpers.go with handleSingleServiceOp() helper
  • Refactored handleStartService and handleRestartService (from 23 lines each to 4 lines)
  • Pattern demonstrated for remaining handlers to adopt incrementally

Issue #201 - Migrate zerolog to slog

  • Converted all zerolog calls in checker.go, checker_http.go, and monitor.go to log/slog
  • Rewrote InitializeLogging() to use slog handlers
  • Pattern: log.Info().Str("k","v").Msg("m") -> slog.Info("m", "k", "v")

Issue #190 - Service package documentation (partial)

  • Added service/doc.go documenting the package's 5 internal domains
  • Full sub-package extraction deferred (43 importers make it too invasive for one PR)

Verification

  • go build ./... passes clean
  • No behavioral changes - all refactoring is structural only
  • All functions maintain their original signatures and semantics

Fixes #233, #234, #236, #191, #185, #201
Partially addresses #190

Split oversized files into focused modules:
- logs.go (1906->808 lines): extracted parsing, formatting, streaming, filtering
- checker.go (1077->394 lines): extracted HTTP, command, and process strategies

Add dependency injection constructors:
- wellknown.NewServiceRegistry() for injectable service definitions
- service.NewOperationManager() alongside existing singleton

Clarify executor naming:
- Renamed service/executor.go to service/service_process.go with doc comments
- Added service/doc.go documenting package domains

Extract MCP handler boilerplate:
- handleSingleServiceOp() eliminates repeated validation/controller setup
- Refactored handleStartService and handleRestartService to use helper

Migrate healthcheck logging from zerolog to log/slog:
- checker.go, checker_http.go, monitor.go converted to structured slog calls
- InitializeLogging rewritten using slog handlers

Addresses: #233, #234, #236, #191, #185, #201
Partially addresses: #190 (doc.go added; full split deferred due to 43 importers)
@jongio jongio force-pushed the patrol/refactoring branch from bdc8657 to 4329ce3 Compare May 18, 2026 16:10
@jongio jongio merged commit d3acba1 into main May 18, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(logs): split 1633-line logs.go god file into focused modules

1 participant