fix(summer): pass delegated command flags to the app binary
- delegateCommand builds a Passthrough bonfire command, so `summer serve --addr :8123`, `summer serve --help` and `-h` reach the app binary instead of failing with "unknown flag: --addr" - one leading "--" is dropped so `summer serve -- --addr :8123` still works - appRunner is a package-level seam that tests replace to capture argv
This commit is contained in:
@@ -13,14 +13,23 @@ import (
|
|||||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// appRunner runs the app binary with argv; tests replace it.
|
||||||
|
var appRunner = runAppBinary
|
||||||
|
|
||||||
func delegateCommand(name, description string) bonfire.Command {
|
func delegateCommand(name, description string) bonfire.Command {
|
||||||
return bonfire.Command{
|
return bonfire.Command{
|
||||||
Name: name,
|
Name: name,
|
||||||
Description: description,
|
Description: description,
|
||||||
|
Passthrough: true,
|
||||||
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
||||||
argv := []string{name}
|
args := in.Args()
|
||||||
argv = append(argv, in.Args()...)
|
// One leading "--" is dropped so the older
|
||||||
return runAppBinary(ctx, argv, out)
|
// `summer serve -- --addr :8123` form keeps working.
|
||||||
|
if len(args) > 0 && args[0] == "--" {
|
||||||
|
args = args[1:]
|
||||||
|
}
|
||||||
|
argv := append([]string{name}, args...)
|
||||||
|
return appRunner(ctx, argv, out)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
44
cmd/summer/runtime_test.go
Normal file
44
cmd/summer/runtime_test.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||||
|
)
|
||||||
|
|
||||||
|
// captureAppRunner replaces appRunner for the test and returns a pointer to
|
||||||
|
// the argv of the last call.
|
||||||
|
func captureAppRunner(t *testing.T) *[]string {
|
||||||
|
t.Helper()
|
||||||
|
got := new([]string)
|
||||||
|
prev := appRunner
|
||||||
|
appRunner = func(ctx context.Context, argv []string, out bonfire.Output) error {
|
||||||
|
*got = append([]string(nil), argv...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { appRunner = prev })
|
||||||
|
return got
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDelegatedServePassesFlags(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
args []string
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{[]string{"serve", "--addr", ":8123"}, []string{"serve", "--addr", ":8123"}},
|
||||||
|
{[]string{"serve", "--help"}, []string{"serve", "--help"}},
|
||||||
|
{[]string{"serve", "-h"}, []string{"serve", "-h"}},
|
||||||
|
{[]string{"serve", "--addr=:8123"}, []string{"serve", "--addr=:8123"}},
|
||||||
|
{[]string{"serve", "--", "--addr", ":8123"}, []string{"serve", "--addr", ":8123"}},
|
||||||
|
} {
|
||||||
|
got := captureAppRunner(t)
|
||||||
|
if out, err := runSummer(t, tc.args...); err != nil {
|
||||||
|
t.Fatalf("%q: %v\n%s", tc.args, err, out)
|
||||||
|
}
|
||||||
|
if !slices.Equal(*got, tc.want) {
|
||||||
|
t.Fatalf("%q: argv = %q, want %q", tc.args, *got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user