sh

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2019 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Overview

Package sh helps you to more easily execute processes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(value string, expander Expander) string

Expand replaces ${var} or $var in the string based on the Expander.

Types

type Expander

type Expander interface {
	// Expand retrieves the value of the variable named
	// by the key. If the variable is found the
	// value (which may be empty) is returned and the boolean is true.
	// Otherwise the returned value will be empty and the boolean will
	// be false.
	Expand(key string) (value string, ok bool)
}

An Expander is used to expand/resolve a variable name to a value

func ChainExpanders

func ChainExpanders(v ...Expander) Expander

Expanders creates an Expander that expands using the provided list of Expanders in order.

You can use this to customize how key not found scenarios are handled. If you want to panic if the key is not found in the OS Env you could build that expander like:

exp := ChainExpanders(ExpandEnv(), ExpandPanic())

func ExpandDisabled

func ExpandDisabled() Expander

ExpandDisabled returns an Expander that evaluates to the same string that describes the expansion.

func ExpandEnv

func ExpandEnv() Expander

ExpandEnv returns an Expander that expands values from the operating system environment.

func ExpandMap

func ExpandMap(m map[string]string) Expander

ExpandMap returns an Expander that values found found in the map.

func ExpandNotFound

func ExpandNotFound() Expander

ExpandNotFound returns an Expander that never finds the value being expanded.

func ExpandPanic

func ExpandPanic() Expander

ExpandPanic returns an Expander that panics when used.

type OutputOptions

type OutputOptions struct {
	NoStderr bool
	NoStdout bool
	NoTrim   bool
}

type Sh

type Sh struct {
	// contains filtered or unexported fields
}

Sh contains all the settings needed to execute process. It is guarded by an immutable builder access model.

func New

func New() *Sh

New returns a new sh.Sh

func (*Sh) Cmd

func (sh *Sh) Cmd() *exec.Cmd

Cmd returns a new exec.Cmd configured with all the settings collected in the sh.Sh

func (*Sh) CommandLog

func (this *Sh) CommandLog(commandLog io.Writer) *Sh

CommandLog returns a new sh.Sh configured io.Writer that will receive the fully expanded command when the process is executed.

func (*Sh) CommandLogPrefix

func (this *Sh) CommandLogPrefix(prefix string) *Sh

CommandLogPrefix returns a new sh.Sh configured with a prfefix to use when logging executed commands.

func (*Sh) Dir

func (this *Sh) Dir(dir string) *Sh

Dir returns a new sh.Sh configured with the directory to run the executed process.

func (*Sh) Env

func (this *Sh) Env(env map[string]string) *Sh

Line returns a new sh.Sh configured with additional env variables to pass to the executed process

func (*Sh) Exec

func (sh *Sh) Exec() (err error)

Exec uses the exec system call to execute the sh.Sh command. When Exec is called the previous go process is replaced by the executed command. Exec panics if called on a sh.Sh that has the default Stdout, Stderr, or Stdin changed. Returns an error if the process cannot be executed.

func (*Sh) ExitCode

func (sh *Sh) ExitCode() int

ExitStatus runs the command and returns the process exit code, or 1 if any other error occured.

func (*Sh) Expand

func (this *Sh) Expand(expander Expander) *Sh

Line returns a new sh.Sh configured with an Expander to control variable expansion. Use Expand(ExpandDisabled()) to disable expanding variables.

func (*Sh) Line

func (this *Sh) Line(commandLine string) *Sh

Line returns a new sh.Sh with the command specified as a single command. The command line is parsed into command line arguments. You can use single and double quotes like you do in bash to group command line arguments. Single quoted strings will have variable expansion disabled.

func (*Sh) LineArgs

func (this *Sh) LineArgs(commandLIne ...string) *Sh

Line returns a new sh.Sh with the specified command line arguments.

func (*Sh) MustExec

func (sh *Sh) MustExec()

MustExec runs the process and panics if it returns a non zero exit code..

func (*Sh) MustZeroExit

func (sh *Sh) MustZeroExit()

MustExec runs the process and panics if it returns a non zero exit code..

func (*Sh) Output

func (sh *Sh) Output(opt ...OutputOptions) (output string, exitCode int, err error)

func (*Sh) Run

func (sh *Sh) Run() error

func (*Sh) Stderr

func (this *Sh) Stderr(writer io.Writer) *Sh

Stderr returns a new sh.Sh configured to write process stderr with the specified writer.

func (*Sh) Stdin

func (this *Sh) Stdin(reader io.Reader) *Sh

Stdin returns a new sh.Sh configured feed process stdin with the specified reader.

func (*Sh) Stdout

func (this *Sh) Stdout(writer io.Writer) *Sh

Stdout returns a new sh.Sh configured to write process stdout with the specified writer.

func (*Sh) String

func (sh *Sh) String() string

Directories

Path Synopsis
Package line lets you parse command line strings into arguments.
Package line lets you parse command line strings into arguments.

Jump to

Keyboard shortcuts

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