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, ...)
- func ApplicationWithPath(funcs map[string]func([]string), scripts []string, ...)
- func Asset(name string) ([]byte, error)
- func AssetDir(name string) ([]string, error)
- func AssetInfo(name string) (os.FileInfo, error)
- func AssetNames() []string
- func MustAsset(name string) []byte
- func RestoreAsset(dir, name string) error
- func RestoreAssets(dir, name string) error
- 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.
func ApplicationWithPath ¶
func ApplicationWithPath( funcs map[string]func([]string), scripts []string, loader func(string) ([]byte, error), copyEnv bool, bashPath string)
ApplicationWithPath functions as Application does while also allowing the developer to modify the specified bashPath.
func Asset ¶
Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.
func AssetDir ¶
AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:
data/ foo.txt img/ a.png b.png
then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.
func AssetInfo ¶
AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.
func MustAsset ¶
MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.
func RestoreAsset ¶
RestoreAsset restores an asset under the given directory
func RestoreAssets ¶
RestoreAssets restores an asset under the given directory recursively
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 ¶
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.