refactor(cli): split CLI and Namespace for clarity

test(cli): coverage for namespace struct
This commit is contained in:
Marc 2024-01-26 00:56:37 -05:00
parent 3206d674cc
commit 18982b5a35
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 66 additions and 30 deletions

View file

@ -13,14 +13,6 @@ type Flags struct {
RawOutput bool
}
// Command definition for CLI subcommands.
type Command struct {
Label string
Handler func([]string, Flags, state.State) error
Usage string
Description string
}
// Represents a CLI invocation.
// Must be initialized with commands via AddCommand before running
// with Run.
@ -29,12 +21,6 @@ type CLI struct {
Metadata map[string]string
}
type Namespace struct {
Label string
Commands map[string]Command
OrderedCommands []string
}
func (c *CLI) AddNamespace(namespace Namespace) *CLI {
if c.Namespaces == nil {
c.Namespaces = map[string]Namespace{}
@ -45,22 +31,6 @@ func (c *CLI) AddNamespace(namespace Namespace) *CLI {
return c
}
// Registers a command.
// This specifies a label that is used to route the user input to
// the right command, a handler that is called when the label is used,
// and usage/description details that get included in autogenerated help messaging.
func (n *Namespace) AddCommand(label string, handler func([]string, Flags, state.State) error, usage string, description string) *Namespace {
if n.Commands == nil {
n.Commands = map[string]Command{}
n.OrderedCommands = []string{}
}
n.OrderedCommands = append(n.OrderedCommands, label)
n.Commands[label] = Command{Label: label, Handler: handler, Usage: usage, Description: description}
return n
}
// Executes one of the registered commands if any match the provided
// user arguments.
func (c CLI) Run(args []string, currentState state.State) error {

35
cli/namespace.go Normal file
View file

@ -0,0 +1,35 @@
package cli
import (
state "v/state"
)
type Namespace struct {
Label string
Commands map[string]Command
OrderedCommands []string
}
// Command definition for CLI subcommands.
type Command struct {
Label string
Handler func([]string, Flags, state.State) error
Usage string
Description string
}
// Registers a command.
// This specifies a label that is used to route the user input to
// the right command, a handler that is called when the label is used,
// and usage/description details that get included in autogenerated help messaging.
func (n *Namespace) AddCommand(label string, handler func([]string, Flags, state.State) error, usage string, description string) *Namespace {
if n.Commands == nil {
n.Commands = map[string]Command{}
n.OrderedCommands = []string{}
}
n.OrderedCommands = append(n.OrderedCommands, label)
n.Commands[label] = Command{Label: label, Handler: handler, Usage: usage, Description: description}
return n
}

31
cli/namespace_test.go Normal file
View file

@ -0,0 +1,31 @@
package cli
import (
"testing"
state "v/state"
)
func TestNamespaceAddCommand(t *testing.T) {
namespace := Namespace{}
canary := 0
handler := func(a []string, b Flags, c state.State) error {
canary = 1
return nil
}
namespace.AddCommand("test", handler, "", "")
if len(namespace.Commands) != 1 {
t.Errorf("Expected one command, found %d", len(namespace.Commands))
}
if e := namespace.Commands["test"].Handler([]string{}, Flags{}, state.State{}); e != nil {
t.Errorf("Unexpected error when running handler: %s", e)
}
if canary != 1 {
t.Errorf("Expected canary value to have been modified.")
}
}