commands.go 2.51 KB
Newer Older
1 2 3 4 5
// Package commands implements the IPFS command interface
//
// Using github.com/ipfs/go-ipfs/commands to define the command line and HTTP
// APIs.  This is the interface available to folks using IPFS from outside of
// the Go language.
6 7 8
package commands

import (
9
	"bytes"
10
	"io"
11
	"sort"
12
	"strings"
13

14
	cmds "github.com/ipfs/go-ipfs/commands"
15 16 17 18 19
)

type Command struct {
	Name        string
	Subcommands []Command
rht's avatar
rht committed
20
	Options     []cmds.Option
21
	ShowOptions bool
22 23
}

24
const (
25
	flagsOptionName = "flags"
26 27
)

28 29 30 31
// CommandsCmd takes in a root command,
// and returns a command that lists the subcommands in that root
func CommandsCmd(root *cmds.Command) *cmds.Command {
	return &cmds.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33 34 35
		Helptext: cmds.HelpText{
			Tagline:          "List all available commands.",
			ShortDescription: `Lists all available commands (and subcommands) and exits.`,
		},
36
		Options: []cmds.Option{
37
			cmds.BoolOption(flagsOptionName, "f", "Show command flags").Default(false),
38
		},
39
		Run: func(req cmds.Request, res cmds.Response) {
40
			showOptions, _, _ := req.Option(flagsOptionName).Bool()
41 42
			rootCmd := cmd2outputCmd("ipfs", root, showOptions)
			res.SetOutput(&rootCmd)
43
		},
44
		Marshalers: cmds.MarshalerMap{
45
			cmds.Text: func(res cmds.Response) (io.Reader, error) {
46
				v := res.Output().(*Command)
47
				buf := new(bytes.Buffer)
48 49 50
				for _, s := range cmdPathStrings(v) {
					buf.Write([]byte(s + "\n"))
				}
51
				return buf, nil
52
			},
53
		},
54
		Type: Command{},
55
	}
56 57
}

58
func cmd2outputCmd(name string, cmd *cmds.Command, showOptions bool) Command {
59 60 61
	output := Command{
		Name:        name,
		Subcommands: make([]Command, len(cmd.Subcommands)),
rht's avatar
rht committed
62
		Options:     cmd.Options,
63
		ShowOptions: showOptions,
64 65 66 67
	}

	i := 0
	for name, sub := range cmd.Subcommands {
68
		output.Subcommands[i] = cmd2outputCmd(name, sub, showOptions)
69 70 71 72 73 74
		i++
	}

	return output
}

75 76
func cmdPathStrings(cmd *Command) []string {
	var cmds []string
77

78 79
	var recurse func(prefix string, cmd *Command)
	recurse = func(prefix string, cmd *Command) {
rht's avatar
rht committed
80 81
		newPrefix := prefix + cmd.Name
		cmds = append(cmds, newPrefix)
82
		if prefix != "" && cmd.ShowOptions {
rht's avatar
rht committed
83
			for _, option := range cmd.Options {
84 85 86 87 88 89 90 91 92
				names := option.Names()
				var cmdOpts []string
				for _, flag := range names {
					if len(flag) == 1 {
						flag = "-" + flag
					} else {
						flag = "--" + flag
					}
					cmdOpts = append(cmdOpts, newPrefix+" "+flag)
rht's avatar
rht committed
93
				}
94
				cmds = append(cmds, strings.Join(cmdOpts, " / "))
rht's avatar
rht committed
95 96
			}
		}
97
		for _, sub := range cmd.Subcommands {
rht's avatar
rht committed
98
			recurse(newPrefix+" ", &sub)
99
		}
100 101
	}

102 103 104
	recurse("", cmd)
	sort.Sort(sort.StringSlice(cmds))
	return cmds
105
}