From 17f24f6abd3a40d6040b14bcf31dbb743e983993 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 4 Dec 2023 02:30:14 +0000 Subject: [PATCH] feat: support to return the process --- command.go | 24 +++++++++++++++++++----- command_test.go | 2 +- fake-command.go | 2 +- fake-command_test.go | 3 ++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/command.go b/command.go index 488c338..8b886d2 100644 --- a/command.go +++ b/command.go @@ -45,7 +45,7 @@ type Execer interface { RunCommandAndReturn(name, dir string, args ...string) (result string, err error) RunCommandWithSudo(name string, args ...string) (err error) RunCommandWithBuffer(name, dir string, stdout, stderr *bytes.Buffer, args ...string) error - RunCommandWithIO(name, dir string, stdout, stderr io.Writer, args ...string) (err error) + RunCommandWithIO(name, dir string, stdout, stderr io.Writer, p chan Process, args ...string) error SystemCall(name string, argv []string, envv []string) (err error) MkdirAll(path string, perm os.FileMode) error OS() string @@ -53,6 +53,13 @@ type Execer interface { WithContext(context.Context) Execer } +type Process interface { + Signal(sig os.Signal) error + Kill() error + Release() error + Wait() (*os.ProcessState, error) +} + const ( // OSLinux is the alias of Linux OSLinux = "linux" @@ -94,7 +101,8 @@ func (e *defaultExecer) Command(name string, arg ...string) ([]byte, error) { // RunCommand runs a command func (e *defaultExecer) RunCommand(name string, arg ...string) error { - return e.RunCommandWithIO(name, "", os.Stdout, os.Stderr, arg...) + err := e.RunCommandWithIO(name, "", os.Stdout, os.Stderr, nil, arg...) + return err } // RunCommandWithEnv runs a command with given Env @@ -127,7 +135,7 @@ func (e *defaultExecer) RunCommandWithEnv(name string, argv, envv []string, stdo } // RunCommandWithIO runs a command with given IO -func (e *defaultExecer) RunCommandWithIO(name, dir string, stdout, stderr io.Writer, args ...string) (err error) { +func (e *defaultExecer) RunCommandWithIO(name, dir string, stdout, stderr io.Writer, p chan Process, args ...string) (err error) { command := exec.CommandContext(e.ctx, name, args...) if dir != "" { command.Dir = dir @@ -138,6 +146,10 @@ func (e *defaultExecer) RunCommandWithIO(name, dir string, stdout, stderr io.Wri stderrIn, _ := command.StderrPipe() err = command.Start() if err == nil { + if p != nil { + p <- command.Process + } + // cmd.Wait() should be called only after we finish reading // from stdoutIn and stderrIn. // wg ensures that we finish @@ -194,12 +206,14 @@ func (e *defaultExecer) RunCommandWithBuffer(name, dir string, stdout, stderr *b if stderr == nil { stderr = &bytes.Buffer{} } - return e.RunCommandWithIO(name, dir, stdout, stderr, args...) + err := e.RunCommandWithIO(name, dir, stdout, stderr, nil, args...) + return err } // RunCommandInDir runs a command func (e *defaultExecer) RunCommandInDir(name, dir string, args ...string) error { - return e.RunCommandWithIO(name, dir, os.Stdout, os.Stderr, args...) + err := e.RunCommandWithIO(name, dir, os.Stdout, os.Stderr, nil, args...) + return err } // RunCommandWithSudo runs a command with sudo diff --git a/command_test.go b/command_test.go index 84ec4be..19e31ae 100644 --- a/command_test.go +++ b/command_test.go @@ -105,7 +105,7 @@ func TestDefaultExecer(t *testing.T) { err = ex.RunCommandWithEnv(tt.cmd, tt.args, nil, os.Stdout, os.Stderr) assert.Equal(t, tt.expectErr, err != nil, err) - err = ex.RunCommandWithIO(tt.cmd, os.TempDir(), os.Stdout, os.Stderr, tt.args...) + err = ex.RunCommandWithIO(tt.cmd, os.TempDir(), os.Stdout, os.Stderr, nil, tt.args...) assert.Equal(t, tt.expectErr, err != nil, err) err = ex.RunCommandInDir(tt.cmd, "", tt.args...) diff --git a/fake-command.go b/fake-command.go index df8aaab..66b1d2d 100644 --- a/fake-command.go +++ b/fake-command.go @@ -88,7 +88,7 @@ func (f FakeExecer) RunCommandWithBuffer(name, dir string, stdout, stderr *bytes } // RunCommandWithIO is a fake method -func (f FakeExecer) RunCommandWithIO(name, dir string, stdout, stderr io.Writer, args ...string) error { +func (f FakeExecer) RunCommandWithIO(name, dir string, stdout, stderr io.Writer, p chan Process, args ...string) error { return f.ExpectError } diff --git a/fake-command_test.go b/fake-command_test.go index 8eb67aa..3bb80ac 100644 --- a/fake-command_test.go +++ b/fake-command_test.go @@ -55,7 +55,8 @@ func TestLookPath(t *testing.T) { assert.Equal(t, "os", fake.OS()) assert.Equal(t, "arch", fake.Arch()) assert.Nil(t, fake.RunCommand("", "")) - assert.Nil(t, fake.RunCommandWithIO("", "", nil, nil)) + err = fake.RunCommandWithIO("", "", nil, nil, nil) + assert.Nil(t, err) assert.Nil(t, fake.RunCommandWithEnv("", nil, nil, nil, nil)) assert.Nil(t, fake.RunCommandInDir("", ""))