script

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: Apache-2.0 Imports: 9 Imported by: 2

README

Go package to facilitate running external programs almost as easily as from a shell.

Example:

	s := script.Script{Trace: true}

	s.Run("ls", "-s")

	s.Cmd("ls").ToFile("ls.out")

	s.Cmd("cat").InputString("hello").Run()

	s.Cmd("cat").InputBytes([]byte("bytes")).Run()

	s.Cmd("cat").InputFile("ls.out").Run()

	pwd := s.Cmd("pwd").ToString()

	s.Cmd("ls", "-1").PipeTo("sort", "-r").Run()
  
	return s.Error()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToLines

func BytesToLines(data []byte) []string

func IterateLines

func IterateLines(data []byte, f func(string) error) error

func Run

func Run(cmd ...*exec.Cmd) error
  • Run 0 or more commands. If there are more than 1 command, pipe Stdout of each command to Stdin of the next command. Any unset Stderr is set to os.Stderr Stdout of the last command is set to os.Stdout, unless it is already set If the global Trace is true, print the commands before running them.

Types

type Cmd

type Cmd struct {
	Commands []*exec.Cmd
	Script   *Script
	// contains filtered or unexported fields
}

Cmd wraps 0 or more exec.Cmd and adds convenience methods for redirecting stdin, stdout in various ways.

func (*Cmd) Add

func (t *Cmd) Add(cmd *exec.Cmd) *Cmd

Add - add an os/exec.Cmd to the command pipeline

func (*Cmd) CombineOutput

func (t *Cmd) CombineOutput() *Cmd

CombineOutput redirects stderr to stdout. If the output is returned or redirected to a file, it includes both stdout and stderr

func (*Cmd) Dir

func (t *Cmd) Dir(dir string) *Cmd

Dir sets Cmd.Dir for all commands so far.

func (*Cmd) ErrorTo

func (t *Cmd) ErrorTo(w io.Writer) *Cmd

ErrorTo sets Cmd.Stderr to the given stream

func (*Cmd) InputBytes

func (t *Cmd) InputBytes(bytes []byte) *Cmd

InputBytes sets Cmd.Stdin to the given []byte input

func (*Cmd) InputFile

func (t *Cmd) InputFile(file string) *Cmd

InputFile sets Cmd.Stdin to the given file

func (*Cmd) InputString

func (t *Cmd) InputString(text string) *Cmd

InputString sets Cmd.Stdin to the given string input

func (*Cmd) PipeTo

func (t *Cmd) PipeTo(name string, arg ...string) *Cmd

PipeTo - shortcut to Add(exec.Command(name, arg...))

func (*Cmd) Run

func (t *Cmd) Run()

Run runs and/or prints the command, applying any specified redirections

func (*Cmd) ToBytes

func (t *Cmd) ToBytes() []byte

ToBytes runs the command and returns its stdout as a []byte

func (*Cmd) ToFile

func (t *Cmd) ToFile(file string)

ToFile redirects the output to a file, runs the command, and closes the file

func (*Cmd) ToLines

func (t *Cmd) ToLines() []string

ToLines runs the command, splits its output into lines, and returns the lines

func (*Cmd) ToNull

func (t *Cmd) ToNull()

ToNull runs the command and discards its output

func (*Cmd) ToString

func (t *Cmd) ToString() string

ToString runs the command and returns its output as a string

func (*Cmd) ToWriter

func (t *Cmd) ToWriter(out io.Writer)

ToWriter redirects the output to an io.Writer and runs the command

type ErrorHandler

type ErrorHandler interface {
	// Add an error.  Do nothing if err is nil.
	Add(err error)
	// Return true if processing should continue.  Should return true if there were no errors.
	// May also return true if there were prior errors, but processing should continue in order to check for more errors.
	Continue() bool
}

ErrorHandler allows a function to report errors, without returning them directly It is used for convenience in order to minimize checking error return values from function calls

type Errors

type Errors struct {
	// The list of non-nil errors that have been passed to Add()
	List []error
	// If AlwaysContinue is true, Continue() always return true.
	// In that case, examine List, or HasErrors() to find out if there were errors
	AlwaysContinue bool
	// Writer is an optional io.Writer to write errors to.
	Writer io.Writer
}

Errors implements ErrorHandler by collecting errors in a list.

func (*Errors) Add

func (t *Errors) Add(err error)

func (*Errors) Clear

func (t *Errors) Clear()

func (*Errors) Continue

func (t *Errors) Continue() bool

func (*Errors) Error

func (t *Errors) Error() error

Return nil if there are no errors. Otherwise, return the first error, or a generic error.

func (*Errors) First

func (t *Errors) First() error

Return the first error

func (*Errors) HasError

func (t *Errors) HasError() bool

type Script

type Script struct {
	Trace  bool // print exec arguments to stdout
	DryRun bool

	Errors Errors
	// contains filtered or unexported fields
}

func (*Script) AddError

func (t *Script) AddError(err error)
  • Check if the argument is an error, or whether the script already has an error. If the argument is an error and it is the first error encountered, it becomes the script's error. Return true if the script has an error, either the given error or a previous one.

func (*Script) Cmd

func (t *Script) Cmd(name string, args ...string) *Cmd

Cmd creates a Pipeline with one command.

func (*Script) Error

func (t *Script) Error() error

func (*Script) HasError

func (t *Script) HasError() bool

* Return true if an error has happened

func (*Script) LogWriter

func (t *Script) LogWriter() io.Writer

func (*Script) Pipeline

func (t *Script) Pipeline() *Cmd

Pipeline creates an empty pipeline

func (*Script) Run

func (t *Script) Run(name string, args ...string)

* Create a command and run it or print it

func (*Script) RunCmd

func (t *Script) RunCmd(cmd ...*exec.Cmd)

* Run or print zero or more commands

func (*Script) SetLogWriter

func (t *Script) SetLogWriter(w io.Writer)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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