Documentation ¶
Index ¶
- type Cmd
- func (c Cmd) Compile() (Cmd, error)
- func (c Cmd) CompileExec(executor ExecFunc) (Process, error)
- func (dd Cmd) Data() map[string]any
- func (dd Cmd) Env() []string
- func (c Cmd) Exec(executor ExecFunc) (Process, error)
- func (c Cmd) MustCompile() Cmd
- func (c Cmd) OSCmd() (*exec.Cmd, error)
- func (c Cmd) Raw() []string
- func (c Cmd) String() string
- func (c Cmd) WithArg(arg string) Cmd
- func (c Cmd) WithArgs(args ...string) Cmd
- func (c Cmd) WithEnv(env ...string) Cmd
- func (c Cmd) WithField(key string, value any) Cmd
- func (c Cmd) WithFields(fields map[string]any, overwrite bool) Cmd
- func (c Cmd) WithFormatter(formatter Formatter) Cmd
- func (c Cmd) WithLocalOSEnv() Cmd
- type EvalFunc
- type ExecFunc
- type Formatter
- type Output
- type Process
- type Result
- type Script
- func (s Script) Cmd() Cmd
- func (s Script) Compile() (Script, error)
- func (dd Script) Data() map[string]any
- func (dd Script) Env() []string
- func (s Script) MustCompile() Script
- 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) WithLocalOSEnv() Script
- func (s Script) WithSubcommand(sc Subcommand) Script
- type Subcommand
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
type Cmd struct {
// contains filtered or unexported fields
}
func (Cmd) Compile ¶
Compile uses the go template engine and the provided data fields to compile the command. These in-turn act a more portable approach than command-line arguments.
func (Cmd) 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 (Cmd) Env ¶
func (dd Cmd) Env() []string
Env returns the env vars in KEY=VALUE format that will be used when executing the script/cmd.
func (Cmd) 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 (Cmd) MustCompile ¶
MustCompile compiles the command, however will panic if an error occurred.
func (Cmd) OSCmd ¶
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 (Cmd) Raw ¶
Raw returns the command split by its arguments in its current state. If not compiled, handlebar values will still be present.
func (Cmd) String ¶
String uses the command's formatter to convert the raw command (a main executable path and a slice of arguments) into a single string.
func (Cmd) WithArg ¶
WithArg adds an argument to the end of the current arguments slice associated with the command.
func (Cmd) WithArgs ¶
WithArgs appends multiple args to the end of the argument slice currently associated with the command.
func (Cmd) WithEnv ¶
WithEnv takes one or more environmental variables in KEY=VALUE format. These will be used when executing the command. These will not be applied to the actual arguments of the command, but to any subprocess spawned by the command. This is different from the Env behavior of a Script.
func (Cmd) WithField ¶
WithField adds a key/value to the map of template data to be used when compiling the command. If the key already exists, it is overwritten.
func (Cmd) WithFields ¶
WithFields takes a map of fields that is merged with the current command data. If a key already exists in the command data, overwite must be set to true in order to replace it, otherwise that key/value is left untouched.
func (Cmd) WithFormatter ¶
func (Cmd) WithLocalOSEnv ¶
WithLocalOSEnv appends the environmental variables from the local system to the env var set currently held be the command.
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 Formatter ¶
Formatter is a function that can convert a raw command (a string slice) into a single string. However, as different commands should be treated differently in this regard, the formatter dictates how this conversion happens.
func QuoteIfSpaceFormatter ¶
QuoteIfSpaceFormatter acts similarly to SpaceSepFormatter, however if any argument contains a space, it will be wrapped in quotes.
func QuoteLastArgFormatter ¶
QuoteLastArgFormatter joins all command and all arguments with a single space to create a string. However, it will wrap the final argument in quotes (using a the given quote chars).
func SpaceSepFormatter ¶
func SpaceSepFormatter() Formatter
SpaceSepFormatter joins all command and all arguments with a single space to create a single string value. This is perhaps the most basic formatter. For example, if a single argument contains spaces, the resulting string will present no differences between an argument that contains spaces, and the spaces used to separate arguments.
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) Cmd ¶
Cmd converts the script to a Cmd in the format [subcommand parts... , raw]. For example, the raw script "ping 8.8.8.8" with a subcommand ["sh", "-c"], this would result in a Cmd equivalent to ["sh", "-c", "ping 8.8.8.8"]. This does not compile the script first or the command after, thus handlebar values will persist.
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) Data ¶
Data returns the map of template data to be used when compiling the script/cmd.
func (Script) Env ¶
func (dd Script) Env() []string
Env returns the env vars in KEY=VALUE format that will be used when executing the script/cmd.
func (Script) MustCompile ¶
MustCompile compiles the script, however will panic if an error occurs.
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 ¶
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.
func (Script) WithLocalOSEnv ¶
WithLocalOSEnv appends the environmental variables from the local system to the env var set currently held be the script.
func (Script) WithSubcommand ¶
func (s Script) WithSubcommand(sc Subcommand) Script
type Subcommand ¶
type Subcommand []string
Subcommand is the actual command to be executed by a given ExecFunc. The remaining script is passed as the final argument to the subcommand, for example a script to be interpreted/executed by the shell ["sh", "-c"] would be the appropriate subcommand.
var ( SCBinary Subcommand = nil SCShell Subcommand = []string{"sh", "-c"} SCBash Subcommand = []string{"bash", "-c"} SCAsh Subcommand = []string{"ash", "-c"} SCPython Subcommand = []string{"python", "-c"} SCPython3 Subcommand = []string{"python3", "-c"} )