Documentation ¶
Overview ¶
Package exec is a wrapper around os/exec that provides a few extra features.
Package exec is a wrapper around os/exec that provides a few extra features.
Index ¶
- Variables
- func LookPath(file string) (string, error)
- type Cmd
- type Option
- func AppendArgs(args ...string) Option
- func Pipe(name string, args ...string) Option
- func RedactArgs(args ...string) Option
- func WithArgs(args ...string) Option
- func WithArgsRedactor(r redact.Redactor) Option
- func WithEnv(key, value string) Option
- func WithEnvs(envs map[string]string) Option
- func WithLogger(logger ctxd.Logger) Option
- func WithStderr(err io.Writer) Option
- func WithStdin(in io.Reader) Option
- func WithStdout(out io.Writer) Option
- func WithTracer(tracer trace.Tracer) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = exec.ErrNotFound
ErrNotFound is the error resulting if a path search failed to find an executable file.
Functions ¶
Types ¶
type Cmd ¶
Cmd is a wrapper around exec.Cmd.
func Command ¶
Command returns the Cmd struct to execute the named program with the given arguments.
See os/exec.Command for more information.
func CommandContext ¶
CommandContext is like Command but includes a context.
See os/exec.CommandContext for more information.
func Run ¶
Run runs the command.
See os/exec.Command.Run for more information.
Example ¶
package main import ( "bytes" "fmt" "go.nhat.io/exec" ) func main() { stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) _, err := exec.Run("echo", exec.WithArgs("hello world"), exec.WithStdout(stdout), exec.WithStderr(stderr), ) if err != nil { panic(err) } fmt.Println(stdout.String()) fmt.Println(stderr.String()) }
Output: hello world
Example (Pipe) ¶
package main import ( "bytes" "fmt" "go.nhat.io/exec" ) func main() { stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) _, err := exec.Run("echo", exec.WithArgs("hello world"), exec.WithStdout(stdout), exec.WithStderr(stderr), exec.Pipe("grep", "-o", "hello"), exec.Pipe("tr", "[:lower:]", "[:upper:]"), ) if err != nil { panic(err) } fmt.Println(stdout.String()) fmt.Println(stderr.String()) }
Output: HELLO
Example (Stdin) ¶
package main import ( "bytes" "fmt" "strings" "go.nhat.io/exec" ) func main() { stdin := strings.NewReader("hello world") stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) _, err := exec.Run("cat", exec.WithStdin(stdin), exec.WithStdout(stdout), exec.WithStderr(stderr), ) if err != nil { panic(err) } fmt.Println(stdout.String()) fmt.Println(stderr.String()) }
Output: hello world
func RunWithContext ¶
RunWithContext runs the command with the given context.
See os/exec.Command.Run for more information.
func (*Cmd) Run ¶
Run starts the specified command and waits for it to complete.
The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.
If the command starts but does not complete successfully, the error is of type *ExitError. Other error types may be returned for other situations.
If the calling goroutine has locked the operating system thread with runtime.LockOSThread and modified any inheritable OS-level thread state (for example, Linux or Plan 9 name spaces), the new process will inherit the caller's thread state.
func (*Cmd) Start ¶
Start starts the specified command but does not wait for it to complete.
If Start returns successfully, the c.Process field will be set.
After a successful call to Start the Wait method must be called in order to release associated system resources.
func (*Cmd) String ¶
String returns a human-readable description of c. It is intended only for debugging. In particular, it is not suitable for use as input to a shell.
The output of String may vary across Go releases.
func (*Cmd) Wait ¶
Wait waits for the command to exit and waits for any copying to stdin or copying from stdout or stderr to complete.
The command must have been started by Start.
The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.
If the command fails to run or doesn't complete successfully, the error is of type *ExitError. Other error types may be returned for I/O problems.
If any of c.Stdin, c.Stdout or c.Stderr are not an *os.File, Wait also waits for the respective I/O loop copying to or from the process to complete.
Wait releases any resources associated with the Cmd.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is an option to configure the Cmd.
func AppendArgs ¶ added in v0.3.0
AppendArgs appends the arguments to the existing ones.
func RedactArgs ¶ added in v0.5.0
RedactArgs redacts the given arguments in traces and logs.
func WithArgsRedactor ¶ added in v0.6.0
WithArgsRedactor sets the redactor to redact the arguments.