commands

package
v2.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const DoubleQuote = "{DOUBLE_QUOTE}"
View Source
const SpaceWithinQuote = "{SPACE_WITHIN_QUOTE}"

Variables

This section is empty.

Functions

func Create added in v2.3.0

func Create(plainCommand string) *exec.Cmd

Create creates a command from a single string This allows you to pass parameters which include spaces as commands. You just need to add "double-quotes" around the parameter, and it will be treated as one parameter and not be split by whitespace.

Types

type Executor

type Executor interface {
	// Execute will run the given command, returning the stdout output and errors (if any).
	// In chatty mode, Execute shows output in both console and file. In quiet mode, output is piped only to the file.
	// Stdout is collected as return output, Stderr is not collected in the output.
	// Any errors are returned as err return value.
	Execute(command string) (output string, err error)

	// ExecuteCmd is same as Execute, except you can provide the os/exec command directly. Useful to avoid Executor escaping logic,
	// in rare cases where the command does not follow the usual --argument value semantics.
	ExecuteCmd(cmd *exec.Cmd) (output string, err error)

	// ExecuteWithProgressInfo is same as Execute, except an infinite progress bar is shown, signaling an async operation to
	// the user. The progress bar can be overridden with Viper parameter "silence-long-running-progress-indicators" - useful
	// for CI for example.
	ExecuteWithProgressInfo(command string) (output string, err error)

	// ExecuteCmdWithProgressInfo is same as ExecuteWithProgressInfo, except you can provide the os/exec command directly. Useful to avoid Executor escaping logic,
	// in rare cases where the command does not follow the usual --argument value semantics.
	ExecuteCmdWithProgressInfo(cmd *exec.Cmd) (output string, err error)

	// ExecuteSilent will run the given command, returning the stdout output and errors (if any).
	// No command output is shown on the console or logged to the file (irrelevant of the chatty / quiet setting). Can be
	// used for commands that are too verbose and clutter the output.
	// Stdout is collected as return output, Stderr is not collected in the output.
	// Any errors are returned as err return value.
	ExecuteSilent(command string) (output string, err error)

	// ExecuteLoud will run the given command, returning the stdout output and errors (if any).
	// Command output is always shown on the console and logged to the file (irrelevant of the chatty / quiet setting). Can be
	// used for commands that are always of output relevant for the user (like an interactive login command or similar)
	ExecuteLoud(command string) (output string, err error)

	// ExecuteCmdSilent is same as ExecuteSilent, except you can provide the os/exec command directly. Useful to avoid Executor escaping logic,
	// in rare cases where the command does not follow the usual --argument value semantics.
	ExecuteCmdSilent(cmd *exec.Cmd) (output string, err error)

	// ExecuteTTY is a special executor for cases where the called command needs to detect it runs in a TTY session.
	// One example of such command is Docker. Commands executed via ExecuteTTY have their output shown on the console,
	// but the output is NOT saved to a log file. Chatty / Quiet settings have no effect on this method.
	ExecuteTTY(command string) error

	// ExecuteCmdTTY is same as ExecuteTTY, except you can provide the os/exec command directly. Useful to avoid Executor escaping logic,
	// in rare cases where the command does not follow the usual --argument value semantics.
	ExecuteCmdTTY(cmd *exec.Cmd) error

	// AskUserToConfirm pauses the execution, and awaits for user to confirm (by either typing yes, Y or y).
	// Parameter displayMessage can be used to show a message on the screen.
	AskUserToConfirm(displayMessage string) bool

	// AskUserToConfirmWithKeyword pauses the execution, and awaits for user to confirm with the requested keyword.
	// Parameter displayMessage can be used to show a message on the screen.
	AskUserToConfirmWithKeyword(displayMessage string, keyword string) bool
}

func NewChatty

func NewChatty(logFileName string, logger *logrus.Logger) Executor

NewChatty creates a new Executor instance. Chatty executor outputs the command output to both file and console at the same time. Best suited for application IaC projects. Required dependencies are the log file name for command output, and the logging subsystem instance. Log file is required, because command output requires to be directly outputted to the file, without logging system interfering with formatting. Logging system is an explicit dependency, so that it is clear that the logging system need to bo be initialized first, before creating an Executor.

func NewQuiet

func NewQuiet(logFileName string, logger *logrus.Logger) Executor

NewQuiet creates a new Executor instance. Quiet executor outputs the command output only to a file, console output is suppressed. Best suited for applications where the console should be kept clean and simple because of large amount of commands being executed. Required dependencies are the log file name for command output, and the logging subsystem instance. Quiet Executor supports a viper flag "verbose", so that the command output can be piped to the console if needed (useful in CI scenarios). Log file is required, because command output requires to be directly outputted to the file, without logging system interfering with formatting. Logging system is an explicit dependency, so that it is clear that the logging system need to bo be initialized first, before creating an Executor.

Jump to

Keyboard shortcuts

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