command.go 2.95 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2 3
package commands

import (
Matt Bell's avatar
Matt Bell committed
4
	"errors"
Matt Bell's avatar
Matt Bell committed
5 6
	"fmt"
	"strings"
7 8

	u "github.com/jbenet/go-ipfs/util"
Matt Bell's avatar
Matt Bell committed
9 10
)

11 12
var log = u.Logger("command")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
// Function is the type of function that Commands use.
// It reads from the Request, and writes results to the Response.
15
type Function func(Request, Response)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16

17 18 19 20 21
// Formatter is a function that takes in a Response, and returns a human-readable string
// (or an error on failure)
// MAYBE_TODO: maybe this should be a io.Reader instead of a string?
type Formatter func(Response) (string, error)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22
// Command is a runnable command, with input arguments and options (flags).
23
// It can also have Subcommands, to group units of work into sets.
Matt Bell's avatar
Matt Bell committed
24
type Command struct {
25 26 27
	Help        string
	Options     []Option
	Run         Function
28
	Format      Formatter
29
	Subcommands map[string]*Command
Matt Bell's avatar
Matt Bell committed
30 31
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33
// ErrNotCallable signals a command that cannot be called.
var ErrNotCallable = errors.New("This command can't be called directly. Try one of its subcommands.")
Matt Bell's avatar
Matt Bell committed
34

35 36
var ErrNoFormatter = errors.New("This command cannot be formatted to plain text")

37
// Call invokes the command for the given Request
38 39
func (c *Command) Call(req Request) Response {
	res := NewResponse(req)
Matt Bell's avatar
Matt Bell committed
40

41
	cmds, err := c.Resolve(req.Path())
Matt Bell's avatar
Matt Bell committed
42
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43
		res.SetError(err, ErrClient)
Matt Bell's avatar
Matt Bell committed
44 45 46
		return res
	}
	cmd := cmds[len(cmds)-1]
Matt Bell's avatar
Matt Bell committed
47

48
	if cmd.Run == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
		res.SetError(ErrNotCallable, ErrClient)
Matt Bell's avatar
Matt Bell committed
50 51
		return res
	}
Matt Bell's avatar
Matt Bell committed
52

53
	options, err := c.GetOptions(req.Path())
Matt Bell's avatar
Matt Bell committed
54
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
		res.SetError(err, ErrClient)
Matt Bell's avatar
Matt Bell committed
56 57
		return res
	}
Matt Bell's avatar
Matt Bell committed
58

59
	err = req.ConvertOptions(options)
Matt Bell's avatar
Matt Bell committed
60
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
		res.SetError(err, ErrClient)
Matt Bell's avatar
Matt Bell committed
62 63
		return res
	}
Matt Bell's avatar
Matt Bell committed
64

65
	cmd.Run(req, res)
Matt Bell's avatar
Matt Bell committed
66 67

	return res
Matt Bell's avatar
Matt Bell committed
68 69
}

Matt Bell's avatar
Matt Bell committed
70 71
// Resolve gets the subcommands at the given path
func (c *Command) Resolve(path []string) ([]*Command, error) {
Matt Bell's avatar
Matt Bell committed
72 73
	cmds := make([]*Command, len(path)+1)
	cmds[0] = c
Matt Bell's avatar
Matt Bell committed
74

Matt Bell's avatar
Matt Bell committed
75 76
	cmd := c
	for i, name := range path {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
		cmd = cmd.Subcommand(name)
Matt Bell's avatar
Matt Bell committed
78

Matt Bell's avatar
Matt Bell committed
79 80 81 82
		if cmd == nil {
			pathS := strings.Join(path[0:i], "/")
			return nil, fmt.Errorf("Undefined command: '%s'", pathS)
		}
Matt Bell's avatar
Matt Bell committed
83

Matt Bell's avatar
Matt Bell committed
84 85
		cmds[i+1] = cmd
	}
Matt Bell's avatar
Matt Bell committed
86

Matt Bell's avatar
Matt Bell committed
87
	return cmds, nil
Matt Bell's avatar
Matt Bell committed
88 89
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
// Get resolves and returns the Command addressed by path
Matt Bell's avatar
Matt Bell committed
91
func (c *Command) Get(path []string) (*Command, error) {
Matt Bell's avatar
Matt Bell committed
92 93 94 95 96
	cmds, err := c.Resolve(path)
	if err != nil {
		return nil, err
	}
	return cmds[len(cmds)-1], nil
Matt Bell's avatar
Matt Bell committed
97 98
}

99 100
// GetOptions gets the options in the given path of commands
func (c *Command) GetOptions(path []string) (map[string]Option, error) {
Matt Bell's avatar
Matt Bell committed
101 102 103 104 105 106
	options := make([]Option, len(c.Options))

	cmds, err := c.Resolve(path)
	if err != nil {
		return nil, err
	}
107 108
	cmds = append(cmds, globalCommand)

Matt Bell's avatar
Matt Bell committed
109 110 111 112 113 114 115
	for _, cmd := range cmds {
		options = append(options, cmd.Options...)
	}

	optionsMap := make(map[string]Option)
	for _, opt := range options {
		for _, name := range opt.Names {
116 117 118 119
			if _, found := optionsMap[name]; found {
				return nil, fmt.Errorf("Option name '%s' used multiple times", name)
			}

Matt Bell's avatar
Matt Bell committed
120 121 122 123 124
			optionsMap[name] = opt
		}
	}

	return optionsMap, nil
125 126
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127 128
// Subcommand returns the subcommand with the given id
func (c *Command) Subcommand(id string) *Command {
129
	return c.Subcommands[id]
130
}