cli + daemon: move test seams off package globals onto injected structs

CLI: introduce internal/cli.deps which owns every RPC/SSH/host-command
seam the tree used to reach through mutable package vars. Command
builders, orchestrators, and the completion helpers become methods on
*deps. Tests construct their own deps per case, so fakes no longer leak
across cases and tests are free to run in parallel.

Daemon: move workspaceInspectRepoFunc + workspaceImportFunc onto the
Daemon struct (workspaceInspectRepo / workspaceImport), mirroring the
existing guestWaitForSSH / guestDial pattern. Workspace-prepare tests
drop t.Parallel() guards now that they no longer mutate process-wide
state.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Thales Maciel 2026-04-19 19:03:55 -03:00
parent d38f580e00
commit c42fcbe012
No known key found for this signature in database
GPG key ID: 33112E6833C34679
19 changed files with 664 additions and 733 deletions

View file

@ -12,30 +12,30 @@ import (
"github.com/spf13/cobra"
)
func newKernelCommand() *cobra.Command {
func (d *deps) newKernelCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "kernel",
Short: "Manage the local kernel catalog",
RunE: helpNoArgs,
}
cmd.AddCommand(
newKernelListCommand(),
newKernelShowCommand(),
newKernelRmCommand(),
newKernelImportCommand(),
newKernelPullCommand(),
d.newKernelListCommand(),
d.newKernelShowCommand(),
d.newKernelRmCommand(),
d.newKernelImportCommand(),
d.newKernelPullCommand(),
)
return cmd
}
func newKernelPullCommand() *cobra.Command {
func (d *deps) newKernelPullCommand() *cobra.Command {
var force bool
cmd := &cobra.Command{
Use: "pull <name>",
Short: "Download a cataloged kernel bundle",
Args: exactArgsUsage(1, "usage: banger kernel pull <name> [--force]"),
RunE: func(cmd *cobra.Command, args []string) error {
layout, _, err := ensureDaemon(cmd.Context())
layout, _, err := d.ensureDaemon(cmd.Context())
if err != nil {
return err
}
@ -55,7 +55,7 @@ func newKernelPullCommand() *cobra.Command {
return cmd
}
func newKernelImportCommand() *cobra.Command {
func (d *deps) newKernelImportCommand() *cobra.Command {
var params api.KernelImportParams
cmd := &cobra.Command{
Use: "import <name>",
@ -72,7 +72,7 @@ func newKernelImportCommand() *cobra.Command {
return err
}
params.FromDir = abs
layout, _, err := ensureDaemon(cmd.Context())
layout, _, err := d.ensureDaemon(cmd.Context())
if err != nil {
return err
}
@ -89,7 +89,7 @@ func newKernelImportCommand() *cobra.Command {
return cmd
}
func newKernelListCommand() *cobra.Command {
func (d *deps) newKernelListCommand() *cobra.Command {
var available bool
cmd := &cobra.Command{
Use: "list",
@ -97,7 +97,7 @@ func newKernelListCommand() *cobra.Command {
Short: "List kernels (local by default, or --available for the catalog)",
Args: noArgsUsage("usage: banger kernel list [--available]"),
RunE: func(cmd *cobra.Command, args []string) error {
layout, _, err := ensureDaemon(cmd.Context())
layout, _, err := d.ensureDaemon(cmd.Context())
if err != nil {
return err
}
@ -119,14 +119,14 @@ func newKernelListCommand() *cobra.Command {
return cmd
}
func newKernelShowCommand() *cobra.Command {
func (d *deps) newKernelShowCommand() *cobra.Command {
return &cobra.Command{
Use: "show <name>",
Short: "Show kernel catalog entry details",
Args: exactArgsUsage(1, "usage: banger kernel show <name>"),
ValidArgsFunction: completeKernelNameOnlyAtPos0,
ValidArgsFunction: d.completeKernelNameOnlyAtPos0,
RunE: func(cmd *cobra.Command, args []string) error {
layout, _, err := ensureDaemon(cmd.Context())
layout, _, err := d.ensureDaemon(cmd.Context())
if err != nil {
return err
}
@ -139,15 +139,15 @@ func newKernelShowCommand() *cobra.Command {
}
}
func newKernelRmCommand() *cobra.Command {
func (d *deps) newKernelRmCommand() *cobra.Command {
return &cobra.Command{
Use: "rm <name>",
Aliases: []string{"remove", "delete"},
Short: "Remove a kernel catalog entry",
Args: exactArgsUsage(1, "usage: banger kernel rm <name>"),
ValidArgsFunction: completeKernelNameOnlyAtPos0,
ValidArgsFunction: d.completeKernelNameOnlyAtPos0,
RunE: func(cmd *cobra.Command, args []string) error {
layout, _, err := ensureDaemon(cmd.Context())
layout, _, err := d.ensureDaemon(cmd.Context())
if err != nil {
return err
}