Command is now a DTO with no root/child awareness:
- Path-based registration: c.Command("deploy/to/homelab", handler)
- Description is an i18n key derived from path: cmd.deploy.to.homelab.description
- Lifecycle: Run(), Start(), Stop(), Restart(), Reload(), Signal()
- All return core.Result — errors flow through Core internally
- Parent commands auto-created from path segments
Cli is now a surface layer that reads from Core's command registry:
- Resolves os.Args to command path
- Parses flags into Options (--port=8080 → Option{K:"port", V:"8080"})
- Calls command action with parsed Options
- Banner and help use i18n
Old Clir code preserved in tests/testdata/cli_clir.go.bak for reference.
211 tests, 77.5% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package core_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
. "forge.lthn.ai/core/go/pkg/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// --- Cli Surface ---
|
|
|
|
func TestCli_Good(t *testing.T) {
|
|
c := New()
|
|
assert.NotNil(t, c.Cli())
|
|
}
|
|
|
|
func TestCli_Banner_Good(t *testing.T) {
|
|
c := New(Options{{K: "name", V: "myapp"}})
|
|
assert.Equal(t, "myapp", c.Cli().Banner())
|
|
}
|
|
|
|
func TestCli_SetBanner_Good(t *testing.T) {
|
|
c := New()
|
|
c.Cli().SetBanner(func(_ *Cli) string { return "Custom Banner" })
|
|
assert.Equal(t, "Custom Banner", c.Cli().Banner())
|
|
}
|
|
|
|
func TestCli_Run_Good(t *testing.T) {
|
|
c := New()
|
|
executed := false
|
|
c.Command("hello", func(_ Options) Result[any] {
|
|
executed = true
|
|
return Result[any]{Value: "world", OK: true}
|
|
})
|
|
r := c.Cli().Run("hello")
|
|
assert.True(t, r.OK)
|
|
assert.Equal(t, "world", r.Value)
|
|
assert.True(t, executed)
|
|
}
|
|
|
|
func TestCli_Run_Nested_Good(t *testing.T) {
|
|
c := New()
|
|
executed := false
|
|
c.Command("deploy/to/homelab", func(_ Options) Result[any] {
|
|
executed = true
|
|
return Result[any]{OK: true}
|
|
})
|
|
r := c.Cli().Run("deploy", "to", "homelab")
|
|
assert.True(t, r.OK)
|
|
assert.True(t, executed)
|
|
}
|
|
|
|
func TestCli_Run_WithFlags_Good(t *testing.T) {
|
|
c := New()
|
|
var received Options
|
|
c.Command("serve", func(opts Options) Result[any] {
|
|
received = opts
|
|
return Result[any]{OK: true}
|
|
})
|
|
c.Cli().Run("serve", "--port=8080", "--debug")
|
|
assert.Equal(t, "8080", received.String("port"))
|
|
assert.True(t, received.Bool("debug"))
|
|
}
|
|
|
|
func TestCli_Run_NoCommand_Good(t *testing.T) {
|
|
c := New()
|
|
// No commands registered — should not panic
|
|
r := c.Cli().Run()
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestCli_PrintHelp_Good(t *testing.T) {
|
|
c := New(Options{{K: "name", V: "myapp"}})
|
|
c.Command("deploy", func(_ Options) Result[any] { return Result[any]{OK: true} })
|
|
c.Command("serve", func(_ Options) Result[any] { return Result[any]{OK: true} })
|
|
// Should not panic
|
|
c.Cli().PrintHelp()
|
|
}
|