doc.go 2.83 KB
Newer Older
keks's avatar
keks committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
	Package cmds helps building both standalone and client-server
	applications.

	Semantics

	The basic building blocks are requests, commands, emitters and
	responses. A command consists of a description of the
	parameters and a function. The function is passed the request
	as well as an emitter as arguments. It does operations on the
	inputs and sends the results to the user by emitting them.

	There are a number of emitters in this package and
	subpackages, but the user is free to create their own.

	Commands

	A command is a struct containing the commands help text, a
	description of the arguments and options, the command's
	processing function and a type to let the caller know what
	type will be emitted. Optionally one of the functions PostRun
keks's avatar
keks committed
22
	and Encoder may be defined that consumes the function's
keks's avatar
keks committed
23
	emitted values and generates a visual representation for e.g.
keks's avatar
keks committed
24
	the terminal. Encoders work on a value-by-value basis, while
keks's avatar
keks committed
25 26 27 28
	PostRun operates on the value stream.

	Emitters

keks's avatar
keks committed
29 30


keks's avatar
keks committed
31 32
	An emitter has the Emit method, that takes the command's
	function's output as an argument and passes it to the user.
keks's avatar
keks committed
33 34

		type ResponseEmitter interface {
35 36
			Close() error
			CloseWithError(error) error
keks's avatar
keks committed
37 38 39 40
			SetLength(length uint64)
			Emit(value interface{}) error
		}

keks's avatar
keks committed
41 42
	The command's function does not know what kind of emitter it
	works with, so the same function may run locally or on a
keks's avatar
keks committed
43 44
	server, using an rpc interface. Emitters can also send errors
	using the SetError method.
keks's avatar
keks committed
45 46 47

	The user-facing emitter usually is the cli emitter. Values
	emitter here will be printed to the terminal using either the
keks's avatar
keks committed
48
	Encoders or the PostRun function.
keks's avatar
keks committed
49 50 51

	Responses

keks's avatar
keks committed
52

keks's avatar
keks committed
53
	A response is a value that the user can read emitted values
keks's avatar
keks committed
54 55 56 57
	from.

		type Response interface {
			Request() Request
Steven Allen's avatar
Steven Allen committed
58
			Error() *Error
keks's avatar
keks committed
59 60 61 62 63
			Length() uint64
			Next() (interface{}, error)
		}

	Responses have a method Next() that returns the next
keks's avatar
keks committed
64
	emitted value and an error value. If the last element has been
keks's avatar
keks committed
65 66 67 68 69
	received, the returned error value is io.EOF. If the
	application code has sent an error using SetError, the error
	ErrRcvdError is returned on next, indicating that the caller
	should call Error(). Depending on the reponse type, other
	errors may also occur.
keks's avatar
keks committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

	Pipes

	Pipes are pairs (emitter, response), such that a value emitted
	on the emitter can be received in the response value. Most
	builtin emitters are "pipe" emitters. The most prominent
	examples are the channel pipe and the http pipe.

	The channel pipe is backed by a channel. The only error value
	returned by the response is io.EOF, which happens when the
	channel is closed.

	The http pipe is backed by an http connection. The response
	can also return other errors, e.g. if there are errors on the
	network.

	Examples

	To get a better idea of what's going on, take a look at the
	examples at
	https://github.com/ipfs/go-ipfs-cmds/tree/master/examples.
keks's avatar
keks committed
91

keks's avatar
keks committed
92 93
*/
package cmds