runcmd

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

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

Go to latest
Published: Jan 11, 2022 License: AGPL-3.0 Imports: 14 Imported by: 1

README

go-runcmd

Go

go-runcmd is a Go library and common interface for running local and remote commands providing the Runner interface which helps to abstract away running local and remote shell commands

Borrowed from kovetskiy/runcmd-

Install

go get github.com/aucloud/go-runcmd

Usage

First, import the library:

import "github.com/aucloud/go-runcmd"

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

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

Local Runner:

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

Remote Runner:

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

After that, create a command, and call .Run():

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

Both local and remote runners implements the 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)
  }
}

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()

License

go-runcmd is licensed under the terms of the AGPLv3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveHostname

func ResolveHostname(hostport string) (net.Addr, error)

Types

type CmdWorker

type CmdWorker interface {
	Run() ([]string, error)
	Start() error
	Wait() error
	StdinPipe() (io.WriteCloser, error)
	StdoutPipe() (io.Reader, error)
	StderrPipe() (io.Reader, error)
	SetStdout(buffer io.Writer)
	SetStderr(buffer io.Writer)
	GetCommandLine() string
}

type ExecError

type ExecError struct {
	ExecutionError error
	CommandLine    string
	Output         []string
}

func (ExecError) Error

func (err ExecError) Error() string

type Local

type Local struct{}

func NewLocalRunner

func NewLocalRunner() (*Local, error)

func (*Local) Command

func (runner *Local) Command(cmdline string) (CmdWorker, error)

type LocalCmd

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

func (*LocalCmd) GetCommandLine

func (cmd *LocalCmd) GetCommandLine() string

func (*LocalCmd) Run

func (cmd *LocalCmd) Run() ([]string, error)

func (*LocalCmd) SetStderr

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

func (*LocalCmd) SetStdout

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

func (*LocalCmd) Start

func (cmd *LocalCmd) Start() error

func (*LocalCmd) StderrPipe

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

func (*LocalCmd) StdinPipe

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

func (*LocalCmd) StdoutPipe

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

func (*LocalCmd) Wait

func (cmd *LocalCmd) Wait() error

type Remote

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

func NewRemoteKeyAuthRunner

func NewRemoteKeyAuthRunner(ctx context.Context, user, host, key string) (*Remote, error)

func NewRemoteKeyAuthRunnerViaJumphost

func NewRemoteKeyAuthRunnerViaJumphost(ctx context.Context, user, host, jumphost, key string) (*Remote, error)

func NewRemotePassAuthRunner

func NewRemotePassAuthRunner(ctx context.Context, user, host, password string) (*Remote, error)

func (*Remote) CloseConnection

func (runner *Remote) CloseConnection() error

func (*Remote) Command

func (runner *Remote) Command(cmdline string) (CmdWorker, error)

type RemoteCmd

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

func (*RemoteCmd) GetCommandLine

func (cmd *RemoteCmd) GetCommandLine() string

func (*RemoteCmd) Run

func (cmd *RemoteCmd) Run() ([]string, error)

func (*RemoteCmd) SetStderr

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

func (*RemoteCmd) SetStdout

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

func (*RemoteCmd) Start

func (cmd *RemoteCmd) Start() error

func (*RemoteCmd) StderrPipe

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

func (*RemoteCmd) StdinPipe

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

func (*RemoteCmd) StdoutPipe

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

func (*RemoteCmd) Wait

func (cmd *RemoteCmd) Wait() error

type Runner

type Runner interface {
	Command(cmd string) (CmdWorker, error)
}

Jump to

Keyboard shortcuts

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