cmds

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2017 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Undefined = ""

	JSON     = "json"
	XML      = "xml"
	Protobuf = "protobuf"
	Text     = "text"
	CLI      = "cli"
)

Supported EncodingType constants.

View Source
const DefaultOutputEncoding = JSON

Variables

View Source
var Decoders = map[EncodingType]func(w io.Reader) Decoder{
	XML: func(r io.Reader) Decoder {
		return xml.NewDecoder(r)
	},
	JSON: func(r io.Reader) Decoder {
		return json.NewDecoder(r)
	},
}
View Source
var Encoders = map[EncodingType]func(req Request) func(w io.Writer) Encoder{
	XML: func(req Request) func(io.Writer) Encoder {
		return func(w io.Writer) Encoder { return xml.NewEncoder(w) }
	},
	JSON: func(req Request) func(io.Writer) Encoder {
		return func(w io.Writer) Encoder { return json.NewEncoder(w) }
	},
	Text: func(req Request) func(io.Writer) Encoder {
		return func(w io.Writer) Encoder { return TextEncoder{w} }
	},
}
View Source
var ErrIncorrectType = errors.New("The command returned a value with a different type than expected")
View Source
var ErrNoFormatter = ClientError("This command cannot be formatted to plain text")
View Source
var ErrNotCallable = ClientError("This command can't be called directly. Try one of its subcommands.")

ErrNotCallable signals a command that cannot be called.

View Source
var (
	ErrRcvdError = fmt.Errorf("received command error")
)

Functions

func ClientError

func ClientError(msg string) error

func Copy

func Copy(re ResponseEmitter, res Response) error

func MakeEncoder

func MakeEncoder(f func(io.Writer, interface{}) error) func(Request) func(io.Writer) Encoder

func NewChanResponsePair

func NewChanResponsePair(req Request) (ResponseEmitter, Response)

func OldCommand

func OldCommand(cmd *Command) *oldcmds.Command

OldCommand returns an oldcmds.Command from a Command.

func OldContext

func OldContext(ctx Context) oldcmds.Context

OldContext returns an oldcmds.Context from a Context

func OldReqLog

func OldReqLog(newrl *ReqLog) *oldcmds.ReqLog

OldReqLog returns an oldcmds.ReqLog from a ReqLog

Types

type Any

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

func (*Any) Add

func (a *Any) Add(v interface{})

func (*Any) Interface

func (a *Any) Interface() interface{}

func (*Any) UnmarshalJSON

func (a *Any) UnmarshalJSON(data []byte) error

type Command

type Command struct {
	Options   []cmdsutil.Option
	Arguments []cmdsutil.Argument
	PreRun    func(req Request) error

	// Run is the function that processes the request to generate a response.
	// Note that when executing the command over the HTTP API you can only read
	// after writing when using multipart requests. The request body will not be
	// available for reading after the HTTP connection has been written to.
	Run      Function
	PostRun  PostRunMap
	Encoders map[EncodingType]func(Request) func(io.Writer) Encoder
	Helptext cmdsutil.HelpText

	// External denotes that a command is actually an external binary.
	// fewer checks and validations will be performed on such commands.
	External bool

	// Type describes the type of the output of the Command's Run Function.
	// In precise terms, the value of Type is an instance of the return type of
	// the Run Function.
	//
	// ie. If command Run returns &Block{}, then Command.Type == &Block{}
	Type           interface{}
	Subcommands    map[string]*Command
	OldSubcommands map[string]*oldcmds.Command
}

Command is a runnable command, with input arguments and options (flags). It can also have Subcommands, to group units of work into sets.

func NewCommand

func NewCommand(oldcmd *oldcmds.Command) *Command

NewCommand returns a Command from an oldcmds.Command

func (*Command) Call

func (c *Command) Call(req Request, re ResponseEmitter) error

Call invokes the command for the given Request

func (*Command) CheckArguments

func (c *Command) CheckArguments(req Request) error

func (*Command) Get

func (c *Command) Get(path []string) (*Command, error)

Get resolves and returns the Command addressed by path

func (*Command) GetOptions

func (c *Command) GetOptions(path []string) (map[string]cmdsutil.Option, error)

GetOptions returns the options in the given path of commands

func (*Command) ProcessHelp

func (c *Command) ProcessHelp()

func (*Command) Resolve

func (c *Command) Resolve(pth []string) ([]*Command, error)

Resolve returns the subcommands at the given path

func (*Command) Subcommand

func (c *Command) Subcommand(id string) *Command

Subcommand returns the subcommand with the given id

func (*Command) Walk

func (c *Command) Walk(visitor CommandVisitor)

Walks tree of all subcommands (including this one)

type CommandVisitor

type CommandVisitor func(*Command)

type Context

type Context struct {
	Online     bool
	ConfigRoot string
	ReqLog     *ReqLog

	LoadConfig func(path string) (*config.Config, error)

	ConstructNode func() (*core.IpfsNode, error)
	// contains filtered or unexported fields
}

func NewContext

func NewContext(ctx oldcmds.Context) Context

NewContext returns a Context from an oldcmds.Context

func (*Context) GetConfig

func (c *Context) GetConfig() (*config.Config, error)

GetConfig returns the config of the current Command exection context. It may load it with the providied function.

func (*Context) GetNode

func (c *Context) GetNode() (*core.IpfsNode, error)

GetNode returns the node of the current Command exection context. It may construct it with the provided function.

func (*Context) NodeWithoutConstructing

func (c *Context) NodeWithoutConstructing() *core.IpfsNode

NodeWithoutConstructing returns the underlying node variable so that clients may close it.

type Decoder

type Decoder interface {
	Decode(value interface{}) error
}

Decoder decodes values into value (which should be a pointer).

type Encoder

type Encoder interface {
	Encode(value interface{}) error
}

Encoder encodes values onto e.g. an io.Writer. Examples are json.Encoder and xml.Encoder.

type EncodingEmitter

type EncodingEmitter interface {
	ResponseEmitter

	SetEncoder(func(io.Writer) Encoder)
}

type EncodingType

type EncodingType string

EncodingType defines a supported encoding

func GetEncoding

func GetEncoding(req Request) EncodingType

GetEncoding returns the EncodingType set in a request, falling back to JSON

type Flusher

type Flusher interface {
	Flush() error
}

type Function

type Function func(Request, ResponseEmitter)

Function is the type of function that Commands use. It reads from the Request, and writes results to the ResponseEmitter.

type Head struct {
	Len uint64
	Err *cmdsutil.Error
}

func (Head) Error

func (h Head) Error() *cmdsutil.Error

func (Head) Length

func (h Head) Length() uint64
type Header interface {
	Head() Head
}

type MarshalerEncoder

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

MarshalerEncoder implements Encoder from a Marshaler

func NewMarshalerEncoder

func NewMarshalerEncoder(req Request, m oldcmds.Marshaler, w io.Writer) *MarshalerEncoder

NewMarshalerEncoder returns a new MarshalerEncoder

func (*MarshalerEncoder) Encode

func (me *MarshalerEncoder) Encode(v interface{}) error

Encode encodes v onto the io.Writer w using Marshaler m, with both m and w passed in NewMarshalerEncoder

type OptMap

type OptMap map[string]interface{}

type PostRunMap

PostRunMap is the map used in Command.PostRun.

type ReqLog

type ReqLog struct {
	Requests []*ReqLogEntry
	// contains filtered or unexported fields
}

func (*ReqLog) Add

func (rl *ReqLog) Add(req Request) *ReqLogEntry

func (*ReqLog) AddEntry

func (rl *ReqLog) AddEntry(rle *ReqLogEntry)

func (*ReqLog) ClearInactive

func (rl *ReqLog) ClearInactive()

func (*ReqLog) Finish

func (rl *ReqLog) Finish(rle *ReqLogEntry)

func (*ReqLog) Report

func (rl *ReqLog) Report() []*ReqLogEntry

Report generates a copy of all the entries in the requestlog

func (*ReqLog) SetKeepTime

func (rl *ReqLog) SetKeepTime(t time.Duration)

type ReqLogEntry

type ReqLogEntry struct {
	StartTime time.Time
	EndTime   time.Time
	Active    bool
	Command   string
	Options   map[string]interface{}
	Args      []string
	ID        int
}

func (*ReqLogEntry) Copy

func (r *ReqLogEntry) Copy() *ReqLogEntry

type Request

type Request interface {
	Path() []string
	Option(name string) *cmdsutil.OptionValue
	Options() cmdsutil.OptMap
	SetOption(name string, val interface{})
	SetOptions(opts cmdsutil.OptMap) error
	Arguments() []string
	StringArguments() []string
	SetArguments([]string)
	Files() files.File
	SetFiles(files.File)
	Context() context.Context
	SetRootContext(context.Context) error
	InvocContext() *Context
	SetInvocContext(Context)
	Command() *Command
	Values() map[string]interface{}
	Stdin() io.Reader
	VarArgs(func(string) error) error

	ConvertOptions() error
}

Request represents a call to a command from a consumer

func NewEmptyRequest

func NewEmptyRequest() (Request, error)

NewEmptyRequest initializes an empty request

func NewRequest

func NewRequest(path []string, opts cmdsutil.OptMap, args []string, file files.File, cmd *Command, optDefs map[string]cmdsutil.Option) (Request, error)

NewRequest returns a request initialized with given arguments An non-nil error will be returned if the provided option values are invalid

func WrapOldRequest

func WrapOldRequest(r oldcmds.Request) Request

WrapOldRequest returns a faked Request from an oldcmds.Request.

type Response

type Response interface {
	Request() Request

	Error() *cmdsutil.Error
	Length() uint64

	// Next returns the next emitted value.
	// The returned error can be a network or decoding error.
	// The error can also be ErrRcvdError if an error has been emitted.
	// In this case the emitted error can be accessed using the Error() method.
	Next() (interface{}, error)
}

Response is the result of a command request. Response is returned to the client.

func NewReaderResponse

func NewReaderResponse(r io.Reader, encType EncodingType, req Request) Response

type ResponseEmitter

type ResponseEmitter interface {
	// closes http conn or channel
	io.Closer

	// SetLength sets the length of the output
	// err is an interface{} so we don't have to manually convert to error.
	SetLength(length uint64)

	// SetError sets the response error
	// err is an interface{} so we don't have to manually convert to error.
	SetError(err interface{}, code cmdsutil.ErrorType) error

	// Emit sends a value
	// if value is io.Reader we just copy that to the connection
	// other values are marshalled
	Emit(value interface{}) error
}

ResponseEmitter encodes and sends the command code's output to the client. It is all a command can write to.

func NewFlushForwarder

func NewFlushForwarder(re ResponseEmitter, f Flusher) ResponseEmitter

func NewTeeEmitter

func NewTeeEmitter(re1, re2 ResponseEmitter) ResponseEmitter

NewTeeEmitter creates a new ResponseEmitter. Writing to it will write to both the passed ResponseEmitters.

type TeeError

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

func (TeeError) BothNil

func (err TeeError) BothNil() bool

func (TeeError) Error

func (err TeeError) Error() string

type TextEncoder

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

func (TextEncoder) Encode

func (e TextEncoder) Encode(v interface{}) error

type WriterResponseEmitter

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

func NewWriterResponseEmitter

func NewWriterResponseEmitter(w io.WriteCloser, req Request, enc func(Request) func(io.Writer) Encoder) *WriterResponseEmitter

func (*WriterResponseEmitter) Close

func (re *WriterResponseEmitter) Close() error

func (*WriterResponseEmitter) Emit

func (re *WriterResponseEmitter) Emit(v interface{}) error

func (*WriterResponseEmitter) Head

func (re *WriterResponseEmitter) Head() Head

func (*WriterResponseEmitter) SetEncoder added in v0.2.3

func (re *WriterResponseEmitter) SetEncoder(mkEnc func(io.Writer) Encoder)

func (*WriterResponseEmitter) SetError

func (re *WriterResponseEmitter) SetError(v interface{}, errType cmdsutil.ErrorType) error

func (*WriterResponseEmitter) SetLength

func (re *WriterResponseEmitter) SetLength(length uint64)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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