Documentation ¶
Overview ¶
Package exec executes a command in a new process.
Index ¶
- Variables
- func CombinedOutputCmdHook(cmd *exec.Cmd) (stdout, stderr string, err error)
- func Execute(cxt context.Context, name string, args ...string) error
- func ExecuteShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) error
- func ExecuteShellScript(ctx context.Context, script string, args ...string) error
- func GetScriptFile(dir, scriptContent string) (filename string, err error)
- func Output(cxt context.Context, name string, args ...string) (string, error)
- func OutputShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) (stdout string, err error)
- func OutputShellScript(ctx context.Context, script string, args ...string) (stdout string, err error)
- func Run(ctx context.Context, name string, args ...string) (stdout, stderr string, err error)
- func RunShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) (stdout, stderr string, err error)
- func RunShellScript(ctx context.Context, script string, args ...string) (stdout, stderr string, err error)
- type Cmd
- func (c Cmd) Execute(cxt context.Context, name string, args ...string) error
- func (c Cmd) ExecuteShellCmd(cxt context.Context, cmdfmt string, cmdargs ...string) error
- func (c Cmd) ExecuteShellScript(ctx context.Context, script string, args ...string) error
- func (c Cmd) Output(cxt context.Context, name string, args ...string) (string, error)
- func (c Cmd) OutputShellCmd(cxt context.Context, cmdfmt string, cmdargs ...string) (string, error)
- func (c Cmd) OutputShellScript(ctx context.Context, script string, args ...string) (stdout string, err error)
- func (c Cmd) Run(cxt context.Context, name string, args ...string) (stdout, stderr string, err error)
- func (c Cmd) RunShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) (stdout, stderr string, err error)
- func (c Cmd) RunShellScript(ctx context.Context, script string, args ...string) (stdout, stderr string, err error)
- func (c Cmd) WithCmdHook(hook CmdHook) Cmd
- func (c Cmd) WithLock(lock *sync.Mutex) Cmd
- func (c Cmd) WithResultHook(hook func(Result)) Cmd
- func (c Cmd) WithShell(shell string) Cmd
- func (c Cmd) WithTimeout(timeout time.Duration) Cmd
- type CmdHook
- type Result
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultCmd = Cmd{Lock: new(sync.Mutex), ResultHook: logResult}
DefaultCmd is the global default cmd executor.
var DefaultShell = "bash"
DefaultShell is the default shell to execute the shell command or script.
var DefaultTimeout = time.Second * 3
DefaultTimeout is the global default timeout.
var ShellScriptDir = os.TempDir()
ShellScriptDir is the directory to save the script file to be executed.
If OS is windows or js, it is reset to "". But you can set it to somewhere.
Functions ¶
func CombinedOutputCmdHook ¶ added in v0.2.0
CombinedOutputCmdHook is a CmdHook to run the command and returns its combined standard output and standard error, like exec.Cmd.CombinedOutput.
func ExecuteShellCmd ¶ added in v0.2.0
ExecuteShellCmd is equal to DefaultCmd.ExecuteShellCmd(ctx, cmdfmt, cmdargs...).
func ExecuteShellScript ¶ added in v0.2.0
ExecuteShellScript is equal to DefaultCmd.ExecuteShellScript.
func GetScriptFile ¶ added in v0.4.0
GetScriptFile generates a unique script filename and writes the script into it.
If dir is empty, use ShellScriptDir instead.
func OutputShellCmd ¶ added in v0.2.0
func OutputShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) ( stdout string, err error)
OutputShellCmd is equal to DefaultCmd.OutputShellCmd(ctx, cmdfmt, cmdargs...).
func OutputShellScript ¶ added in v0.2.0
func OutputShellScript(ctx context.Context, script string, args ...string) ( stdout string, err error)
OutputShellScript is equal to DefaultCmd.OutputShellScript
Types ¶
type Cmd ¶
type Cmd struct { // If not nil, it will be locked during the command is executed. // // Default: nil Lock *sync.Mutex // Shell is used to execute the command as the shell. // // If empty, use DefaultShell by default. Shell string // Timeout is used to produce the timeout context based on the context // argument if not 0 when executing the command. // // If empty, use DefaultTimeout by default. Timeout time.Duration // CmdHook is used to customize how to run the command. // // Default: nil CmdHook CmdHook // ResultHook is used to to observe the result of the command. // // Default: nil ResultHook func(Result) }
Cmd represents a command executor.
Example ¶
//go:build unix || aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris // +build unix aix darwin dragonfly freebsd linux netbsd openbsd solaris package main import ( "context" "fmt" ) const scripttmpl = ` ls %s rm -rf %s ` func main() { _ = Execute(context.Background(), "mkdir", "testdir") _ = ExecuteShellCmd(context.Background(), "echo abc > %s/%s", "testdir", "testfile") data, _ := OutputShellCmd(context.Background(), "cat %s/%s", "testdir", "testfile") fmt.Println(data) _, _, err := RunShellScript(context.Background(), scripttmpl, "testdir", "testdir") fmt.Println(err) }
Output: abc <nil>
func WithCmdHook ¶ added in v0.2.0
WithCmdHook is equal to DefaultCmd.WithCmdHook(hook).
func WithResultHook ¶ added in v0.2.0
WithResultHook is equal to DefaultCmd.WithResultHook(hook).
func WithTimeout ¶ added in v0.2.0
WithTimeout is equal to DefaultCmd.WithTimeout(timeout).
func (Cmd) ExecuteShellCmd ¶ added in v0.2.0
ExecuteShellCmd is the same as RunShellCmd, but only returns the error.
func (Cmd) ExecuteShellScript ¶ added in v0.2.0
ExecuteShellScript is the same as RunShellScript, but only returns the error.
func (Cmd) OutputShellCmd ¶ added in v0.2.0
OutputShellCmd is the same as RunShellCmd, but only returns stdout and error.
func (Cmd) OutputShellScript ¶ added in v0.2.0
func (c Cmd) OutputShellScript(ctx context.Context, script string, args ...string) ( stdout string, err error)
OutputShellScript is the same as RunShellScript, but only returns stdout and error.
func (Cmd) Run ¶
func (c Cmd) Run(cxt context.Context, name string, args ...string) ( stdout, stderr string, err error)
Run executes the command "name" with its arguments "args", then returns the stdout and stderr.
Notice: if err is not nil, it is Result.
func (Cmd) RunShellCmd ¶
func (c Cmd) RunShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) ( stdout, stderr string, err error)
RunShellCmd runs the command cmdargs with cmdargs as the shell command, that's,
shell -c "fmt.Sprintf(cmdfmt, cmdargs...)".
func (Cmd) RunShellScript ¶
func (c Cmd) RunShellScript(ctx context.Context, script string, args ...string) ( stdout, stderr string, err error)
RunShellScript runs the script with args as the shell script, the content of which is fmt.Sprintf(script, args...).
func (Cmd) WithCmdHook ¶ added in v0.2.0
WithCmdHook returns a new Cmd with the cmd hook.
func (Cmd) WithResultHook ¶ added in v0.2.0
WithResultHook returns a new Cmd with the result hook.
type CmdHook ¶ added in v0.2.0
CmdHook is used to customize how to run the command.
func StdoutAndStderrBufferCmdHook ¶ added in v0.2.0
StdoutAndStderrBufferCmdHook returns a CmdHook that uses the given stdout and stderr buffer as the stdout and stderr of exec.Cmd.