runcmd

package
v0.0.0-...-2b2ca87 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2018 License: MIT Imports: 15 Imported by: 0

README

runcmd

runcmd golang package helps you run shell commands on local or remote hosts

http://godoc.org/github.com/theairkit/runcmd

Installation:

go get github.com/theairkit/runcmd

Description and examples:

First, create runner: this is a type, that holds:

  • for local commands: empty struct
  • for remote commands: connect to remote host; so, you can create only one remote runner to remote host

Note: there are no ability to set connection timeout in golang-ssh package

(track my request: https://codereview.appspot.com/158760043/)

lRunner, err := runcmd.NewLocalRunner()
if err != nil {
	//handle error
}

rRunner, err := runcmd.NewRemoteKeyAuthRunner(
			"user",
			"127.0.0.1:22",
			"/home/user/id_rsa",
			)
if err != nil {
	//handle error
}

rRunner, err := runcmd.NewRemotePassAuthRunner(
			"user",
			"127.0.0.1:22",
			"userpass",
			)
if err != nil {
	//handle error
}

After that, create command, and run methods:

c, err := rRunner.Command("date")
if err != nil {
	//handle error
}
out, err := c.Run()
if err != nil {
	//handle error
}

Both local and remote runners implements Runner interface, so, you can work with them as Runner:

func listSomeDir(r Runner) error {
	c, err := r.Command("ls -la")
	if err != nil {
		//handle error
	}
	out, err := c.Run()
	if err != nil {
		//handle error
	}
	for _, i := range out {
		fmt.Println(i)
	}
}

// List some dir on local host:
if err := listSomeDir(lRunner); err != nil {
	//handle error
}

// List some dir on remote host:
if err := listSomeDir(rRunner); err != nil {
	//handle error
}

Another useful code snippet: pipe from local to remote command:

lRunner, err := NewLocalRunner()
if err != nil {
	//handle error
}

rRunner, err := NewRemoteKeyAuthRunner(user, host, key)
if err != nil {
	//handle error
}

cLocal, err := lRunner.Command("date")
if err != nil {
	//handle error
}
if err = cmdLocal.Start(); err != nil {
	//handle error
}
cRemote, err := rRunner.Command("tee /tmp/tmpfile")
if err != nil {
	//handle error
}
if err = cRemote.Start(); err != nil {
	//handle error
}
if _, err = io.Copy(cRemote.StdinPipe(),cLocal.StdoutPipe(),); err != nil {
	//handle error
}

// Correct handle end of copying:
cmdLocal.Wait()
cmdRemote.StdinPipe().Close()
cmdRemote.Wait()

For other examples see runcmd_test.go Before running 'go test', change next variables in runcmd_test.go:

	//Change it before running the tests:
	user = "user"
	host = "127.0.0.1:22"
	key  = "/home/user/.ssh/id_rsa"
	pass = "somepass"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdWorker

type CmdWorker interface {
	Run() error
	Output() ([]byte, []byte, error)
	Start() error
	Wait() error
	StdinPipe() (io.WriteCloser, error)
	StdoutPipe() (io.Reader, error)
	StderrPipe() (io.Reader, error)
	SetStdout(io.Writer)
	SetStderr(io.Writer)
	SetStdin(io.Reader)
	GetArgs() []string
	CmdError() error
}

CmdWorker executes commands.

type ExecError

type ExecError struct {
	ExecutionError error
	Args           []string
	Output         []byte
}

ExecError represents error messages occured while executing command.

func (ExecError) Error

func (err ExecError) Error() string

type Local

type Local struct{}

Local is implementation of Runner interface for local commands

func NewLocalRunner

func NewLocalRunner() (*Local, error)

NewLocalRunner creates instance of local runner

func (*Local) Command

func (local *Local) Command(name string, arg ...string) CmdWorker

Command creates worker for current command execution

type LocalCmd

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

LocalCmd is implementation of CmdWorker interface for local commands

func (*LocalCmd) CmdError

func (local *LocalCmd) CmdError() error

stub to suite interface

func (*LocalCmd) GetArgs

func (cmd *LocalCmd) GetArgs() []string

GetArgs returns cmdline for current worker

func (*LocalCmd) Output

func (cmd *LocalCmd) Output() ([]byte, []byte, error)

func (*LocalCmd) Run

func (cmd *LocalCmd) Run() error

Run executes current command and retuns output splitted by newline

func (*LocalCmd) SetStderr

func (cmd *LocalCmd) SetStderr(buffer io.Writer)

SetStderr is for binding your own writer to worker stderr

func (*LocalCmd) SetStdin

func (cmd *LocalCmd) SetStdin(reader io.Reader)

func (*LocalCmd) SetStdout

func (cmd *LocalCmd) SetStdout(buffer io.Writer)

SetStdout is for binding your own writer to worker stdout

func (*LocalCmd) Start

func (cmd *LocalCmd) Start() error

Start begins current command execution

func (*LocalCmd) StderrPipe

func (cmd *LocalCmd) StderrPipe() (io.Reader, error)

StderrPipe returns stderr of current worker

func (*LocalCmd) StdinPipe

func (cmd *LocalCmd) StdinPipe() (io.WriteCloser, error)

StdinPipe returns stdin of current worker

func (*LocalCmd) StdoutPipe

func (cmd *LocalCmd) StdoutPipe() (io.Reader, error)

StdoutPipe returns stdout of current worker

func (*LocalCmd) Wait

func (cmd *LocalCmd) Wait() error

Wait returns error after command execution if current command return nonzero exit code

type MockRunner

type MockRunner struct {
	// Stdout is a bytes slice which will be returned as program's mock stdout.
	Stdout []byte

	// Stdout is a bytes slice which will be returned as program's mock stderr.
	Stderr []byte

	// Error is a error which will be used to fail program's run.
	Error error

	// OnCommand is a callback which will be called on each Command() call with
	// worker which will be similar to exec.Cmd.
	OnCommand func(*MockRunnerWorker)
}

MockRunner represents runner which is suitable for writing tests for code that uses runcmd library.

func (MockRunner) Command

func (runner MockRunner) Command(name string, args ...string) CmdWorker

Command returns standard CmdWorker with streams setup to return mocked stdout and stderr.

type MockRunnerWorker

type MockRunnerWorker struct {
	MockRunner

	Args []string
	// contains filtered or unexported fields
}

func (*MockRunnerWorker) CmdError

func (worker *MockRunnerWorker) CmdError() error

func (*MockRunnerWorker) GetArgs

func (worker *MockRunnerWorker) GetArgs() []string

GetArgs returns original command line arguments.

func (*MockRunnerWorker) Output

func (worker *MockRunnerWorker) Output() ([]byte, []byte, error)

Output returns mocked stdout, stderr and execution error.

func (*MockRunnerWorker) Run

func (worker *MockRunnerWorker) Run() error

Run runs Start() and then Wait().

func (*MockRunnerWorker) SetStderr

func (worker *MockRunnerWorker) SetStderr(writer io.Writer)

SetStderr sets writer which will be used to write mocked stderr.

func (*MockRunnerWorker) SetStdin

func (worker *MockRunnerWorker) SetStdin(reader io.Reader)

SetStdin sets reader which will be fully read on Start() or Run() till Wait().

func (*MockRunnerWorker) SetStdout

func (worker *MockRunnerWorker) SetStdout(writer io.Writer)

SetStdout sets writer which will be used to write mocked stdout.

func (*MockRunnerWorker) Start

func (worker *MockRunnerWorker) Start() error

Start will read all incoming stdin (if any), write stdout and stderr to specified output streams (if any is set up with SetStdout() or SetStderr() methods) and then returns error if any.

func (*MockRunnerWorker) StderrPipe

func (worker *MockRunnerWorker) StderrPipe() (io.Reader, error)

StderrPipe returns reader with mocked stderr.

func (*MockRunnerWorker) StdinPipe

func (worker *MockRunnerWorker) StdinPipe() (io.WriteCloser, error)

StdinPipe returns no-op writer.

func (*MockRunnerWorker) StdoutPipe

func (worker *MockRunnerWorker) StdoutPipe() (io.Reader, error)

StdoutPipe returns reader with mocked stdout.

func (*MockRunnerWorker) Wait

func (worker *MockRunnerWorker) Wait() error

Wait waits stdin, stdout and stderr streams are processed and returns error if any.

type Remote

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

Remote is implementation of Runner interface for remote commands

func NewRemoteAgentAuthRunner

func NewRemoteAgentAuthRunner(user, host, agent string) (*Remote, error)

NewRemoteAgenntAuthRunner is one of functions for creating remote runner

func NewRemoteAgentAuthRunnerWithTimeouts

func NewRemoteAgentAuthRunnerWithTimeouts(user, host, agent string, timeouts Timeouts) (*Remote, error)

NewRemoteAgentAuthRunnerWithTimeouts is one of functions for creating remote runner Use this one instead of NewRemoteAgentAuthRunner if you need to setup nondefault timeouts for ssh connection

func NewRemoteKeyAuthRunner

func NewRemoteKeyAuthRunner(user, host, key string) (*Remote, error)

NewRemoteKeyAuthRunner is one of functions for creating remote runner

func NewRemoteKeyAuthRunnerWithTimeouts

func NewRemoteKeyAuthRunnerWithTimeouts(
	user, host, key string, timeouts Timeouts,
) (*Remote, error)

NewRemoteKeyAuthRunnerWithTimeouts is one of functions for creating remote runner. Use this one instead of NewRemoteKeyAuthRunner if you need to setup nondefault timeouts for ssh connection

func NewRemotePassAuthRunner

func NewRemotePassAuthRunner(user, host, password string) (*Remote, error)

NewRemotePassAuthRunner is one of functions for creating remote runner

func NewRemotePassAuthRunnerWithTimeouts

func NewRemotePassAuthRunnerWithTimeouts(
	user, host, password string, timeouts Timeouts,
) (*Remote, error)

NewRemotePassAuthRunnerWithTimeouts is one of functions for creating remote runner. Use this one instead of NewRemotePassAuthRunner if you need to setup nondefault timeouts for ssh connection

func NewRemoteRawKeyAuthRunnerWithTimeouts

func NewRemoteRawKeyAuthRunnerWithTimeouts(
	user, host, key string, timeouts Timeouts,
) (*Remote, error)

NewRemoteRawKeyAuthRunnerWithTimeouts is same, as NewRemoteKeyAuthRunnerWithTimeouts, but key should be raw byte sequence instead of path.

func (*Remote) CloseConnection

func (remote *Remote) CloseConnection() error

CloseConnection is method for closing ssh connection of current runner

func (*Remote) Command

func (remote *Remote) Command(name string, arg ...string) CmdWorker

Command creates worker for current command execution

type RemoteCmd

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

RemoteCmd is implementation of CmdWorker interface for remote commands

func (*RemoteCmd) CmdError

func (remote *RemoteCmd) CmdError() error

func (*RemoteCmd) GetArgs

func (cmd *RemoteCmd) GetArgs() []string

GetArgs returns cmdline for current worker

func (*RemoteCmd) Output

func (cmd *RemoteCmd) Output() ([]byte, []byte, error)

func (*RemoteCmd) Run

func (cmd *RemoteCmd) Run() error

Run executes current command

func (*RemoteCmd) SetStderr

func (cmd *RemoteCmd) SetStderr(buffer io.Writer)

SetStderr is method for binding your own writer to worker stderr

func (*RemoteCmd) SetStdin

func (cmd *RemoteCmd) SetStdin(buffer io.Reader)

func (*RemoteCmd) SetStdout

func (cmd *RemoteCmd) SetStdout(buffer io.Writer)

SetStdout is method for binding your own writer to worker stdout

func (*RemoteCmd) Start

func (cmd *RemoteCmd) Start() error

Start begins current command execution

func (*RemoteCmd) StderrPipe

func (cmd *RemoteCmd) StderrPipe() (io.Reader, error)

StderrPipe returns stderr of current worker

func (*RemoteCmd) StdinPipe

func (cmd *RemoteCmd) StdinPipe() (io.WriteCloser, error)

StdinPipe returns stdin of current worker

func (*RemoteCmd) StdoutPipe

func (cmd *RemoteCmd) StdoutPipe() (io.Reader, error)

StdoutPipe returns stdout of current worker

func (*RemoteCmd) Wait

func (cmd *RemoteCmd) Wait() (err error)

Wait returns error after command execution if current command return nonzero exit code

type Runner

type Runner interface {
	Command(name string, arg ...string) CmdWorker
}

Runner creates command workers.

type Timeouts

type Timeouts struct {
	ConnectionTimeout time.Duration
	SendTimeout       time.Duration
	ReceiveTimeout    time.Duration
	KeepAlive         time.Duration
}

Timeouts is struct for setting various timeouts for ssh connection

Jump to

Keyboard shortcuts

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