提交 f797d135 作者: Matt Bell 提交者: Juan Batiz-Benet

core/commands2: Added function to generate a 'commands' command for a root

上级 fd8b1930
...@@ -11,23 +11,27 @@ type Command struct { ...@@ -11,23 +11,27 @@ type Command struct {
Subcommands []Command Subcommands []Command
} }
var commandsCmd = &cmds.Command{ // CommandsCmd takes in a root command,
Description: "List all available commands.", // and returns a command that lists the subcommands in that root
Help: `Lists all available commands (and subcommands) and exits. func CommandsCmd(root *cmds.Command) *cmds.Command {
`, return &cmds.Command{
Description: "List all available commands.",
Run: func(req cmds.Request) (interface{}, error) { Help: `Lists all available commands (and subcommands) and exits.
root := outputCommand("ipfs", Root) `,
return &root, nil
}, Run: func(req cmds.Request) (interface{}, error) {
Marshallers: map[cmds.EncodingType]cmds.Marshaller{ root := outputCommand("ipfs", root)
cmds.Text: func(res cmds.Response) ([]byte, error) { return &root, nil
v := res.Output().(*Command) },
s := formatCommand("", v) Marshallers: map[cmds.EncodingType]cmds.Marshaller{
return []byte(s), nil cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*Command)
s := formatCommand("", v)
return []byte(s), nil
},
}, },
}, Type: &Command{},
Type: &Command{}, }
} }
func outputCommand(name string, cmd *cmds.Command) Command { func outputCommand(name string, cmd *cmds.Command) Command {
......
...@@ -52,7 +52,7 @@ Plumbing commands: ...@@ -52,7 +52,7 @@ Plumbing commands:
var rootSubcommands = map[string]*cmds.Command{ var rootSubcommands = map[string]*cmds.Command{
"cat": catCmd, "cat": catCmd,
"ls": lsCmd, "ls": lsCmd,
"commands": commandsCmd, "commands": CommandsCmd(Root),
"name": nameCmd, "name": nameCmd,
"add": addCmd, "add": addCmd,
"log": logCmd, "log": logCmd,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论