Skip to content

Latest commit

 

History

History
63 lines (50 loc) · 3.8 KB

File metadata and controls

63 lines (50 loc) · 3.8 KB

← Back to Backlog

[SC-005] ✅ DONE - Refactor WSL Manager command dispatch (DRY)

Status: Done (2026-03-09) Priority: Medium Component: tools/pslib/wsl/lib/commands.ps1, tools/pslib/wsl/lib/manager.ps1

Summary: As a WSL manager user, I want action commands to prompt me for missing arguments so that I don't have to remember the exact CLI syntax to use a command. As a developer, I want a single dispatch path so that CLI and interactive menu don't duplicate routing logic.

Description: This item covers two related refactorings:

Phase 1 — Self-validating action functions (done)

The Invoke-WslManager switch block inconsistently handled missing arguments. Some commands passed arguments through, some branched to *Interactive wrappers, and one hard-failed with exit 1. Each action function now owns its argument validation: accept what's given, prompt for what's missing. The *Interactive wrapper functions were merged into their non-Interactive counterparts.

Phase 2 — DRY command dispatch (done)

Invoke-WslManager (CLI entry) and Start-InteractiveMode (TUI entry) both contained switch blocks that mapped commands to action functions — violating DRY. Extracted a single Invoke-WslCommand dispatcher that both call.

Phase 3 — DRY interactive distro selection (done)

Nine action functions contained identical interactive distro selection logic (~30 lines each): fetch distros, show numbered table, prompt for number or name, resolve selection. Extracted Select-WslDistro helper, reducing each caller to a 2-line delegation.

Scope decisions:

  • Extract Invoke-WslCommand dispatcher in commands.ps1 — single switch block mapping command names to action functions
  • Move Invoke-WslManager and Start-InteractiveMode to new manager.ps1 — orchestration separate from action logic
  • Invoke-WslManager calls Invoke-WslCommand after parsing CLI args
  • Start-InteractiveMode maps menu keys to command names, wraps Invoke-WslCommand in a single try/catch instead of per-command try/catch boilerplate
  • Display helpers (Format-DistroListEntry, Show-WslDistroList) stay in commands.ps1 since action functions use them
  • wsl-manager.ps1 updated to source both commands.ps1 and manager.ps1
  • Integration tests updated to source manager.ps1

Acceptance Criteria:

Phase 1 (done):

  • All IsNullOrWhiteSpace checks removed from Invoke-WslManager switch
  • Each action function prompts for missing required arguments
  • Invoke-SetupDockerInteractive merged into Invoke-SetupDocker and removed
  • Invoke-SetupPodmanInteractive merged into Invoke-SetupPodman and removed
  • Invoke-SetupProxyInteractive merged into Invoke-SetupProxy and removed
  • Invoke-SetupUserInteractive merged into Invoke-SetupUser and removed
  • Invoke-RepairInterop created with distro name prompting when not provided
  • repair-interop no longer hard-fails with exit 1

Phase 2:

  • Invoke-WslCommand dispatcher created in commands.ps1
  • Invoke-WslManager and Start-InteractiveMode moved to manager.ps1
  • Invoke-WslManager delegates to Invoke-WslCommand
  • Start-InteractiveMode delegates to Invoke-WslCommand (single try/catch, not per-command)
  • wsl-manager.ps1 sources manager.ps1
  • Integration tests source manager.ps1
  • Unit tests updated/split for new file structure
  • All existing tests continue to pass

Phase 3:

  • Select-WslDistro helper extracted in commands.ps1
  • 8 action functions refactored to use Select-WslDistro
  • Invoke-TerminateDistro delegates selection but keeps running-state validation
  • Invoke-CreateDistro left unchanged (different data source)
  • 11 new unit tests for Select-WslDistro
  • All 810 tests pass

← Back to Backlog