Documentation ¶
Overview ¶
Package cmdexec provides a means of executing multiple subcommands with the ability to expand the command line arguments using Go's text/template package and environment variables.
Index ¶
- func AppendToOSEnv(v ...string) []string
- type Option
- func WithCommandsPrefix(v ...string) Option
- func WithDryRun(v bool) Option
- func WithEnv(v []string) Option
- func WithExpandMapping(v func(string) string) Option
- func WithLogger(v func(string, ...any) (int, error)) Option
- func WithStderr(v io.Writer) Option
- func WithStdout(v io.Writer) Option
- func WithTemplateFuncs(v template.FuncMap) Option
- func WithTemplateVars(v any) Option
- func WithVerbose(v bool) Option
- func WithWorkingDir(v string) Option
- type Runner
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendToOSEnv ¶
AppendToOSEnv returns a copy of os.Environ with the supplied environment variables appended to it.
Types ¶
type Option ¶
type Option func(*options)
Option represents an option to New.
func WithCommandsPrefix ¶
WithCommandsPrefix sets a common set of arguments prepended to all commands.
func WithDryRun ¶
WithDryRun logs the commands that would be executed but does not actually execute them.
func WithExpandMapping ¶
WithExpandMapping sets the mapping function to be used to expand environment variables in the command line arguments. The default is os.Getenv.
func WithLogger ¶
WithLogger sets the logger function to be used to log the verbose and dry run output. The default is fmt.Printf.
func WithStderr ¶
WithStderr sets the writer to which the standard error of the commands will be written.
func WithStdout ¶
WithStdout sets the writer to which the standard output of the commands will be written.
func WithTemplateFuncs ¶
WithTemplateFuncs sets the template functions to be used to expand the command line arguments.
func WithTemplateVars ¶
WithTemplateVars sets the template variables to be used to expand the command line arguments.
func WithVerbose ¶
WithVerbose sets the verbose flag for the commands which generally results in the expanded command line execeuted being logged.
func WithWorkingDir ¶
WithWorkingDir sets the working directory for the commands.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner represents a command Runner
Example ¶
package main import ( "context" "fmt" "os" "cloudeng.io/cmdutil/cmdexec" ) func main() { ctx := context.Background() os.Setenv("ENV_VAR", "ENV_VAR_VAL") err := cmdexec.New("test", cmdexec.WithTemplateVars(struct{ A string }{A: "value"}), ).Run(ctx, "echo", "{{.A}}", "$ENV_VAR") if err != nil { fmt.Println(err) } }
Output: value ENV_VAR_VAL
func (*Runner) ExpandCommandLine ¶
ExpandCommandLine expands the supplied command line arguments using the supplied template functions and template variables, followed by environment variale expansion. Template expansion is performed before environment variable expansion, that is, a template expression may evaulate to an environment variable expression (eg. to return ${MYVAR}). NOTE that the environment variables are expanded by the current process and not by the executed command.