github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands2/commands.go (about) 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 Description: "List all available commands.", 17 Help: `Lists all available commands (and subcommands) and exits. 18 `, 19 20 Run: func(req cmds.Request) (interface{}, error) { 21 root := outputCommand("ipfs", Root) 22 return &root, nil 23 }, 24 Marshallers: map[cmds.EncodingType]cmds.Marshaller{ 25 cmds.Text: func(res cmds.Response) ([]byte, error) { 26 v := res.Output().(*Command) 27 s := formatCommand(v, 0) 28 return []byte(s), nil 29 }, 30 }, 31 Type: &Command{}, 32 } 33 34 func outputCommand(name string, cmd *cmds.Command) Command { 35 output := Command{ 36 Name: name, 37 Subcommands: make([]Command, len(cmd.Subcommands)), 38 } 39 40 i := 0 41 for name, sub := range cmd.Subcommands { 42 output.Subcommands[i] = outputCommand(name, sub) 43 i++ 44 } 45 46 return output 47 } 48 49 func formatCommand(cmd *Command, depth int) string { 50 var s string 51 52 if depth > 0 { 53 indent := strings.Repeat(" ", depth-1) 54 s = fmt.Sprintf("%s%s\n", indent, cmd.Name) 55 } 56 57 for _, sub := range cmd.Subcommands { 58 s += formatCommand(&sub, depth+1) 59 } 60 61 return s 62 }