Skip to content

Refactor: Extract commands from monolithic main.go #55

@dapi

Description

@dapi

Problem

The cmd/port-selector/main.go file has grown to 625 lines and handles multiple responsibilities:

  • Argument parsing
  • Port selection business logic
  • Output formatting
  • All command implementations (list, scan, forget, lock/unlock)

This violates the Single Responsibility Principle and makes the code harder to maintain and test.

Proposed Solution

Extract commands into a separate package:

internal/
└── commands/
    ├── commands.go  # Common types and interfaces
    ├── run.go       # Main port selection logic
    ├── list.go      # --list command
    ├── scan.go      # --scan command
    ├── forget.go    # --forget, --forget-all commands
    └── lock.go      # --lock, --unlock commands

Benefits

  • Better testability (each command can be tested independently)
  • Cleaner main.go (just argument routing)
  • Easier to add new commands
  • Better code organization

Acceptance Criteria

  • Create internal/commands/ package
  • Extract each command into separate file
  • Keep main.go under 150 lines
  • All existing tests pass
  • Add unit tests for extracted commands

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions