command.go 2.98 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(Response, Request)
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
	Type        interface{}
30
	Subcommands map[string]*Command
Matt Bell's avatar
Matt Bell committed
31 32
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34
// 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
35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

100 101
// 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
102 103 104 105 106 107
	options := make([]Option, len(c.Options))

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

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

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

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

	return optionsMap, nil
126 127
}

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