Skip to content

Commit

Permalink
feat: add support for options in cmd (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-kapstan committed Nov 1, 2023
1 parent 767d88b commit f06a752
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func (r *Runner) SetupRoot(cmd *cobra.Command) *Runner {
}

// Setup sets up the Command
func (r *Runner) Setup(cmd *cobra.Command, module fx.Option) *Runner {
func (r *Runner) Setup(cmd *cobra.Command, options ...fx.Option) *Runner {
if cmd.RunE == nil {
cmd.RunE = r.runE(module)
cmd.RunE = r.runE(options...)
}

cmd.AddCommand(&cobra.Command{
Expand All @@ -95,7 +95,7 @@ func (r *Runner) Setup(cmd *cobra.Command, module fx.Option) *Runner {
fx.Supply(service.Name(r.prefix)),
fx.Supply(logging.FatalLevel),
orlop.Module,
module,
fx.Options(options...),
fx.Populate(&cfgMgr),
)

Expand Down Expand Up @@ -154,7 +154,7 @@ func (r *Runner) preRunE(cmd *cobra.Command, args []string) error {
return r.prevPreRunE(cmd, args)
}

func (r *Runner) runE(module fx.Option) func(cmd *cobra.Command, args []string) error {
func (r *Runner) runE(options ...fx.Option) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
l := log.New()

Expand All @@ -170,7 +170,7 @@ func (r *Runner) runE(module fx.Option) func(cmd *cobra.Command, args []string)
fx.Supply(service.Name(r.prefix)),
fx.Supply(logging.Level(loglevelFlag)),
orlop.Module,
module,
fx.Options(options...),
)

app.Run()
Expand Down
53 changes: 53 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"os"
"os/exec"
"testing"
"time"

Expand Down Expand Up @@ -148,3 +149,55 @@ func TestInit(t *testing.T) {
err := cmd.Execute()
require.NoError(t, err)
}

func TestOptions(t *testing.T) {
module := fx.Options(
fx.Invoke(
func(lifecycle fx.Lifecycle, s fx.Shutdowner) {
lifecycle.Append(
fx.Hook{
OnStart: func(_ context.Context) error {
return s.Shutdown()
},
OnStop: func(ctx context.Context) error {
time.Sleep(fx.DefaultTimeout + time.Second)
return nil
},
},
)
},
),
)

if os.Getenv("CRASH") == "0" {
testOptions(module, fx.StopTimeout(2*fx.DefaultTimeout))
return
} else if os.Getenv("CRASH") == "1" {
testOptions(module)
return
}

crashCmd := exec.Command(os.Args[0], "-test.run=TestOptions")
crashCmd.Env = append(os.Environ(), "CRASH=1")
err := crashCmd.Run()
assert.Error(t, err, "process did not return an error", err)
assert.IsType(t, &exec.ExitError{}, err, "process did not return an ExitError", err)

successCmd := exec.Command(os.Args[0], "-test.run=TestOptions")
successCmd.Env = append(os.Environ(), "CRASH=0")
err = successCmd.Run()
assert.NoError(t, err, "process returned error %v, want exit status 0", err)
}

func testOptions(module fx.Option, options ...fx.Option) error {
options = append(options, module)

var cmd = &cobra.Command{
Use: "test",
TraverseChildren: true,
}

NewRunner("test").SetupRoot(cmd).Setup(cmd, options...)

return cmd.Execute()
}

0 comments on commit f06a752

Please sign in to comment.