cmd

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2019 License: MIT Imports: 9 Imported by: 0

README

Build Status GoDoc Test Coverage Maintainability Go Report Card

cmd package

A simple package to execute shell commands on linux, darwin and windows.

Installation

$ go get -u github.com/SimonBaeumer/cmd@v1.0.0

Usage

cmd := NewCommand("echo hello")

err := cmd.Execute()
if err != nil {
    panic(err.Error())    
}

fmt.Println(cmd.Stdout())
fmt.Println(cmd.Stderr())
Stream output to stderr and stdout
cmd := NewCommand("echo hello", cmd.WithStandardStreams)
cmd.Execute()
Set custom options
func SetTimeout(c *Command) {
    c.Timeout = 1 * time.Hour
}

func SetWorkingDir(c *Command) {
    c.WorkingDir = "/tmp/test"
}

cmd := NewCommand("pwd", SetTimeout, SetWorkingDir)
cmd.Execute()

Development

Running tests
make test
ToDo
  • os.Stdout and os.Stderr output access after execution

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithStandardStreams

func WithStandardStreams(c *Command)

WithStandardStreams creates a command which steams its output to the stderr and stdout streams of the operating system

Types

type Command

type Command struct {
	Command      string
	Env          []string
	Dir          string
	Timeout      time.Duration
	StderrWriter io.Writer
	StdoutWriter io.Writer
	WorkingDir   string
	// contains filtered or unexported fields
}

Command represents a single command which can be executed

func NewCommand

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

NewCommand creates a new command You can add option with variadic option argument

Example:

     c := cmd.NewCommand("echo hello", function (c *Command) {
		    c.WorkingDir = "/tmp"
     })
     c.Execute()

or you can use existing options functions

c := cmd.NewCommand("echo hello", cmd.WithStandardStreams)
c.Execute()

func (*Command) AddEnv

func (c *Command) AddEnv(key string, value string)

AddEnv adds an environment variable to the command If a variable gets passed like ${VAR_NAME} the env variable will be read out by the current shell

func (*Command) Execute

func (c *Command) Execute() error

Execute executes the command and writes the results into it's own instance The results can be received with the Stdout(), Stderr() and ExitCode() methods

func (*Command) Executed

func (c *Command) Executed() bool

Executed returns if the command was already executed

func (*Command) ExitCode

func (c *Command) ExitCode() int

ExitCode returns the exit code of the command

func (*Command) SetTimeout

func (c *Command) SetTimeout(timeout string) error

SetTimeout sets the timeout given a time unit Example: SetTimeout("100s") sets the timeout to 100 seconds

func (*Command) SetTimeoutMS

func (c *Command) SetTimeoutMS(ms int)

SetTimeoutMS sets the timeout in milliseconds

func (*Command) Stderr

func (c *Command) Stderr() string

Stderr returns the output to stderr

func (*Command) Stdout

func (c *Command) Stdout() string

Stdout returns the output to stdout

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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