Skip to content

Commit

Permalink
Merge pull request #8 from LinuxSuRen/feat/process
Browse files Browse the repository at this point in the history
feat: support to return the process
  • Loading branch information
LinuxSuRen authored Dec 4, 2023
2 parents f1e4ace + 17f24f6 commit a36148d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
24 changes: 19 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ 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
Arch() string
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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion fake-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion fake-command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("", ""))

Expand Down

0 comments on commit a36148d

Please sign in to comment.