commandlineexecutor

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package commandlineexecutor creates an interface to streamline execution of shell commands across multiple platforms

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommandExists

func CommandExists(executable string) bool

CommandExists returns whether or not an executable command exists within the current os runtime environment.

Types

type Execute

type Execute func(params Params) Result

Execute is a function to execute a command. Production callers to pass commandlineexecutor.ExecuteCommand while calling this package's APIs.

type Exists

type Exists func(string) bool

Exists is a function to check if a command exists. Production callers to pass commandlineexecutor.CommandExists while calling this package's APIs.

type ExitCode

type ExitCode func(err error) int

ExitCode is a function to get the exit code from an error. Production callers to pass commandlineexecutor.CommandExitCode while calling this package's APIs.

type Params

type Params struct {
	Executable string
	// One of ArgsToSplit or Args should be defined on the Params.
	// ArgsToSplit should be preferred when issuing commands with a subshell and using "-c".
	// An example would be an invocation like:
	//   Executable: "/bin/sh"
	//   ArgsToSplit: "-c 'ls /usr/sap/*/SYS/global/hdb/custom/config/global.ini'"
	// In this case ArgsToSplit will be split up correctly as:
	//   []string{"-c", "'ls /usr/sap/*/SYS/global/hdb/custom/config/global.ini'"}
	ArgsToSplit string
	Args        []string
	Timeout     int // defaults to 60, so timeout will occur in 60 seconds
	User        string
	Env         []string
}

Params encapsulates the parameters used by the Exec* and RunWithEnv funcs.

type Result

type Result struct {
	StdOut, StdErr   string
	ExitCode         int
	Error            error
	ExecutableFound  bool
	ExitStatusParsed bool // Will be true if "exit status ([0-9]+)" is in the error result
}

Result holds the stdout, stderr, exit code, and error from the execution.

func ExecuteCommand

func ExecuteCommand(params Params) Result

ExecuteCommand takes Params and returns a Result.

If the params.Executable does not exist it will return early with the Result.Error filled If the Params ArgsToSplit is not empty then it will be split into an arguments array Else the Args will be used as the arguments array If the User is not empty then the command will be executed as that user If Env is defined then that environment will be used to execute the command

The returned Result will contain the standard out, standard error, the exit code and an error if one was encountered during execution.

type Run

type Run func() error

Run is a testable version of the exec.Run method. Should only be used during testing.

type SetupExeForPlatform

type SetupExeForPlatform func(exe *exec.Cmd, params Params) error

SetupExeForPlatform is a testable version of the setupExeForPlatform call. Should only be used during testing.

Jump to

Keyboard shortcuts

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