dshl

package
v0.0.0-...-15d4781 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2019 License: ISC Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPS1 = NewPS1("~> ", "!> ")

The default value for Shell.PS1.

View Source
var (
	ErrorColor = color.New(color.FgRed, color.Bold)
)

Functions

func SplitWords

func SplitWords(line string) ([]string, error)

func ToInt64

func ToInt64(v interface{}) (int64, error)

func ToString

func ToString(v interface{}) string

func ToUInt64

func ToUInt64(v interface{}) (uint64, error)

func WithShell

func WithShell(ctx context.Context, sh *Shell) context.Context

Attaches a Shell to a context. This is done automatically in command handlers invoked from Shell.Run, Shell.Eval, Shell.Exec or Shell.Call.

Types

type Args

type Args []Value

func (Args) Chan

func (a Args) Chan(i int) <-chan interface{}

func (Args) Get

func (a Args) Get(i int, ctx ...context.Context) interface{}

func (Args) Int64

func (a Args) Int64(i int, ctx ...context.Context) (int64, error)

func (Args) String

func (a Args) String(i int, ctx ...context.Context) string

func (Args) Strings

func (a Args) Strings(ctx ...context.Context) []string

func (Args) UInt64

func (a Args) UInt64(i int, ctx ...context.Context) (uint64, error)

type Command

type Command interface {
	// Returns a CommandInfo struct describing this command.
	CommandInfo() CommandInfo

	// Returns flags for the command. If nil, arguments are passed verbatim.
	Flags() *pflag.FlagSet

	// Invokes the command. Returns a return value (if any), which will be printed.
	// In the future, it will be possible to assign return values to variables.
	Call(ctx context.Context, flags *pflag.FlagSet, args Args) (interface{}, error)
}

type CommandInfo

type CommandInfo struct {
	Name  string // Command name.
	Usage string // Description of arg usage.
	Short string // Short help text for `help`.
	Long  string // Long help text for `help [name]`.

	Subcommands []Command // Subcommands.
}

type HelpCommand

type HelpCommand struct {
}

func (HelpCommand) Call

func (c HelpCommand) Call(ctx context.Context, flags *pflag.FlagSet, args Args) (interface{}, error)

func (HelpCommand) CommandInfo

func (c HelpCommand) CommandInfo() CommandInfo

func (HelpCommand) Flags

func (c HelpCommand) Flags() *pflag.FlagSet

func (HelpCommand) ShowCommand

func (c HelpCommand) ShowCommand(ctx context.Context, args Args) error

func (HelpCommand) ShowIndex

func (c HelpCommand) ShowIndex(ctx context.Context)

type PS1

type PS1 func(err error) string

A function to be called to get the left-hand prompt (PS1).

func NewPS1

func NewPS1(succ, unsucc string) PS1

Returns a PS1 that shows a fixed message for successful and unsuccessful last commands.

type Scope

type Scope struct {
	Parent *Scope // Parent scope.
	Modal  bool   // A modal scope can be exitted with Ctrl+D.
	PS1    PS1    // PS1 associated with this scope; can be shadowed.
	// contains filtered or unexported fields
}

A Scope represents a scope in the shell, holding variables and commands. Scopes can be stacked, in a reverse linked list structure, and shadow each other.

func (*Scope) All

func (s *Scope) All() map[string]interface{}

Returns a map of all members visible in this scope and any parent scopes.

func (*Scope) Assign

func (s *Scope) Assign(name string, v interface{})

Sets a name to a value, attempting first to overwrite an existing instance of it, then resorting to creating it in the current scope.

func (*Scope) Child

func (s *Scope) Child() *Scope

Returns a child scope, with this scope set as its parent.

func (*Scope) Delete

func (s *Scope) Delete(name string)

Deletes the given name. It can be in this scope or in a lower one.

func (*Scope) Get

func (s *Scope) Get(name string) interface{}

Looks up the given name in this state or any higher ones. Returns an untyped (interface{}) nil if nothing is found.

func (*Scope) Set

func (s *Scope) Set(name string, v interface{})

Set the given name to a value. Shadows lower scopes.

type Shell

type Shell struct {
	Scope *Scope
	// contains filtered or unexported fields
}

func GetShell

func GetShell(ctx context.Context) *Shell

Returns the current shell. This is always available inside command handlers, or if you've previously called WithShell.

func New

func New() *Shell

func (*Shell) AddCommand

func (sh *Shell) AddCommand(cmd Command)

func (*Shell) Call

func (sh *Shell) Call(ctx context.Context, cmd Command, args []string) (interface{}, error)

func (*Shell) Dump

func (sh *Shell) Dump(retval interface{}) error

func (*Shell) DumpError

func (sh *Shell) DumpError(err error)

func (*Shell) Eval

func (sh *Shell) Eval(ctx context.Context, line string) (interface{}, error)

func (*Shell) Exec

func (sh *Shell) Exec(ctx context.Context, words []string) (interface{}, error)

func (*Shell) Lookup

func (sh *Shell) Lookup(args []string) (Command, []string, error)

func (*Shell) PasswordPrompt

func (sh *Shell) PasswordPrompt(ps1 string) (string, error)

func (*Shell) PopScope

func (sh *Shell) PopScope(s *Scope) *Scope

func (*Shell) Prompt

func (sh *Shell) Prompt(ps1 string) (string, error)

func (*Shell) PushScope

func (sh *Shell) PushScope() *Scope

func (*Shell) Run

func (sh *Shell) Run(ctx context.Context) (rerr error)

type Value

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

Wraps a value received from the commandline or another argument. This exists to abstract away the difference between values and streams of values, allowing you to build pipeline constructions without needlessly complicating the simple (and much more common) use case of Just Passing A Dang String.

func Wrap

func Wrap(v interface{}) Value

Wraps a value or list of values in a Value.

func WrapChan

func WrapChan(ch <-chan interface{}) Value

Wraps a channel of values in a Value.

func (Value) Chan

func (v Value) Chan() <-chan interface{}

Returns a channel for reading values. This will always return a channel - if the Value wraps a channel, that channel is returned, otherwise a buffered channel is returned that reads the wrapped value(s).

func (Value) Get

func (v Value) Get(ctx_ ...context.Context) interface{}

Returns the value; if the value wraps a channel, this will be the first value read from it.

func (Value) Int64

func (v Value) Int64(ctx ...context.Context) (int64, error)

func (Value) String

func (v Value) String(ctx ...context.Context) string

func (Value) UInt64

func (v Value) UInt64(ctx ...context.Context) (uint64, error)

Jump to

Keyboard shortcuts

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