sh

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Require

func Require(verbose bool) func(runtime *goja.Runtime, module *goja.Object)

Types

type ExecutionError

type ExecutionError struct {
	Command  string
	Args     []string
	ExitCode int
}

func (ExecutionError) Error

func (t ExecutionError) Error() string

type Module

type Module struct {
	Verbose bool
	// contains filtered or unexported fields
}

func New

func New(verbose bool) *Module

func (*Module) CmdRan

func (sh *Module) CmdRan(err error) bool

CmdRan examines the error to determine if it was generated as a result of a command running via os/exec.Command. If the error is nil, or the command ran (even if it exited with a non-zero exit code), CmdRan reports true. If the error is an unrecognized type, or it is an error from exec.Command that says the command failed to run (usually due to the command not existing or not being executable), it reports false.

func (*Module) Exec

func (sh *Module) Exec(env map[string]string, stdout, stderr io.Writer, cmd string, args ...string) (ran bool, err error)

Exec executes the command, piping its stderr to mage's stderr and piping its stdout to the given writer. If the command fails, it will return an error that, if returned from a target or mg.Deps call, will cause mage to exit with the same code as the command failed with. Env is a list of environment variables to set when running the command, these override the current environment variables set (which are also passed to the command). cmd and args may include references to environment variables in $FOO format, in which case these will be expanded before the command is run.

Ran reports if the command ran (rather than was not found or not executable). Code reports the exit code the command returned if it ran. If err == nil, ran is always true and code is always 0.

func (*Module) ExitStatus

func (sh *Module) ExitStatus(err error) int

ExitStatus returns the exit status of the error if it is an exec.ExitError or if it implements ExitStatus() int. 0 if it is nil or 1 if it is a different error.

func (*Module) OutCmd

func (sh *Module) OutCmd(cmd string, args ...string) func(args ...string) (string, error)

OutCmd is like RunCmd except the command returns the output of the command.

func (*Module) Output

func (sh *Module) Output(cmd string, args ...string) (string, error)

Output runs the command and returns the text from stdout.

func (*Module) OutputWith

func (sh *Module) OutputWith(env map[string]string, cmd string, args ...string) (string, error)

OutputWith is like RunWith, but returns what is written to stdout.

func (*Module) Run

func (sh *Module) Run(cmd string, args ...string) error

Run is like RunWith, but doesn't specify any environment variables.

func (*Module) RunCmd

func (sh *Module) RunCmd(cmd string, args ...string) func(args ...string) error

RunCmd returns a function that will call Run with the given command. This is useful for creating command aliases to make your scripts easier to read, like this:

 // in a helper file somewhere
 var g0 = sh.RunCmd("go")  // go is a keyword :(

 // somewhere in your main code
	if err := g0("install", "github.com/gohugo/hugo"); err != nil {
		return err
 }

Args passed to command get baked in as args to the command when you run it. Any args passed in when you run the returned function will be appended to the original args. For example, this is equivalent to the above:

var goInstall = sh.RunCmd("go", "install") goInstall("github.com/gohugo/hugo")

RunCmd uses Exec underneath, so see those docs for more details.

func (*Module) RunV

func (sh *Module) RunV(cmd string, args ...string) error

RunV is like Run, but always sends the command's stdout to os.Stdout.

func (*Module) RunWith

func (sh *Module) RunWith(env map[string]string, cmd string, args ...string) error

RunWith runs the given command, directing stderr to this program's stderr and printing stdout to stdout if mage was run with -v. It adds adds env to the environment variables for the command being run. Environment variables should be in the format name=value.

func (*Module) RunWithV

func (sh *Module) RunWithV(env map[string]string, cmd string, args ...string) error

RunWithV is like RunWith, but always sends the command's stdout to os.Stdout.

Jump to

Keyboard shortcuts

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