execute

package module
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: MIT Imports: 7 Imported by: 25

README

go-execute

A wrapper for Go's command execution packages.

go get github.com/alexellis/go-execute/v2

Docs

See docs at pkg.go.dev: github.com/alexellis/go-execute

go-execute users

Used by dozens of projects as identified by GitHub, notably:

Community examples:

Feel free to add a link to your own projects in a PR.

Main options

  • DisableStdioBuffer - Discard Stdio, rather than buffering into memory
  • StreamStdio - Stream stderr and stdout to the console, useful for debugging and testing
  • Shell - Use bash as a shell to execute the command, rather than exec a binary directly
  • StdOutWriter - an additional writer for stdout, useful for mutating or filtering the output
  • StdErrWriter - an additional writer for stderr, useful for mutating or filtering the output
  • PrintCommand - print the command to stdout before executing it

Example of exec without streaming to STDIO

This example captures the values from stdout and stderr without relaying to the console. This means the values can be inspected and used for automation.

package main

import (
	"fmt"

	execute "github.com/alexellis/go-execute/v2"
	"context"
)

func main() {
	cmd := execute.ExecTask{
		Command:     "docker",
		Args:        []string{"version"},
		StreamStdio: false,
	}

	res, err := cmd.Execute(context.Background())
	if err != nil {
		panic(err)
	}

	if res.ExitCode != 0 {
		panic("Non-zero exit code: " + res.Stderr)
	}

	fmt.Printf("stdout: %s, stderr: %s, exit-code: %d\n", res.Stdout, res.Stderr, res.ExitCode)
}

Example with "shell" and exit-code 0

package main

import (
	"fmt"

	execute "github.com/alexellis/go-execute/v2"
	"context"
)

func main() {
	ls := execute.ExecTask{
		Command: "ls",
		Args:    []string{"-l"},
		Shell:   true,
	}
	res, err := ls.Execute(context.Background())
	if err != nil {
		panic(err)
	}

	fmt.Printf("stdout: %q, stderr: %q, exit-code: %d\n", res.Stdout, res.Stderr, res.ExitCode)
}

Example with "shell" and exit-code 1

package main

import (
	"fmt"

	"context"
	execute "github.com/alexellis/go-execute/v2"
)

func main() {
	ls := execute.ExecTask{
		Command: "exit 1",
		Shell:   true,
	}
	res, err := ls.Execute(context.Background())
	if err != nil {
		panic(err)
	}

	fmt.Printf("stdout: %q, stderr: %q, exit-code: %d\n", res.Stdout, res.Stderr, res.ExitCode)
}

Contributing

Commits must be signed off with git commit -s

License: MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecResult

type ExecResult struct {
	Stdout    string
	Stderr    string
	ExitCode  int
	Cancelled bool
}

type ExecTask

type ExecTask struct {
	// Command is the command to execute. This can be the path to an executable
	// or the executable with arguments. The arguments are detected by looking for
	// a space.
	//
	// Any arguments must be given via Args
	Command string

	// Args are the arguments to pass to the command. These are ignored if the
	// Command contains arguments.
	Args []string

	// Shell run the command in a bash shell.
	// Note that the system must have `bash` installed in the PATH or in /bin/bash
	Shell bool

	// Env is a list of environment variables to add to the current environment,
	// these are used to override any existing environment variables.
	Env []string

	// Cwd is the working directory for the command
	Cwd string

	// Stdin connect a reader to stdin for the command
	// being executed.
	Stdin io.Reader

	// PrintCommand prints the command before executing
	PrintCommand bool

	// StreamStdio prints stdout and stderr directly to os.Stdout/err as
	// the command runs.
	StreamStdio bool

	// DisableStdioBuffer prevents any output from being saved in the
	// TaskResult, which is useful for when the result is very large, or
	// when you want to stream the output to another writer exclusively.
	DisableStdioBuffer bool

	// StdoutWriter when set will receive a copy of stdout from the command
	StdOutWriter io.Writer

	// StderrWriter when set will receive a copy of stderr from the command
	StdErrWriter io.Writer
}

func (ExecTask) Execute

func (et ExecTask) Execute(ctx context.Context) (ExecResult, error)

Jump to

Keyboard shortcuts

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