exec

package
v0.18.2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: Apache-2.0 Imports: 10 Imported by: 9

Documentation

Overview

Package exec provides helpers for setting execution options for commands

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRemote = errors.New("remote exec error") // ErrRemote is returned when an action fails on remote host
	ErrSudo   = errors.New("sudo error")        // ErrSudo is returned when wrapping a command with sudo fails
)
View Source
var (
	// DisableRedact will make redact not redact anything
	DisableRedact = false
	// Confirm will make all command execs ask for confirmation - this is a simplistic way for auditing what will be executed
	Confirm = false

	// DebugFunc can be replaced to direct the output of exec logging into your own function (standard sprintf interface)
	DebugFunc = func(s string, args ...any) {
		log.Debugf(s, args...)
	}

	// InfoFunc can be replaced to direct the output of exec logging into your own function (standard sprintf interface)
	InfoFunc = func(s string, args ...any) {
		log.Infof(s, args...)
	}

	// ErrorFunc can be replaced to direct the output of exec logging into your own function (standard sprintf interface)
	ErrorFunc = func(s string, args ...any) {
		log.Errorf(s, args...)
	}

	// ConfirmFunc is called to ask for confirmation
	ConfirmFunc = func(s string) bool {
		fmt.Println(s)
		fmt.Print("Allow? [Y/n]: ")
		reader := bufio.NewReader(os.Stdin)
		text, _ := reader.ReadString('\n')
		text = strings.TrimSpace(text)
		return text == "" || text == "Y" || text == "y"
	}
)

Functions

This section is empty.

Types

type Option

type Option func(*Options)

Option is a functional option for the exec package

func AllowWinStderr

func AllowWinStderr() Option

AllowWinStderr exec option allows command to output to stderr without failing

func HideCommand

func HideCommand() Option

HideCommand exec option for hiding the command-string and stdin contents from the logs

func HideOutput

func HideOutput() Option

HideOutput exec option for hiding the command output from logs

func LogError added in v0.17.2

func LogError(v bool) Option

LogError exec option for enabling or disabling live error logging during exec

func Output

func Output(output *string) Option

Output exec option for setting output string target

func Redact

func Redact(rexp string) Option

Redact exec option for defining a redact regexp pattern that will be replaced with [REDACTED] in the logs

func RedactString

func RedactString(s ...string) Option

RedactString exec option for defining one or more strings to replace with [REDACTED] in the log output

func Sensitive

func Sensitive() Option

Sensitive exec option for disabling all logging of the command

func Stdin

func Stdin(t string) Option

Stdin exec option for sending data to the command through stdin

func StreamOutput

func StreamOutput() Option

StreamOutput exec option for sending the command output to info log

func Sudo added in v0.4.0

func Sudo(h host) Option

Sudo exec option for running the command with elevated permissions

func Writer added in v0.3.20

func Writer(w io.Writer) Option

Writer exec option for sending command stdout to an io.Writer

type Options

type Options struct {
	Stdin          string
	AllowWinStderr bool
	LogInfo        bool
	LogDebug       bool
	LogError       bool
	LogCommand     bool
	LogOutput      bool
	StreamOutput   bool
	Sudo           bool
	RedactFunc     func(string) string
	Output         *string
	Writer         io.Writer
	// contains filtered or unexported fields
}

Options is a collection of exec options

func Build

func Build(opts ...Option) *Options

Build returns an instance of Options

func (*Options) AddOutput

func (o *Options) AddOutput(prefix, stdout, stderr string)

AddOutput is for appending / displaying output of the command

func (*Options) Command added in v0.4.0

func (o *Options) Command(cmd string) (string, error)

Command returns the command wrapped in a sudo if sudo is enabled or the original command

func (*Options) LogCmd

func (o *Options) LogCmd(prefix, cmd string)

LogCmd is for logging the command to be executed

func (*Options) LogDebugf

func (o *Options) LogDebugf(s string, args ...any)

LogDebugf is a conditional debug logger

func (*Options) LogErrorf

func (o *Options) LogErrorf(s string, args ...any)

LogErrorf is a conditional error logger

func (*Options) LogInfof

func (o *Options) LogInfof(s string, args ...any)

LogInfof is a conditional info logger

func (*Options) LogStdin

func (o *Options) LogStdin(prefix string)

LogStdin is for logging information about command stdin input

func (*Options) Redact

func (o *Options) Redact(s string) string

Redact is for filtering out sensitive text using a regexp

type Waiter added in v0.16.0

type Waiter interface {
	Wait() error
}

Waiter is a process that can be waited to finish

Jump to

Keyboard shortcuts

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