exec

package module
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 14 Imported by: 2

README

Command Execution Build Status GoDoc License

The package supplies the common command execution supporting Go1.7+.

Install

$ go get -u github.com/xgfone/go-exec

Example

package main

import (
	"context"
	"fmt"

	"github.com/xgfone/go-exec"
)

const scripttmpl = `
ls %s
rm -rf %s
`

func main() {
	exec.Execute(context.Background(), "mkdir", "testdir")
	exec.ExecuteShellCmd(context.Background(), "echo abc > %s/%s", "testdir", "testfile")

	data, _ := exec.OutputShellCmd(context.Background(), "cat %s/%s", "testdir", "testfile")
	fmt.Println(data)

	_, _, err := exec.RunShellScript(context.Background(), scripttmpl, "testdir", "testdir")
	fmt.Println(err)

	// Output:
	// abc
	//
	// <nil>
}

Documentation

Overview

Package exec executes a command in a new process.

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultCmd = Cmd{Lock: new(sync.Mutex), ResultHook: logResult}

DefaultCmd is the global default cmd executor.

View Source
var DefaultShell = "bash"

DefaultShell is the default shell to execute the shell command or script.

View Source
var DefaultTimeout = time.Second * 3

DefaultTimeout is the global default timeout.

View Source
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

func CombinedOutputCmdHook(cmd *exec.Cmd) (stdout, stderr string, err error)

CombinedOutputCmdHook is a CmdHook to run the command and returns its combined standard output and standard error, like exec.Cmd.CombinedOutput.

func Execute

func Execute(cxt context.Context, name string, args ...string) error

Execute is equal to DefaultCmd.Execute(cxt, name, args...).

func ExecuteShellCmd added in v0.2.0

func ExecuteShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) error

ExecuteShellCmd is equal to DefaultCmd.ExecuteShellCmd(ctx, cmdfmt, cmdargs...).

func ExecuteShellScript added in v0.2.0

func ExecuteShellScript(ctx context.Context, script string, args ...string) error

ExecuteShellScript is equal to DefaultCmd.ExecuteShellScript.

func GetScriptFile added in v0.4.0

func GetScriptFile(dir, scriptContent string) (filename string, err error)

GetScriptFile generates a unique script filename and writes the script into it.

If dir is empty, use ShellScriptDir instead.

func Output

func Output(cxt context.Context, name string, args ...string) (string, error)

Output is equal to DefaultCmd.Output(cxt, name, args...).

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

func Run

func Run(ctx context.Context, name string, args ...string) (stdout, stderr string, err error)

Run is equal to DefaultCmd.Run(ctx, name, args...).

func RunShellCmd

func RunShellCmd(ctx context.Context, cmdfmt string, cmdargs ...string) (
	stdout, stderr string, err error)

RunShellCmd is equal to DefaultCmd.RunShellCmd(ctx, cmdfmt, cmdargs...).

func RunShellScript

func RunShellScript(ctx context.Context, script string, args ...string) (
	stdout, stderr string, err error)

RunShellScript is equal to DefaultCmd.RunShellScript(ctx, script, args...).

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

func WithCmdHook(hook CmdHook) Cmd

WithCmdHook is equal to DefaultCmd.WithCmdHook(hook).

func WithLock added in v0.2.0

func WithLock(lock *sync.Mutex) Cmd

WithLock is equal to DefaultCmd.WithLock(lock).

func WithResultHook added in v0.2.0

func WithResultHook(hook func(Result)) Cmd

WithResultHook is equal to DefaultCmd.WithResultHook(hook).

func WithShell added in v0.2.0

func WithShell(shell string) Cmd

WithShell is equal to DefaultCmd.WithShell(shell).

func WithTimeout added in v0.2.0

func WithTimeout(timeout time.Duration) Cmd

WithTimeout is equal to DefaultCmd.WithTimeout(timeout).

func (Cmd) Execute

func (c Cmd) Execute(cxt context.Context, name string, args ...string) error

Execute is the same as RunCmd, but only returns the error.

func (Cmd) ExecuteShellCmd added in v0.2.0

func (c Cmd) ExecuteShellCmd(cxt context.Context, cmdfmt string, cmdargs ...string) error

ExecuteShellCmd is the same as RunShellCmd, but only returns the error.

func (Cmd) ExecuteShellScript added in v0.2.0

func (c Cmd) ExecuteShellScript(ctx context.Context, script string, args ...string) error

ExecuteShellScript is the same as RunShellScript, but only returns the error.

func (Cmd) Output

func (c Cmd) Output(cxt context.Context, name string, args ...string) (string, error)

Output is the same as RunCmd, but only returns the stdout and the error.

func (Cmd) OutputShellCmd added in v0.2.0

func (c Cmd) OutputShellCmd(cxt context.Context, cmdfmt string, cmdargs ...string) (string, error)

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

func (c Cmd) WithCmdHook(hook CmdHook) Cmd

WithCmdHook returns a new Cmd with the cmd hook.

func (Cmd) WithLock added in v0.2.0

func (c Cmd) WithLock(lock *sync.Mutex) Cmd

WithLock returns a new Cmd with the lock.

func (Cmd) WithResultHook added in v0.2.0

func (c Cmd) WithResultHook(hook func(Result)) Cmd

WithResultHook returns a new Cmd with the result hook.

func (Cmd) WithShell added in v0.2.0

func (c Cmd) WithShell(shell string) Cmd

WithShell returns a new Cmd with the shell.

func (Cmd) WithTimeout added in v0.2.0

func (c Cmd) WithTimeout(timeout time.Duration) Cmd

WithTimeout returns a new Cmd with the timeout.

type CmdHook added in v0.2.0

type CmdHook func(cmd *exec.Cmd) (stdout, stderr string, err error)

CmdHook is used to customize how to run the command.

func StdoutAndStderrBufferCmdHook added in v0.2.0

func StdoutAndStderrBufferCmdHook(stdout, stderr *bytes.Buffer) CmdHook

StdoutAndStderrBufferCmdHook returns a CmdHook that uses the given stdout and stderr buffer as the stdout and stderr of exec.Cmd.

type Result added in v0.2.0

type Result struct {
	Name string
	Args []string

	Stdout string
	Stderr string
	Err    error
}

Result represents the result of the executed command, which has implemented the interface error, so may be used as an error.

func NewResult added in v0.2.0

func NewResult(name string, args []string, stdout, stderr string, err error) Result

NewResult returns a new Result.

func (Result) Error added in v0.2.0

func (r Result) Error() string

Error implements the interface error.

func (Result) Unwrap added in v0.2.0

func (r Result) Unwrap() error

Unwrap implements errors.Unwrap.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL