command

package module
v0.0.0-...-218fc0e Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2015 License: MIT Imports: 8 Imported by: 6

README

go-command Build Status GoDoc

Yet another wrapper of os/exec

Installation

The recommended way to install go-command

go get github.com/mcuadros/go-command

Examples

How import the package

import (
    "github.com/mcuadros/go-command"
)

Basic example:

cmd := NewCommand("./test", "-exit=0", "-time=1", "-max=1")
if err := cmd.Run(); err != nil {
    panic(err)
}

if err := cmd.Wait(); err != nil {
    panic(err)
}

response := cmd.GetResponse()
fmt.Printf("Failed: %v, ExitCode: %d, Elapsed: %s",
    response.Failed,
    response.ExitCode,
    response.RealTime,
)

//Return: Failed: false, ExitCode: 0, RealTime: 1.004233166s

License

MIT, see LICENSE

Documentation

Overview

Yet another wrapper of os/exec with an extended execution report

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Stdout *bytes.Buffer
	Stderr *bytes.Buffer
	Pid    int
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(command string) *Command

NewCommand returns the Command struct to execute the named program with the given arguments.

func (*Command) GetResponse

func (c *Command) GetResponse() *ExecutionResponse

GetResponse returns a ExecutionResponse struct, must be called at the end of the execution

func (*Command) Kill

func (c *Command) Kill() error

Kill causes the Command to exit immediately.

func (*Command) Run

func (c *Command) Run() error

Run starts the specified command and waits for it to complete.

The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.

func (*Command) SetEnvironment

func (c *Command) SetEnvironment(environment []string)

SetEnvironment sets the environment of the process. If not is set, Run uses the current process's environment.

func (*Command) SetTimeout

func (c *Command) SetTimeout(timeout time.Duration)

SetTimeout configure the limit amount of time to run

func (*Command) SetUser

func (c *Command) SetUser(username string)

SetUser execute a command as this user (need sudo privilegies)

func (*Command) SetWorkingDir

func (c *Command) SetWorkingDir(workingDir string)

SetWorkingDir sets the working directory of the command. If not is set, Run runs the command in the calling process's current directory.

func (*Command) Wait

func (c *Command) Wait() error

Wait waits for the Command to exit.

type ExecutionResponse

type ExecutionResponse struct {
	RealTime time.Duration
	UserTime time.Duration
	SysTime  time.Duration
	Stdout   *bytes.Buffer
	Stderr   *bytes.Buffer
	Pid      int
	ExitCode int
	Failed   bool
	User     string
	Rusage   *syscall.Rusage
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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