diff --git a/example_minimal_test.go b/example_minimal_test.go index 33745bf..05e41a5 100644 --- a/example_minimal_test.go +++ b/example_minimal_test.go @@ -6,15 +6,14 @@ import ( "context" "flag" "fmt" - "io" "os" "github.com/Rican7/lieut" ) func Example_minimal() { - do := func(ctx context.Context, arguments []string, out io.Writer) error { - _, err := fmt.Fprintln(out, arguments) + do := func(ctx context.Context, arguments []string) error { + _, err := fmt.Println(arguments) return err } diff --git a/example_multicommand_test.go b/example_multicommand_test.go index 7348199..58a9568 100644 --- a/example_multicommand_test.go +++ b/example_multicommand_test.go @@ -6,7 +6,6 @@ import ( "context" "flag" "fmt" - "io" "os" "time" @@ -63,26 +62,26 @@ func validateGlobals() error { return err } -func printTime(ctx context.Context, arguments []string, out io.Writer) error { +func printTime(ctx context.Context, arguments []string) error { format := "15:04" if includeSeconds { format += ":05" } - _, err := fmt.Fprintln(out, time.Now().Format(format)) + _, err := fmt.Println(time.Now().Format(format)) return err } -func printDate(ctx context.Context, arguments []string, out io.Writer) error { +func printDate(ctx context.Context, arguments []string) error { format := "01-02" if includeYear { format += "-2006" } - _, err := fmt.Fprintln(out, time.Now().Format(format)) + _, err := fmt.Println(time.Now().Format(format)) return err } diff --git a/example_singlecommand_test.go b/example_singlecommand_test.go index b25128b..2a1fd28 100644 --- a/example_singlecommand_test.go +++ b/example_singlecommand_test.go @@ -6,7 +6,6 @@ import ( "context" "flag" "fmt" - "io" "os" "strings" @@ -36,11 +35,11 @@ func Example_singleCommand() { os.Exit(exitCode) } -func sayHello(ctx context.Context, arguments []string, out io.Writer) error { +func sayHello(ctx context.Context, arguments []string) error { names := strings.Join(arguments, ", ") hello := fmt.Sprintf("Hello %s!", names) - _, err := fmt.Fprintln(out, hello) + _, err := fmt.Println(hello) return err } diff --git a/lieut.go b/lieut.go index ee646eb..ad3b7ea 100644 --- a/lieut.go +++ b/lieut.go @@ -31,9 +31,8 @@ const ( // Executor is a functional interface that defines an executable command. // -// It takes a context, arguments, an output writer, and returns an error (if any -// occurred). -type Executor func(ctx context.Context, arguments []string, out io.Writer) error +// It takes a context and arguments, and returns an error (if any occurred). +type Executor func(ctx context.Context, arguments []string) error // CommandInfo describes information about a command. type CommandInfo struct { @@ -357,7 +356,7 @@ func (a *app) execute(ctx context.Context, exec Executor, arguments []string) in ctx, stop := signal.NotifyContext(ctx, os.Interrupt) defer stop() - err := exec(ctx, arguments, a.out) + err := exec(ctx, arguments) if err != nil && !errors.Is(err, context.Canceled) { return a.printErr(err, true) } diff --git a/lieut_test.go b/lieut_test.go index f8fbff8..6d229e2 100644 --- a/lieut_test.go +++ b/lieut_test.go @@ -20,7 +20,7 @@ var testAppInfo = AppInfo{ Version: "vTest", } -var testNoOpExecutor = func(ctx context.Context, arguments []string, out io.Writer) error { +var testNoOpExecutor = func(ctx context.Context, arguments []string) error { return nil } @@ -568,13 +568,11 @@ func TestSingleCommandApp_Run(t *testing.T) { var executorCapture struct { ctx context.Context arguments []string - out io.Writer } - executor := func(ctx context.Context, arguments []string, out io.Writer) error { + executor := func(ctx context.Context, arguments []string) error { executorCapture.ctx = ctx executorCapture.arguments = arguments - executorCapture.out = out return nil } @@ -615,16 +613,12 @@ func TestSingleCommandApp_Run(t *testing.T) { if executorCapture.arguments[0] != args[0] && executorCapture.arguments[1] != args[1] { t.Errorf("app.Run executor gave %q, wanted %q", executorCapture.arguments, args) } - - if executorCapture.out != out { - t.Errorf("app.Run executor gave %q, wanted %q", executorCapture.out, out) - } } func TestSingleCommandApp_Run_EmptyArgsProvided(t *testing.T) { var capturedArgs []string - executor := func(ctx context.Context, arguments []string, out io.Writer) error { + executor := func(ctx context.Context, arguments []string) error { capturedArgs = arguments return nil } @@ -723,7 +717,7 @@ test vTest (%s/%s) wantedErrOut: "Error: test init error\n", }, "execute returns error": { - exec: func(ctx context.Context, arguments []string, out io.Writer) error { + exec: func(ctx context.Context, arguments []string) error { return errors.New("test exec error") }, @@ -771,12 +765,10 @@ func TestMultiCommandApp_Run(t *testing.T) { var executorCapture struct { ctx context.Context arguments []string - out io.Writer } - executor := func(ctx context.Context, arguments []string, out io.Writer) error { + executor := func(ctx context.Context, arguments []string) error { executorCapture.ctx = ctx executorCapture.arguments = arguments - executorCapture.out = out return nil } @@ -815,10 +807,6 @@ func TestMultiCommandApp_Run(t *testing.T) { if executorCapture.arguments[0] != args[1] && executorCapture.arguments[1] != args[2] { t.Errorf("app.Run executor gave %q, wanted %q", executorCapture.arguments, args) } - - if executorCapture.out != out { - t.Errorf("app.Run executor gave %q, wanted %q", executorCapture.out, out) - } } func TestMultiCommandApp_Run_EmptyArgsProvided(t *testing.T) { @@ -830,7 +818,7 @@ func TestMultiCommandApp_Run_EmptyArgsProvided(t *testing.T) { testCommandInfo := CommandInfo{Name: "testcommand"} var capturedArgs []string - executor := func(ctx context.Context, arguments []string, out io.Writer) error { + executor := func(ctx context.Context, arguments []string) error { capturedArgs = arguments return nil } @@ -985,7 +973,7 @@ test vTest (%s/%s) wantedErrOut: "Error: test init error\n", }, "execute returns error": { - exec: func(ctx context.Context, arguments []string, out io.Writer) error { + exec: func(ctx context.Context, arguments []string) error { return errors.New("test exec error") },