executil

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2021 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package executil lets to call to system commands.

Index

Constants

This section is empty.

Variables

View Source
var DebugAsBash bool

DebugRunAsBash shows debug messages at functions related to 'RunAsBash()'.

View Source
var ErrProcKilled = errors.New("the process hasn't exited or was terminated by a signal")

ErrProcKilled reports an error by a process killed.

View Source
var ListWinShell = []WinShell{
	Cmd,
	Db2,
}

ListWinShell is the list of Windows shells.

Functions

func CheckStderr

func CheckStderr(stderr []byte, err error) error

CheckStderr returns an error whether 'stderr' is not empty or there is any error.

func RunAsBash

func RunAsBash(command string) (output []byte, err error)

RunAsBash executes external commands just like RunAsBashWithMatch, but does not return the boolean `match`.

func RunAsBashWithMatch

func RunAsBashWithMatch(command string) (output []byte, match bool, err error)

RunAsBashWithMatch executes external commands with access to shell features such as filename wildcards, shell pipes, environment variables, and expansion of the shortcut character "~" to home directory. It also logs the command.

This function avoids to have execute commands through a shell since an unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution.

The most of commands return a text in output or an error if any. `match` is used in commands like *grep*, *find*, or *cmp* to indicate if the search is matched.

func RunAsBashWithMatchf

func RunAsBashWithMatchf(format string, args ...interface{}) ([]byte, bool, error)

RunAsBashWithMatchf is like RunAsBashWithMatch, but formats its arguments according to the format. Analogous to Printf().

func RunAsBashf

func RunAsBashf(format string, args ...interface{}) ([]byte, error)

RunAsBashf is like RunAsBash, but formats its arguments according to the format. Analogous to Printf().

func RunWinshell

func RunWinshell(sh WinShell, closeWindow bool, cmd string) (out []byte, err error)

RunWinshell executes a command into a Windows' shell called from Powershell. Logs the command and returns the command output.

Types

type Command added in v1.3.0

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

Command represents a command to execute.

func NewCommand added in v1.3.0

func NewCommand(cmd string, args ...string) *Command

NewCommand sets the basic arguments to execute a command.

func (*Command) AddEnv added in v1.3.0

func (c *Command) AddEnv(e []string) *Command

AddEnv adds environment variables.

func (*Command) BadExitCodes added in v1.3.0

func (c *Command) BadExitCodes(codes []int) *Command

BadExitCodes sets the exit codes with errors for the command.

func (*Command) Command added in v1.3.0

func (c *Command) Command(cmd string, args ...string) *Command

Command sets the arguments to run other command.

func (*Command) Env added in v1.3.0

func (c *Command) Env(e []string) *Command

Env sets the environment variables.

func (*Command) ExitCode added in v1.3.0

func (c *Command) ExitCode() int

ExitCode returns the exit status code which is returned after of call to Run().

func (*Command) OkExitCodes added in v1.3.3

func (c *Command) OkExitCodes(codes []int) *Command

OkExitCodes sets the exit codes without errors for the command.

func (*Command) OutputCombined added in v1.3.0

func (c *Command) OutputCombined() (stdout, stderr []byte, err error)

OutputCombined runs the command and returns both standard output and error.

func (*Command) OutputStderr added in v1.3.0

func (c *Command) OutputStderr() (stderr []byte, err error)

OutputStderr runs the command and returns the standard error.

func (*Command) OutputStdout added in v1.3.0

func (c *Command) OutputStdout() (stdout []byte, err error)

OutputStdout runs the command and returns the standard output.

func (*Command) Run added in v1.3.0

func (c *Command) Run() (exitCode int, err error)

Run executes the command. Logs the command and the exit code.

func (*Command) StdCombinedTofile added in v1.3.0

func (c *Command) StdCombinedTofile(
	dir, filename string, fnCheckStderr func([]byte) error,
) error

StdCombinedTofile runs the command and saves both standard output and error into files. The full names are formed with the values of 'filename' plus "_stdout.log" and 'filename' plus "_stderr.log".

func (*Command) Stderr added in v1.3.0

func (c *Command) Stderr(err io.Writer) *Command

Stderr sets the standard error.

func (*Command) StderrTofile added in v1.3.0

func (c *Command) StderrTofile(dir, filename string, fnCheckStderr func([]byte) error) error

StderrTofile runs the command and saves the standard error into a file. The full name is formed with the value of 'filename' plus "_stderr.log". fnCheckStderr is a function to check the standard error.

func (*Command) Stdout added in v1.3.0

func (c *Command) Stdout(out io.Writer) *Command

Stdout sets the standard out.

func (*Command) StdoutTofile added in v1.3.0

func (c *Command) StdoutTofile(dir, filename string) error

StdoutTofile runs the command and saves the standard output into a file. The full name is formed with the value of 'filename' plus "_stdout.log".

func (*Command) TimeTokillCmd added in v1.3.0

func (c *Command) TimeTokillCmd(tm time.Duration) *Command

TimeTokillCmd sets the time of waiting to finish the command before of kill it.

type WinShell

type WinShell uint8

WinShell represents a Windows shell.

const (
	Cmd WinShell
	Db2
)

Windows shells.

func (WinShell) String

func (sh WinShell) String() string

Jump to

Keyboard shortcuts

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