Skip to content

refactor(cmd): replace init() global pattern with lazy NewXCmd() factory functions (issue #535)#948

Open
Miracle656 wants to merge 1 commit intodotandev:mainfrom
Miracle656:feature/cli-refactor-535
Open

refactor(cmd): replace init() global pattern with lazy NewXCmd() factory functions (issue #535)#948
Miracle656 wants to merge 1 commit intodotandev:mainfrom
Miracle656:feature/cli-refactor-535

Conversation

@Miracle656
Copy link
Copy Markdown
Contributor

Overview

Refactors the CLI framework in internal/cmd to eliminate eager memory allocations caused by Go's init() pattern, replacing global var xyzCmd declarations with lazy factory functions.

Problem

All 33+ Cobra command structs were declared as package-level globals and registered via func init(). This meant every allocat ion — flags, nested command trees, string defaults — happened at process startup regardless of which command was invoked. Under concurrent or repeated CLI invocations this produced non-reclaimable static memory bloat.

Changes

internal/cmd/*.go (all 33 command files)

  • Replaced var xyzCmd = &cobra.Command{...} globals with func NewXyzCmd() *cobra.Command factory functions
  • Moved flag bindings from func init() into their respective factory functions
  • Removed all rootCmd.AddCommand() calls from init() blocks

internal/cmd/root.go

  • Converted var rootCmd to func NewRootCmd() *cobra.Command
  • NewRootCmd() now wires the full command tree by calling all NewXyzCmd() factories
  • Execute() calls NewRootCmd().ExecuteContext(...) — no global state

Tests

  • Updated debug_test.go, doctor_test.go, status_test.go, mem_bench_test.go to use factory functions instead of removed globals
  • Converted generate_test.go and regression_test.go (which were _test.go files the automated refactor skipped) to the factory pattern
  • Added mem_bench_test.go with BenchmarkCLIInit proving allocations are scoped to the call

Verification

  • ✅ All 42 unit tests pass (go test ./internal/cmd/...)
  • ✅ BenchmarkCLIInit confirms allocations are scoped, not static
  • ✅ Public CLI interface unchanged — all commands still available via erst <command>
  • ✅ Backward compatible

Type of Change

  • Performance / refactor
  • New feature
  • Bug fix
  • Breaking change

Checklist

  • Code follows project style guidelines
  • Tests updated to use new factory pattern
  • No new linting issues
  • Public interface preserved
  • Benchmark added proving the refactor works

Closes #535

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 26, 2026

@Miracle656 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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 CLI framework to reduce memory allocations during peak load.

1 participant