cmdexec

package
v0.0.0-...-e2c53ed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 7 Imported by: 2

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendToOSEnv

func AppendToOSEnv(v ...string) []string

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

func WithCommandsPrefix(v ...string) Option

WithCommandsPrefix sets a common set of arguments prepended to all commands.

func WithDryRun

func WithDryRun(v bool) Option

WithDryRun logs the commands that would be executed but does not actually execute them.

func WithEnv

func WithEnv(v []string) Option

WithEnv sets the environment variables to be made available to the executed command.

func WithExpandMapping

func WithExpandMapping(v func(string) string) Option

WithExpandMapping sets the mapping function to be used to expand environment variables in the command line arguments. The default is os.Getenv.

func WithLogger

func WithLogger(v func(string, ...any) (int, error)) Option

WithLogger sets the logger function to be used to log the verbose and dry run output. The default is fmt.Printf.

func WithStderr

func WithStderr(v io.Writer) Option

WithStderr sets the writer to which the standard error of the commands will be written.

func WithStdout

func WithStdout(v io.Writer) Option

WithStdout sets the writer to which the standard output of the commands will be written.

func WithTemplateFuncs

func WithTemplateFuncs(v template.FuncMap) Option

WithTemplateFuncs sets the template functions to be used to expand the command line arguments.

func WithTemplateVars

func WithTemplateVars(v any) Option

WithTemplateVars sets the template variables to be used to expand the command line arguments.

func WithVerbose

func WithVerbose(v bool) Option

WithVerbose sets the verbose flag for the commands which generally results in the expanded command line execeuted being logged.

func WithWorkingDir

func WithWorkingDir(v string) Option

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 New

func New(name string, opts ...Option) *Runner

New creates a new Runner instance with the supplied name and options.

func (*Runner) ExpandCommandLine

func (r *Runner) ExpandCommandLine(args ...string) ([]string, error)

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.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, cmds ...string) error

Run executes the supplied commands with the expanded command line as per ExpandCommandLine.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL