execute

package module
v0.0.0-...-a4bd850 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2017 License: GPL-3.0 Imports: 17 Imported by: 6

README

execute

This is a convenience wrapper around the standard GO exec package.

It provides a somewhat elegant way to access stdout and stderr and feed data to stdin, without bothering the programmer with pipes and I/O.

There's little doubt that it can be optimized. It works fine, however, so there's little incentive to change anything for the better right now.

Usage

go get github.com/ms-xy/execute
import (
  "github.com/ms-xy/execute"
  "github.com/ms-xy/logtools"
)

func main() {
  logtools.Initialize()

  cmd := &execute.Command{
    LookupPath: true,
    WorkingDir: ".",

    Executable: "bash",
    Arguments:  []string{"-c", '"echo \"hello world\""'},
    Input:      []byte{},

    RlimitArgs: []string{}, // see github.com/ms-xy/rlimiter
    Timeout:    10*time.Second,
    StdoutSize: 10000, // size in bytes
    StderrSize: 10000, // size in bytes
  }

  if result, err := execute.Execute(cmd); err != nil {
    logtools.Errorf("Error happened while executing command: %+v", err)
  } else {
    logtools.Infof("Stdout of command: %s", string(result.Stdout))
  }
}

struct Command

type Command struct {
  LookupPath bool
  Executable string
  WorkingDir string
  Input      []byte
  RlimitArgs []string
  Arguments  []string
  Timeout    time.Duration
  StdoutSize int
  StderrSize int
}

struct ExecResult

type ExecResult struct {
  Stdout     []byte
  Stderr     []byte
  ExitCode   int
  Error      string
  ModTime    time.Time
  Killed     bool
  KillReason string
}

License

GNU GPLv3. Please see the attached License.txt file for details. Different license terms can be arranged on request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ERR_WORKDIR_MISSING  = errors.New("working directory is missing")
	ERR_WORKDIR_ISFILE   = errors.New("working directory must be a directory")
	ERR_EXECUTABLE_ISDIR = errors.New("executable must be a file")
	ERR_NOTEXECUTABLE    = errors.New("file is not executable")
)
View Source
var (
	UnknownIOError = IOError{"Unknown Error", nil}
	EOSTDOUT       = errors.New("stdout overflow")
	EOSTDERR       = errors.New("stderr overflow")
	ETIMEOUT       = errors.New("timeout reached")
)

Functions

func GetExitCode

func GetExitCode(err error) int

Helper function to retrieve the exit code

func IsIOError

func IsIOError(err error) bool

Types

type Command

type Command struct {
	LookupPath bool
	Executable string
	WorkingDir string
	Input      []byte
	RlimitArgs []string
	Arguments  []string
	Timeout    time.Duration
	StdoutSize int
	StderrSize int
}

type ExecResult

type ExecResult struct {
	Stdout     []byte
	Stderr     []byte
	ExitCode   int
	Error      string
	ModTime    time.Time
	Killed     bool
	KillReason string
}

convenience structure for return values directly used as parts for the db-models of RunResult and GccResult

func Execute

func Execute(command *Command) (*ExecResult, error)

type IOError

type IOError struct {
	Msg         string `json:"msg"`
	OriginalErr error
}

Helper functions to deal with program execution execute returns as following: stdout, stderr, error Where error may be of type IOError with potentially embedded errors that are the original errors (like pipe / fd IO errors). Other error types may be applicable (like *exec.ExitError) if cmd.Start or cmd.Wait() fail. Also at most one error is returned, though multiple may be possible at the same time. (e.g. IO errors on multiple pipes simultaneously)

func NewIOError

func NewIOError(msg string, original error) IOError

func (IOError) Error

func (err IOError) Error() string

Jump to

Keyboard shortcuts

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