Documentation
¶
Index ¶
- type EvalFunc
- type ExecFunc
- type Output
- type Process
- type Result
- type Script
- func (s Script) Compile() (Script, error)
- func (s Script) CompileExec(executor ExecFunc) (Process, error)
- func (s Script) Data() map[string]any
- func (s Script) Env() []string
- func (s Script) Exec(executor ExecFunc) (Process, error)
- func (s Script) MustCompile() Script
- func (s Script) OSCmd(subcommand []string) (*exec.Cmd, error)
- func (s Script) Raw() string
- func (s Script) WithEnv(env ...string) Script
- func (s Script) WithField(key string, value any) Script
- func (s Script) WithFields(fields map[string]any, overwrite bool) Script
- func (s Script) WithOSEnv() Script
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExecFunc ¶
ExecFunc is used to execute a script, in-turn creating a process. If the script fails to be executed for any reason, this function can also error.
type Output ¶
Output is a key/value map of data, where the value can be any type. This should be generated from NewOutput or from the helper methods of a result.
func NewOutput ¶
NewOutput creates an Output from a given input string (such as stdout). It will type cast select types if a type is given in the set-output message (or a string if not).
type Process ¶
type Process interface { // Kill sends a SIGKILL to the running process. If this fails, for example if // the process is not running, this will return an error. Kill() error // Signal sends a signal (such as SIGINT) to the running process. If this // fails, for example if the process is not running, this will return an // error. Signal(os.Signal) error // Write sends a string to the process's STDIN. Note that the string is sent // as-is. Thus if the program is looking for a newline before the read is // complete, this must be included in the string provided. If the write fails, // an error is returned. Write(string) error // Result waits for a script to complete execution, then a result is returned. // If the script returns an unknown error, this will also error. Result() (*Result, error) // Close should be called on a process, freeing any resources used where // appropriate. Close() }
Process is a single instance of the script, either running or exited. A process can be used to control the script and extract results from a script that has completed its execution.
type Result ¶
type Result struct { StdOut string `json:"stdout"` StdErr string `json:"stderr"` ExitCode int `json:"exitCode"` TotalTime time.Duration `json:"executionTime"` }
Result represents the output of a completed script execution. This is only created by the `Result` function of a "Process" and thus the process must have exited.
func (Result) CombinedOutput ¶
Output parses the specified outputs from the script's stdOut and stdErr. In the event that stdOut and stdErr specify an output of the same name, the value from stdOut is preferred. This is returned as a map. Any field that is not correctly parsed, will simply be ignored.
type Script ¶
type Script struct {
// contains filtered or unexported fields
}
Script is some executable string, along with data to supplement its execution, such as template data and env vars. The string can contain go template handles. These are to replace script arguments, as the use of arguments can be complex on certain platforms where the script may be executed.
func NewScriptFromFile ¶
NewScriptFromFile creates a Script from the string extracted from a given file. This can error if the file can not be read.
func NewScriptFromHTTP ¶
NewScriptFromHTTP creates a Script from the string extracted from a given URL. This can error if the contents of the remote resource can not be read.
func (Script) Compile ¶
Compile uses the go template engine and the provided data fields to compile the script. These in-turn act a more portable approach than command-line arguments.
func (Script) CompileExec ¶
CompileExec will "compile" the script using the given data and the golang template system, then calls the given ExecFunc. Returned will be the process that is created as a result of execution. An error is returned if the script fails to execute for any reason.
func (Script) Env ¶
Env returns the env vars in KEY=VALUE format that will be used when executing the script.
func (Script) Exec ¶
Exec will call the given ExecFunc to execute the script. Returned will be the process that is created as a result of execution. An error is returned if the script fails to execute for any reason.
func (Script) MustCompile ¶
MustCompile compiles the script, however will panic if an error occurs.
func (Script) OSCmd ¶ added in v0.6.0
OSCmd attempts to convert the script to an os.exec package Cmd. For this, a subcommand must be provided. For example, if the subcommand ["sh", "-c"] was provided, and the compiled script was `echo 'Hello, world!'“, the resulting command would be equivalent to: sh -c "echo 'Hello, world!'". Env vars given to the script are also preserved in the resulting Cmd.
func (Script) Raw ¶
Raw returns the raw executable string as is. If the script contains template handlebars, they will be returned as provided, not compiled.
func (Script) WithEnv ¶
WithEnv takes one or more environmental variables in KEY=VALUE format. These will be used when executing the script.
func (Script) WithField ¶
WithField adds a key/value to the map of template data to be used when compiling the script. If the key already exists, it is overwritten.
func (Script) WithFields ¶ added in v0.6.0
WithFields takes a map of fields that is merged with the current script data. If a key already exists in the script data, overwite must be set to true in order to replace it, otherwise that key/value is left untouched.