This commit is contained in:
Mike Schwörer 2023-01-29 21:27:55 +01:00
parent 34e6d1819d
commit 2070a432a5
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 100 additions and 58 deletions

56
cmdext/builder.go Normal file
View File

@ -0,0 +1,56 @@
package cmdext
import (
"fmt"
"time"
)
type CommandRunner struct {
program string
args []string
timeout *time.Duration
env []string
}
func Runner(program string) *CommandRunner {
return &CommandRunner{
program: program,
args: make([]string, 0),
timeout: nil,
env: make([]string, 0),
}
}
func (r *CommandRunner) Arg(arg string) *CommandRunner {
r.args = append(r.args, arg)
return r
}
func (r *CommandRunner) Args(arg []string) *CommandRunner {
r.args = append(r.args, arg...)
return r
}
func (r *CommandRunner) Timeout(timeout time.Duration) *CommandRunner {
r.timeout = &timeout
return r
}
func (r *CommandRunner) Env(key, value string) *CommandRunner {
r.env = append(r.env, fmt.Sprintf("%s=%s", key, value))
return r
}
func (r *CommandRunner) RawEnv(env string) *CommandRunner {
r.env = append(r.env, env)
return r
}
func (r *CommandRunner) Envs(env []string) *CommandRunner {
r.env = append(r.env, env...)
return r
}
func (r *CommandRunner) Run() (CommandResult, error) {
return run(*r)
}

View File

@ -14,9 +14,9 @@ type CommandResult struct {
CommandTimedOut bool CommandTimedOut bool
} }
func RunCommand(program string, args []string, timeout *time.Duration) (CommandResult, error) { func run(opt CommandRunner) (CommandResult, error) {
cmd := exec.Command(opt.program, opt.args...)
cmd := exec.Command(program, args...) cmd.Env = append(cmd.Env, opt.env)
stdoutPipe, err := cmd.StdoutPipe() stdoutPipe, err := cmd.StdoutPipe()
if err != nil { if err != nil {
@ -75,68 +75,42 @@ func RunCommand(program string, args []string, timeout *time.Duration) (CommandR
} }
}() }()
if timeout != nil { var timeoutChan <-chan time.Time = make(chan time.Time, 1)
if opt.timeout != nil {
timeoutChan = time.After(*opt.timeout)
}
select { select {
case <-time.After(*timeout): case <-timeoutChan:
_ = cmd.Process.Kill() _ = cmd.Process.Kill()
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: -1,
CommandTimedOut: true,
}, nil
case err := <-errch:
if exiterr, ok := err.(*exec.ExitError); ok {
return CommandResult{ return CommandResult{
StdOut: stdout, StdOut: stdout,
StdErr: stderr, StdErr: stderr,
StdCombined: stdcombined, StdCombined: stdcombined,
ExitCode: -1, ExitCode: exiterr.ExitCode(),
CommandTimedOut: true, CommandTimedOut: false,
}, nil
} else if err != nil {
return CommandResult{}, err
} else {
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: 0,
CommandTimedOut: false,
}, nil }, nil
case err := <-errch:
if exiterr, ok := err.(*exec.ExitError); ok {
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: exiterr.ExitCode(),
CommandTimedOut: false,
}, nil
} else if err != nil {
return CommandResult{}, err
} else {
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: 0,
CommandTimedOut: false,
}, nil
}
}
} else {
select {
case err := <-errch:
if exiterr, ok := err.(*exec.ExitError); ok {
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: exiterr.ExitCode(),
CommandTimedOut: false,
}, nil
} else if err != nil {
return CommandResult{}, err
} else {
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: 0,
CommandTimedOut: false,
}, nil
}
} }
} }
} }

12
cmdext/helper.go Normal file
View File

@ -0,0 +1,12 @@
package cmdext
import "time"
func RunCommand(program string, args []string, timeout *time.Duration) (CommandResult, error) {
b := Runner(program)
b = b.Args(args)
if timeout != nil {
b = b.Timeout(*timeout)
}
return b.Run()
}