@cryptotaxi247 / kubo / commits / 0c205f56c

core/commands2: Added 'commands' command

Matt Bell committed Oct 30, 2014 at 21:19 UTC 0c205f56cb4a254ab1eb00ea13ed8fd209f9e206
2 files changed +57
core/commands2/commands.go new
+56
@@ -0,0 +1,56 @@
1 +package commands
2 +
3 +import (
4 + "fmt"
5 + "strings"
6 +
7 + cmds "github.com/jbenet/go-ipfs/commands"
8 +)
9 +
10 +type Command struct {
11 + Name string
12 + Subcommands []Command
13 +}
14 +
15 +var commandsCmd = &cmds.Command{
16 + Help: "TODO",
17 + Run: func(res cmds.Response, req cmds.Request) {
18 + root := outputCommand("ipfs", Root)
19 + res.SetValue(&root)
20 + },
21 + Format: func(res cmds.Response) (string, error) {
22 + v := res.Value().(*Command)
23 + return formatCommand(v, 0), nil
24 + },
25 + Type: &Command{},
26 +}
27 +
28 +func outputCommand(name string, cmd *cmds.Command) Command {
29 + output := Command{
30 + Name: name,
31 + Subcommands: make([]Command, len(cmd.Subcommands)),
32 + }
33 +
34 + i := 0
35 + for name, sub := range cmd.Subcommands {
36 + output.Subcommands[i] = outputCommand(name, sub)
37 + i++
38 + }
39 +
40 + return output
41 +}
42 +
43 +func formatCommand(cmd *Command, depth int) string {
44 + var s string
45 +
46 + if depth > 0 {
47 + indent := strings.Repeat(" ", depth-1)
48 + s = fmt.Sprintf("%s%s\n", indent, cmd.Name)
49 + }
50 +
51 + for _, sub := range cmd.Subcommands {
52 + s += formatCommand(&sub, depth+1)
53 + }
54 +
55 + return s
56 +}
core/commands2/root.go
+1
@@ -110,4 +110,5 @@ Use "ipfs help <command>" for more information about a command.
110
111 func init() {
112 Root.Subcommands["daemon"] = daemonCmd
113 + Root.Subcommands["commands"] = commandsCmd
114 }