commandexecutor

package
v0.372.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

README

commandexector

CommandExecutors have an important role in this library since they allow us to:

  • Execute commands on remote systems like (jump-)hosts reachable over the SSH.
  • Execute the same command on the local machine, inside a container (no matter if docker, kubernetes, ...)
  • Can be used to short cut the amount of development time since we already know how to do things with redirects in bash or a specific tool...

But it has downsides:

  • It's not real programming, it's abusing golang for scripting automation.
  • It's a security risk. Calling exce (especially with unchecked user input as parameter) leads to security issues.

Avoid exec calls.

To avoid exec calls on the local machine set the env var accordingly:

export ASCIICHGOLANGPUBLIC_AVOID_EXEC=1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsLiveOutputOnStdoutEnabled added in v0.300.0

func IsLiveOutputOnStdoutEnabled(ctx context.Context) bool

func WithLiveOutputOnStdout added in v0.300.0

func WithLiveOutputOnStdout(ctx context.Context) context.Context

func WithLiveOutputOnStdoutEnabled added in v0.300.0

func WithLiveOutputOnStdoutEnabled(ctx context.Context, enabled bool) context.Context

func WithLiveOutputOnStdoutIfVerbose added in v0.300.0

func WithLiveOutputOnStdoutIfVerbose(ctx context.Context) context.Context

Types

type BashService

type BashService struct {
	CommandExecutorBase
}

func Bash

func Bash() (b *BashService)

Can be used to run commands in bash on localhost.

func NewBashService

func NewBashService() (b *BashService)

func (*BashService) GetDeepCopy

func (b *BashService) GetDeepCopy() (deepCopy CommandExecutor)

func (*BashService) GetHostDescription

func (b *BashService) GetHostDescription() (hostDescription string, err error)

func (*BashService) RunCommand

func (b *BashService) RunCommand(ctx context.Context, options *parameteroptions.RunCommandOptions) (commandOutput *CommandOutput, err error)

func (*BashService) RunOneLiner

func (b *BashService) RunOneLiner(ctx context.Context, oneLiner string) (output *CommandOutput, err error)

func (*BashService) RunOneLinerAndGetStdoutAsLines

func (b *BashService) RunOneLinerAndGetStdoutAsLines(ctx context.Context, oneLiner string) (stdoutLines []string, err error)

func (*BashService) RunOneLinerAndGetStdoutAsString

func (b *BashService) RunOneLinerAndGetStdoutAsString(ctx context.Context, oneLiner string) (stdout string, err error)

type CommandExecutor

type CommandExecutor interface {
	GetHostDescription() (hostDescription string, err error)
	RunCommand(ctx context.Context, options *parameteroptions.RunCommandOptions) (commandOutput *CommandOutput, err error)

	// These Commands can be implemented by embedding the `CommandExecutorBase` struct:
	IsRunningOnLocalhost() (isRunningOnLocalhost bool, err error)
	RunCommandAndGetStdoutAsBytes(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout []byte, err error)
	RunCommandAndGetStdoutAsFloat64(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout float64, err error)
	RunCommandAndGetStdoutAsInt64(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout int64, err error)
	RunCommandAndGetStdoutAsLines(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdoutLines []string, err error)
	RunCommandAndGetStdoutAsString(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout string, err error)
}

A CommandExecutor is able to run a command like Exec or bash does.

func GetDeepCopyOfCommandExecutor

func GetDeepCopyOfCommandExecutor(commandExectuor CommandExecutor) (copy CommandExecutor, err error)

type CommandExecutorBase

type CommandExecutorBase struct {
	// contains filtered or unexported fields
}

func NewCommandExecutorBase

func NewCommandExecutorBase() (c *CommandExecutorBase)

func (*CommandExecutorBase) GetParentCommandExecutorForBaseClass

func (c *CommandExecutorBase) GetParentCommandExecutorForBaseClass() (parentCommandExecutorForBaseClass CommandExecutor, err error)

func (*CommandExecutorBase) IsRunningOnLocalhost

func (c *CommandExecutorBase) IsRunningOnLocalhost() (isRunningOnLocalhost bool, err error)

func (*CommandExecutorBase) RunCommandAndGetStdoutAsBytes

func (c *CommandExecutorBase) RunCommandAndGetStdoutAsBytes(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout []byte, err error)

func (*CommandExecutorBase) RunCommandAndGetStdoutAsFloat64

func (c *CommandExecutorBase) RunCommandAndGetStdoutAsFloat64(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout float64, err error)

func (*CommandExecutorBase) RunCommandAndGetStdoutAsInt64

func (c *CommandExecutorBase) RunCommandAndGetStdoutAsInt64(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout int64, err error)

func (*CommandExecutorBase) RunCommandAndGetStdoutAsLines

func (c *CommandExecutorBase) RunCommandAndGetStdoutAsLines(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdoutLines []string, err error)

func (*CommandExecutorBase) RunCommandAndGetStdoutAsString

func (c *CommandExecutorBase) RunCommandAndGetStdoutAsString(ctx context.Context, options *parameteroptions.RunCommandOptions) (stdout string, err error)

func (*CommandExecutorBase) SetParentCommandExecutorForBaseClass

func (c *CommandExecutorBase) SetParentCommandExecutorForBaseClass(parentCommandExecutorForBaseClass CommandExecutor) (err error)

type CommandOutput

type CommandOutput struct {
	ReturnCode *int
	Stdout     *[]byte
	Stderr     *[]byte
	// contains filtered or unexported fields
}

func NewCommandOutput

func NewCommandOutput() (c *CommandOutput)

func (*CommandOutput) CheckExitSuccess

func (o *CommandOutput) CheckExitSuccess(verbose bool) (err error)

func (*CommandOutput) GetCmdRunError

func (c *CommandOutput) GetCmdRunError() (cmdRunError *error, err error)

func (*CommandOutput) GetCmdRunErrorStringOrEmptyStringIfUnset

func (o *CommandOutput) GetCmdRunErrorStringOrEmptyStringIfUnset() (cmdRunErrorString string)

func (*CommandOutput) GetFirstLineOfStdoutAsString

func (c *CommandOutput) GetFirstLineOfStdoutAsString() (firstLine string, err error)

func (*CommandOutput) GetReturnCode

func (o *CommandOutput) GetReturnCode() (returnCode int, err error)

func (*CommandOutput) GetStderr

func (c *CommandOutput) GetStderr() (stderr *[]byte, err error)

func (*CommandOutput) GetStderrAsString

func (o *CommandOutput) GetStderrAsString() (stderr string, err error)

func (*CommandOutput) GetStderrAsStringOrEmptyIfUnset

func (o *CommandOutput) GetStderrAsStringOrEmptyIfUnset() (stderr string)

func (*CommandOutput) GetStdout

func (c *CommandOutput) GetStdout() (stdout *[]byte, err error)

func (*CommandOutput) GetStdoutAsBytes

func (o *CommandOutput) GetStdoutAsBytes() (stdout []byte, err error)

func (*CommandOutput) GetStdoutAsFloat64

func (c *CommandOutput) GetStdoutAsFloat64() (stdout float64, err error)

func (*CommandOutput) GetStdoutAsLines

func (o *CommandOutput) GetStdoutAsLines(removeLastLineIfEmpty bool) (stdoutLines []string, err error)

func (*CommandOutput) GetStdoutAsString

func (o *CommandOutput) GetStdoutAsString() (stdout string, err error)

func (*CommandOutput) IsExitSuccess

func (o *CommandOutput) IsExitSuccess() (isSuccess bool)

func (*CommandOutput) IsStderrEmpty

func (c *CommandOutput) IsStderrEmpty() (isEmpty bool, err error)

func (*CommandOutput) IsStdoutAndStderrEmpty

func (c *CommandOutput) IsStdoutAndStderrEmpty() (isEmpty bool, err error)

func (*CommandOutput) IsStdoutEmpty

func (c *CommandOutput) IsStdoutEmpty() (isEmpty bool, err error)

func (*CommandOutput) IsTimedOut

func (o *CommandOutput) IsTimedOut() (IsTimedOut bool, err error)

func (*CommandOutput) LogStdoutAsInfo

func (c *CommandOutput) LogStdoutAsInfo() (err error)

func (*CommandOutput) MustCheckExitSuccess

func (c *CommandOutput) MustCheckExitSuccess(verbose bool)

func (*CommandOutput) MustGetCmdRunError

func (c *CommandOutput) MustGetCmdRunError() (cmdRunError *error)

func (*CommandOutput) MustGetFirstLineOfStdoutAsString

func (c *CommandOutput) MustGetFirstLineOfStdoutAsString() (firstLine string)

func (*CommandOutput) MustGetReturnCode

func (c *CommandOutput) MustGetReturnCode() (returnCode int)

func (*CommandOutput) MustGetStderr

func (c *CommandOutput) MustGetStderr() (stderr *[]byte)

func (*CommandOutput) MustGetStderrAsString

func (c *CommandOutput) MustGetStderrAsString() (stdout string)

func (*CommandOutput) MustGetStdout

func (c *CommandOutput) MustGetStdout() (stdout *[]byte)

func (*CommandOutput) MustGetStdoutAsBytes

func (c *CommandOutput) MustGetStdoutAsBytes() (stdout []byte)

func (*CommandOutput) MustGetStdoutAsFloat64

func (c *CommandOutput) MustGetStdoutAsFloat64() (stdout float64)

func (*CommandOutput) MustGetStdoutAsLines

func (c *CommandOutput) MustGetStdoutAsLines(removeLastLineIfEmpty bool) (stdoutLines []string)

func (*CommandOutput) MustGetStdoutAsString

func (c *CommandOutput) MustGetStdoutAsString() (stdout string)

func (*CommandOutput) MustIsStderrEmpty

func (c *CommandOutput) MustIsStderrEmpty() (isEmpty bool)

func (*CommandOutput) MustIsStdoutAndStderrEmpty

func (c *CommandOutput) MustIsStdoutAndStderrEmpty() (isEmpty bool)

func (*CommandOutput) MustIsStdoutEmpty

func (c *CommandOutput) MustIsStdoutEmpty() (isEmpty bool)

func (*CommandOutput) MustIsTimedOut

func (c *CommandOutput) MustIsTimedOut() (IsTimedOut bool)

func (*CommandOutput) MustLogStdoutAsInfo

func (c *CommandOutput) MustLogStdoutAsInfo()

func (*CommandOutput) MustSetReturnCode

func (c *CommandOutput) MustSetReturnCode(returnCode int)

func (*CommandOutput) MustSetStderr

func (c *CommandOutput) MustSetStderr(stderr []byte)

func (*CommandOutput) MustSetStderrByString

func (c *CommandOutput) MustSetStderrByString(stderr string)

func (*CommandOutput) MustSetStdout

func (c *CommandOutput) MustSetStdout(stdout []byte)

func (*CommandOutput) MustSetStdoutByString

func (c *CommandOutput) MustSetStdoutByString(stdout string)

func (*CommandOutput) SetCmdRunError

func (o *CommandOutput) SetCmdRunError(err error)

func (*CommandOutput) SetReturnCode

func (o *CommandOutput) SetReturnCode(returnCode int) (err error)

func (*CommandOutput) SetStderr

func (o *CommandOutput) SetStderr(stderr []byte) (err error)

func (*CommandOutput) SetStderrByString

func (o *CommandOutput) SetStderrByString(stderr string) (err error)

func (*CommandOutput) SetStdout

func (o *CommandOutput) SetStdout(stdout []byte) (err error)

func (*CommandOutput) SetStdoutByString

func (o *CommandOutput) SetStdoutByString(stdout string) (err error)

type ContextKeyLiveOutputOnStdout added in v0.300.0

type ContextKeyLiveOutputOnStdout struct{}

type ExecService

type ExecService struct {
	CommandExecutorBase
}

func Exec

func Exec() (e *ExecService)

func NewExec

func NewExec() (e *ExecService)

func NewExecService

func NewExecService() (e *ExecService)

func (*ExecService) GetDeepCopy

func (e *ExecService) GetDeepCopy() (deepCopy CommandExecutor)

func (*ExecService) GetHostDescription

func (e *ExecService) GetHostDescription() (hostDescription string, err error)

func (*ExecService) RunCommand

func (e *ExecService) RunCommand(ctx context.Context, options *parameteroptions.RunCommandOptions) (commandOutput *CommandOutput, err error)

type PowerShellService added in v0.255.0

type PowerShellService struct {
	CommandExecutorBase
}

func NewPowerShell added in v0.255.0

func NewPowerShell() (p *PowerShellService)

func NewPowerShellService added in v0.255.0

func NewPowerShellService() (p *PowerShellService)

func PowerShell added in v0.255.0

func PowerShell() (p *PowerShellService)

func (*PowerShellService) RunCommand added in v0.255.0

func (b *PowerShellService) RunCommand(ctx context.Context, options *parameteroptions.RunCommandOptions) (commandOutput *CommandOutput, err error)

func (*PowerShellService) RunOneLiner added in v0.255.0

func (p *PowerShellService) RunOneLiner(ctx context.Context, oneLiner string) (output *CommandOutput, err error)

func (*PowerShellService) RunOneLinerAndGetStdoutAsString added in v0.255.0

func (p *PowerShellService) RunOneLinerAndGetStdoutAsString(ctx context.Context, oneLiner string) (stdout string, err error)

Jump to

Keyboard shortcuts

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