shellexec

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 10 Imported by: 1

README

shellexec package

Provide a library for executing any program or script. It allow to retrieve stdout, stderr and error code. Default execution timeout is 5 seconds.

Usage

See cmd/cli/main.go

Documentation

Overview

shellexec provide functions for executing any program or script. It allow to retrieve stdout, stderr and error code.

Index

Constants

View Source
const (
	// ErrorCode values
	ErrorCodeUnknown    = ErrorCode(100)
	ErrorCodeReadStdout = ErrorCode(101)
	ErrorCodeReadStderr = ErrorCode(102)
	ErrorCodeTimeout    = ErrorCode(103)
	ErrorCodeKillFailed = ErrorCode(104)
)
View Source
const (
	// DefaultTimeout for exiting
	DefaultTimeout = 5 * time.Second
	MinimumTimeout = 100 * time.Millisecond
)

Variables

View Source
var (
	ErrInvalidProgram = errors.New("Invalid program")
	ErrTimeout        = errors.New("Timeout reached")
	ErrTimeoutKill    = errors.New("Timeout kill failed")
	ErrUnknown        = errors.New("Unknown error")
)

Functions

This section is empty.

Types

type ErrorCode

type ErrorCode int

ErrorCode type

func ExitCodeFromError

func ExitCodeFromError(err error) (code ErrorCode)

ExitCodeFromError return ErroCode from error

type Options

type Options struct {
	Timeout time.Duration // Timeout execution
	Program string        // program name
	Path    string        // program path (optional)
	Env     []string      // Program env (optional)
	Stdin   io.Reader     // Program Stdin (optional)
	Args    []string      // Program arguments (optional)
}

Options for Execute

func DefaultOptions

func DefaultOptions() Options

DefaultOptions return defaults

func (Options) WithArgs

func (option Options) WithArgs(args ...string) Options

WithArgs add args to options

func (Options) WithEnv

func (option Options) WithEnv(env ...string) Options

WithEnv add env to options

func (Options) WithPath

func (option Options) WithPath(path string) Options

WithPath add path to options

func (Options) WithProgram

func (option Options) WithProgram(program string) Options

WithProgram add program to options

func (Options) WithStdin

func (option Options) WithStdin(stdin io.Reader) Options

WithStdin add stdin to options

func (Options) WithTimeout

func (option Options) WithTimeout(timeout time.Duration) Options

WithTimeout add timeout to options

type Output

type Output struct {
	Stdout string
	Stderr string
	Code   ErrorCode
}

Output contains program outputs and ErrorCode

func Execute

func Execute(ctx context.Context, options Options) (Output, error)

Execute execute program in options. Returns standard outputs or error in Result Kill program execution if last more than timeout option

Directories

Path Synopsis
cmd
cli

Jump to

Keyboard shortcuts

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