Skip to content

Commit

Permalink
Removing the out io.Writer param from Executor
Browse files Browse the repository at this point in the history
It's not necessary for the lib/framework, and can just be tracked as an
application dependency by users.
  • Loading branch information
Rican7 committed Aug 11, 2023
1 parent b0dabcc commit 33a1a40
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 34 deletions.
5 changes: 2 additions & 3 deletions example_minimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions example_multicommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"flag"
"fmt"
"io"
"os"
"time"

Expand Down Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions example_singlecommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"flag"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -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
}
7 changes: 3 additions & 4 deletions lieut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
26 changes: 7 additions & 19 deletions lieut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
},

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
},

Expand Down

0 comments on commit 33a1a40

Please sign in to comment.