Skip to content

Commit

Permalink
⚙️ refactor: Change the Options parameter to the pointer type
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Aug 23, 2024
1 parent 46d12f2 commit 906d21e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
16 changes: 8 additions & 8 deletions zshell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type pipeWork struct {
w *io.PipeWriter
}

func PipeExecCommand(ctx context.Context, commands [][]string, opt ...func(o Options) Options) (code int, outStr, errStr string, err error) {
func PipeExecCommand(ctx context.Context, commands [][]string, opt ...func(o *Options)) (code int, outStr, errStr string, err error) {
var (
cmds []*pipeWork
out bytes.Buffer
Expand Down Expand Up @@ -173,7 +173,7 @@ func PipeExecCommand(ctx context.Context, commands [][]string, opt ...func(o Opt
return status, out.String(), "", nil
}

func ExecCommand(ctx context.Context, command []string, stdIn io.Reader, stdOut io.Writer, stdErr io.Writer, opt ...func(o Options) Options) (code int, outStr, errStr string, err error) {
func ExecCommand(ctx context.Context, command []string, stdIn io.Reader, stdOut io.Writer, stdErr io.Writer, opt ...func(o *Options)) (code int, outStr, errStr string, err error) {
stdout := newShellStdBuffer(stdOut)
stderr := newShellStdBuffer(stdErr)
code, err = ExecCommandHandle(ctx, command, func(cmd *exec.Cmd) error {
Expand All @@ -188,23 +188,23 @@ func ExecCommand(ctx context.Context, command []string, stdIn io.Reader, stdOut
return
}

func Run(command string, opt ...func(o Options) Options) (code int, outStr, errStr string, err error) {
func Run(command string, opt ...func(o *Options)) (code int, outStr, errStr string, err error) {
return RunContext(context.Background(), command, opt...)
}

func RunContext(ctx context.Context, command string, opt ...func(o Options) Options) (code int, outStr, errStr string, err error) {
func RunContext(ctx context.Context, command string, opt ...func(o *Options)) (code int, outStr, errStr string, err error) {
return ExecCommand(ctx, fixCommand(command), nil, nil, nil, opt...)
}

func OutRun(command string, stdIn io.Reader, stdOut io.Writer, stdErr io.Writer, opt ...func(o Options) Options) (code int, outStr, errStr string, err error) {
return ExecCommand(context.Background(), fixCommand(command), stdIn, stdOut, stdErr)
}

func BgRun(command string, opt ...func(o Options) Options) (err error) {
func BgRun(command string, opt ...func(o *Options)) (err error) {
return BgRunContext(context.Background(), command, opt...)
}

func BgRunContext(ctx context.Context, command string, opt ...func(o Options) Options) (err error) {
func BgRunContext(ctx context.Context, command string, opt ...func(o *Options)) (err error) {
if strings.TrimSpace(command) == "" {
return errors.New("no such command")
}
Expand All @@ -224,7 +224,7 @@ func BgRunContext(ctx context.Context, command string, opt ...func(o Options) Op
return err
}

func CallbackRun(command string, callback func(out string, isBasic bool), opt ...func(o Options) Options) (<-chan int, func(string), error) {
func CallbackRun(command string, callback func(out string, isBasic bool), opt ...func(o *Options)) (<-chan int, func(string), error) {
return CallbackRunContext(context.Background(), command, callback, opt...)
}

Expand All @@ -233,7 +233,7 @@ type Options struct {
Env []string
}

func CallbackRunContext(ctx context.Context, command string, callback func(str string, isStdout bool), opt ...func(o Options) Options) (<-chan int, func(string), error) {
func CallbackRunContext(ctx context.Context, command string, callback func(str string, isStdout bool), opt ...func(o *Options)) (<-chan int, func(string), error) {
var (
cmd *exec.Cmd
err error
Expand Down
9 changes: 3 additions & 6 deletions zshell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ func TestPipe(t *testing.T) {
{"grep", "shell_notwin"},
}

code, outStr, errStr, err := PipeExecCommand(ctx, commands, func(o Options) Options {
code, outStr, errStr, err := PipeExecCommand(ctx, commands, func(o *Options) {
o.Dir = "."
return o
})

t.Log(outStr, errStr, err)
Expand Down Expand Up @@ -64,9 +63,8 @@ func TestBash(t *testing.T) {
t.Log(err)

if !zutil.IsWin() {
code, _, _, err = Run("ls", func(o Options) Options {
code, _, _, err = Run("ls", func(o *Options) {
o.Dir = "."
return o
})
tt.EqualExit(0, code)
tt.EqualExit(true, err == nil)
Expand Down Expand Up @@ -103,8 +101,7 @@ func TestCallbackRun(t *testing.T) {
if i > 3 {
cancel()
}
}, func(o Options) Options {
return o
}, func(o *Options) {
})
tt.NoError(err)
tt.Log("code", <-code)
Expand Down
4 changes: 2 additions & 2 deletions zshell/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package zshell

import "os/exec"

func wrapOptions(cmd *exec.Cmd, opt ...func(o Options) Options) {
func wrapOptions(cmd *exec.Cmd, opt ...func(o *Options)) {
o := Options{}
for _, v := range opt {
o = v(o)
v(&o)
}

if o.Dir != "" {
Expand Down

0 comments on commit 906d21e

Please sign in to comment.