Documentation ¶
Overview ¶
Package someutils offers some CLI utilities for the commandline and also as Go objects
Attention ¶
This package is in an alpha stage, and still in heavy development. APIs may change, and things may break.
See http://github.com/laher/someutils for details.
Introduction
Index ¶
- Constants
- Variables
- func Call(name string, args []string) (error, int)
- func CallUtil(util CliPipable, args []string, invocation *Invocation) (error, int)
- func CliExists(name string) bool
- func Exists(name string) bool
- func FanoutByLineStrategy(fanout *SomeFanout, invocation *Invocation) (chan *PipelineInvocation, chan error)
- func LineProcessor(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer, fu LineProcessorFunc) error
- func List() []string
- func PipableExists(name string) bool
- func Register(u CliPipable)
- func RegisterPipable(somefunc CliPipableFactory)
- func RegisterSimple(somefunc CliPipableSimpleFactory)
- func StdInvoke(pcu CliPipable, call []string) (error, int)
- type ArchiveItem
- type CliPipable
- type CliPipableFactory
- type CliPipableSimple
- type CliPipableSimpleFactory
- type CliPipableSimpleWrapper
- type Cliable
- type FanoutStrategy
- type Invocation
- func AwaitAllErrors(e chan *Invocation, count int) (bool, []*Invocation)
- func AwaitAllErrorsFor(e chan *Invocation, count int, timeout time.Duration) (bool, []*Invocation)
- func InvocationFromReader(inPipe io.Reader) (*Invocation, *bytes.Buffer, *bytes.Buffer)
- func NewErrorState(err error) *Invocation
- func NewInvocation(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer) *Invocation
- func StdInvocation() *Invocation
- func Wait(e chan *Invocation, count int) *Invocation
- func WaitFor(e chan *Invocation, count int, timeout time.Duration) *Invocation
- type LineProcessorFunc
- type Named
- type NamedPipable
- type NamedPipableSimple
- type NamedPipableSimpleFactory
- type NamedPipableSimpleWrapper
- type Pipable
- type PipableFactory
- type PipableSimple
- type PipableSimpleFactory
- type PipableSimpleWrapper
- type PipableWrapper
- type Pipe
- type PipeRedirector
- type Pipeline
- func (p *Pipeline) ExecAndWait(invocation *PipelineInvocation) *Invocation
- func (p *Pipeline) ExecAndWaitUpTo(invocation *PipelineInvocation, timeout time.Duration) *Invocation
- func (p *Pipeline) Invoke(mainInvocation *PipelineInvocation) (chan *Invocation, int)
- func (p *Pipeline) InvokeReader(inPipe io.Reader) (*PipelineInvocation, *bytes.Buffer, *bytes.Buffer)
- type PipelineInvocation
- type Signal
- type SignalSimple
- type SomeCd
- type SomeFanout
- type SomeLn
Examples ¶
Constants ¶
const EXIT_OK = 0
const (
VERSION = "0.5.1-snapshot"
)
Variables ¶
var (
SIGINT = &SignalSimple{1}
)
Functions ¶
func CallUtil ¶
func CallUtil(util CliPipable, args []string, invocation *Invocation) (error, int)
func FanoutByLineStrategy ¶
func FanoutByLineStrategy(fanout *SomeFanout, invocation *Invocation) (chan *PipelineInvocation, chan error)
func LineProcessor ¶
func LineProcessor(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer, fu LineProcessorFunc) error
Process line-by-line
func PipableExists ¶
func RegisterPipable ¶
func RegisterPipable(somefunc CliPipableFactory)
func RegisterSimple ¶
func RegisterSimple(somefunc CliPipableSimpleFactory)
Types ¶
type ArchiveItem ¶
type ArchiveItem struct { //if FileSystemPath is empty, use Data instead FileSystemPath string ArchivePath string Data []byte }
ArchiveItem is used by tar & zip
type CliPipable ¶
type CliPipable interface { NamedPipable Cliable }
PipableUtil represents a util which can be initialized by flags & executed on a Pipeline
func WrapCliPipable ¶
func WrapCliPipable(ps CliPipableSimple) CliPipable
type CliPipableFactory ¶
type CliPipableFactory func() CliPipable
type NamedPipableFactory func() NamedPipable
func GetCliPipableFactory ¶
func GetCliPipableFactory(name string) CliPipableFactory
type CliPipableSimple ¶
type CliPipableSimple interface { NamedPipableSimple Cliable }
type CliPipableSimpleFactory ¶
type CliPipableSimpleFactory func() CliPipableSimple
type CliPipableSimpleWrapper ¶
type CliPipableSimpleWrapper struct {
CliPipableSimple
}
func (*CliPipableSimpleWrapper) Invoke ¶
func (cpsw *CliPipableSimpleWrapper) Invoke(i *Invocation) (error, int)
type FanoutStrategy ¶
type FanoutStrategy func(*SomeFanout, *Invocation) (chan *PipelineInvocation, chan error)
type Invocation ¶
type Invocation struct { Pipeline *Pipeline Pipable Pipable MainPipe *Pipe ErrPipe *Pipe /* MainPipe.In io.Reader MainPipe.Out io.Writer ErrPipe.In io.Reader ErrPipe.Out io.Writer */ SignalReceiver chan Signal ExitCode *int Err error Closed bool // contains filtered or unexported fields }
A set of invocation (In, Out, ErrOut, and even ErrIn (but ErrIn is usually only used by the special 'Redirector' util) Note that Pipables are not expected to use this type (Pipables should not need any dependency on someutils - just the implicit implementation of the Pipable interface)
func AwaitAllErrors ¶
func AwaitAllErrors(e chan *Invocation, count int) (bool, []*Invocation)
Await all errors forever
func AwaitAllErrorsFor ¶
func AwaitAllErrorsFor(e chan *Invocation, count int, timeout time.Duration) (bool, []*Invocation)
Await Errors for a duration
func InvocationFromReader ¶
Factory taking a string for Stdin, and using byte buffers for the sdout and stderr invocation This returns the byte buffers to avoid the need to cast. (The assumption being that you'll want to call .Bytes() or .String() on those buffers)
func NewErrorState ¶
func NewErrorState(err error) *Invocation
func NewInvocation ¶
Factory for a pipeline with the given invocation
func StdInvocation ¶
func StdInvocation() *Invocation
Convenience method returns the Stdin/Stdout/Stderr invocation associated with this process
func WaitFor ¶
func WaitFor(e chan *Invocation, count int, timeout time.Duration) *Invocation
Await completion or error, for a duration
func (*Invocation) AutoHandleSignals ¶
func (i *Invocation) AutoHandleSignals()
func (i *Invocation) AutoPipeErrInOut() { go autoPipe(i.ErrPipe.Out, i.ErrPipe.In) }
TODO!!
func (*Invocation) Close ¶
func (i *Invocation) Close() error
func (i *Invocation) PipeToPipeline(pipeline *Pipeline) (error, chan *Invocation, int) { if i.Pipeline != nil { return errors.New("This invocation already was already invoked!"), nil, 0 } i.Pipeline = pipeline invocationChannel, count := pipeline.Invoke(i) return nil, invocationChannel, count }
warning. This closes a channel. Don't call it twice - panic ensues!
func (*Invocation) Wait ¶
func (i *Invocation) Wait() *int
type LineProcessorFunc ¶
A function which processses a line from a reader
type NamedPipable ¶
a Named Pipable can be registered for use by e.g. xargs
func WrapNamed ¶
func WrapNamed(ps NamedPipableSimple) NamedPipable
type NamedPipableSimple ¶
type NamedPipableSimple interface { PipableSimple Named }
type NamedPipableSimpleFactory ¶
type NamedPipableSimpleFactory func() NamedPipableSimple
type NamedPipableSimpleWrapper ¶
type NamedPipableSimpleWrapper struct {
NamedPipableSimple
}
func (*NamedPipableSimpleWrapper) Invoke ¶
func (npsw *NamedPipableSimpleWrapper) Invoke(i *Invocation) (error, int)
type Pipable ¶
type Pipable interface {
Invoke(i *Invocation) (error, int)
}
a Pipable can be executed on a pipeline
func Wrap ¶
func Wrap(ps PipableSimple) Pipable
type PipableFactory ¶
type PipableFactory func() Pipable
func GetPipableFactory ¶
func GetPipableFactory(name string) PipableFactory
type PipableSimple ¶
type PipableSimple interface {
Exec(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer) (error, int)
}
a PipableSimple can be executed on a pipeline when wrapped inside a PipableSimpleWrapper
type PipableSimpleFactory ¶
type PipableSimpleFactory func() PipableSimple
type PipableSimpleWrapper ¶
type PipableSimpleWrapper struct {
PipableSimple
}
func (*PipableSimpleWrapper) Invoke ¶
func (psw *PipableSimpleWrapper) Invoke(i *Invocation) (error, int)
type PipableWrapper ¶
type PipableWrapper struct {
PipableSimple
}
type Pipe ¶
func (*Pipe) CloseIfClosers ¶
func (p *Pipe) CloseIfClosers()
type PipeRedirector ¶
type PipeRedirector struct { Filename string // contains filtered or unexported fields }
PipeRedirector represents and performs a redirection between one Execable and another Note that PipeRedirector is an Execable but not a CLI util
func ErrTo ¶
func ErrTo(filename string) *PipeRedirector
Factory for redirecting 'err' pipe to a file
func ErrToNull ¶
func ErrToNull() *PipeRedirector
Factory for redirecting 'err' pipe to Null (nowhere)
func OutTo ¶
func OutTo(filename string) *PipeRedirector
Factory for redirecting 'out' pipe to a file
func OutToNull ¶
func OutToNull() *PipeRedirector
Factory for redirecting 'out' pipe to Null (nowhere)
func (*PipeRedirector) Invoke ¶
func (redirector *PipeRedirector) Invoke(invocation *Invocation) (error, int)
Exec actually performs the redirection
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Chains together the input/output of utils in a 'pipeline'
Example ¶
package main import ( "fmt" "io" "strings" ) type ExampleUtil struct { } func (ex *ExampleUtil) Exec(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer) (error, int) { err := LineProcessor(inPipe, outPipe, errPipe, func(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer, line []byte) error { _, err := fmt.Fprintln(outPipe, string(line)) return err }) if err != nil { return err, 1 } return nil, 0 } func main() { p := NewPipeline(&PipableSimpleWrapper{&ExampleUtil{}}, &PipableSimpleWrapper{&ExampleUtil{}}) mainInvocation, out, err := p.InvokeReader(strings.NewReader("Hi\nHo\nhI\nhO\n")) //, os.Stdout, os.Stderr) //e, _ := p.Invoke(mainInvocation) //<-e //<-e mainInvocation.Wait() fmt.Println(out.String()) fmt.Println(err.String()) }
Output: Hi Ho hI hO
func NewPipeline ¶
func (*Pipeline) ExecAndWait ¶
func (p *Pipeline) ExecAndWait(invocation *PipelineInvocation) *Invocation
// Intended as a subtype for Pipable which can redirect the error output of the previous command. This is treated as a special case because commands do not typically have access to this.
type WillRedirectErrIn interface { SetErrIn(errMainPipe.In io.Reader) }
Pipe and wait for errors (up until a timeout occurs)
func (*Pipeline) ExecAndWaitUpTo ¶
func (p *Pipeline) ExecAndWaitUpTo(invocation *PipelineInvocation, timeout time.Duration) *Invocation
Pipe and wait for errors (up until a timeout occurs)
func (*Pipeline) Invoke ¶
func (p *Pipeline) Invoke(mainInvocation *PipelineInvocation) (chan *Invocation, int)
Run pipables in a sequence, weaving together their inputs and outputs appropriately
func (*Pipeline) InvokeReader ¶
type PipelineInvocation ¶
type PipelineInvocation struct { *Invocation Invocations []*Invocation }
func NewPipelineInvocation ¶
func NewPipelineInvocation(invocation *Invocation) *PipelineInvocation
func (*PipelineInvocation) Add ¶
func (pi *PipelineInvocation) Add(invocation *Invocation)
func (*PipelineInvocation) SignalAll ¶
func (pi *PipelineInvocation) SignalAll(signal Signal)
func (*PipelineInvocation) Wait ¶
func (pi *PipelineInvocation) Wait() *Invocation
Wait until an exit status has occurred
func (*PipelineInvocation) WaitUpTo ¶
func (pi *PipelineInvocation) WaitUpTo(timeout time.Duration) *Invocation
Wait until an exit status has occurred
type SignalSimple ¶
type SignalSimple struct {
StatusCode int
}
func (*SignalSimple) Status ¶
func (ss *SignalSimple) Status() int
type SomeCd ¶
type SomeCd struct {
// contains filtered or unexported fields
}
SomeCd represents and performs a `cd` invocation
type SomeFanout ¶
type SomeFanout struct {
// contains filtered or unexported fields
}
SomeFanout represents and performs a `fanout` invocation
func (*SomeFanout) Invoke ¶
func (fanout *SomeFanout) Invoke(invocation *Invocation) (error, int)
Invoke actually performs the fanout
func (*SomeFanout) Name ¶
func (fanout *SomeFanout) Name() string
Name() returns the name of the util