Documentation ¶
Overview ¶
Package basher provides an API for running and integrating with Bash from Go
Index ¶
- func Application(funcs map[string]func([]string), scripts []string, ...)
- type Context
- func (c *Context) CopyEnv()
- func (c *Context) Export(name string, value string)
- func (c *Context) ExportFunc(name string, fn func([]string))
- func (c *Context) HandleFuncs(args []string) bool
- func (c *Context) Run(command string, args []string) (int, error)
- func (c *Context) Source(filepath string, loader func(string) ([]byte, error)) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Application ¶
func Application( funcs map[string]func([]string), scripts []string, loader func(string) ([]byte, error), copyEnv bool)
Application sets up a common entrypoint for a Bash application that uses exported Go functions. It uses the DEBUG environment variable to set debug on the Context, and SHELL for the Bash binary if it includes the string "bash". You can pass a loader function to use for the sourced files, and a boolean for whether or not the environment should be copied into the Context process.
Types ¶
type Context ¶
type Context struct { sync.Mutex // Debug simply leaves the generated BASH_ENV file produced // from each Run call of this Context for debugging. Debug bool // BashPath is the path to the Bash executable to be used by Run BashPath string // SelfPath is set by NewContext to be the current executable path. // It's used to call back into the calling Go process to run exported // functions. SelfPath string // The io.Reader given to Bash for STDIN Stdin io.Reader // The io.Writer given to Bash for STDOUT Stdout io.Writer // The io.Writer given to Bash for STDERR Stderr io.Writer // contains filtered or unexported fields }
A Context is an instance of a Bash interpreter and environment, including sourced scripts, environment variables, and embedded Go functions
func NewContext ¶
Creates and initializes a new Context that will use the given Bash executable. The debug mode will leave the produced temporary BASH_ENV file for inspection.
func (*Context) CopyEnv ¶
func (c *Context) CopyEnv()
Copies the current environment variables into the Context
func (*Context) ExportFunc ¶
Registers a function with the Context that will produce a Bash function in the environment that calls back into your executable triggering the function defined as fn.
func (*Context) HandleFuncs ¶
Expects your os.Args to parse and handle any callbacks to Go functions registered with ExportFunc. You normally call this at the beginning of your program. If a registered function is found and handled, HandleFuncs will exit with the appropriate exit code for you.
func (*Context) Run ¶
Runs a command in Bash from this Context. With each call, a temporary file is generated used as BASH_ENV when calling Bash that includes all variables, sourced scripts, and exported functions from the Context. Standard I/O by default is attached to the calling process I/O. You can change this by setting the Stdout, Stderr, Stdin variables of the Context.
func (*Context) Source ¶
Adds a shell script to the Context environment. The loader argument can be nil which means it will use ioutil.Readfile and load from disk, but it exists so you can use the Asset function produced by go-bindata when including script files in your Go binary. Calls to Source adds files to the environment in order.