提交 0c205f56 作者: Matt Bell 提交者: Juan Batiz-Benet

core/commands2: Added 'commands' command

上级 921d3a51
package commands
import (
"fmt"
"strings"
cmds "github.com/jbenet/go-ipfs/commands"
)
type Command struct {
Name string
Subcommands []Command
}
var commandsCmd = &cmds.Command{
Help: "TODO",
Run: func(res cmds.Response, req cmds.Request) {
root := outputCommand("ipfs", Root)
res.SetValue(&root)
},
Format: func(res cmds.Response) (string, error) {
v := res.Value().(*Command)
return formatCommand(v, 0), nil
},
Type: &Command{},
}
func outputCommand(name string, cmd *cmds.Command) Command {
output := Command{
Name: name,
Subcommands: make([]Command, len(cmd.Subcommands)),
}
i := 0
for name, sub := range cmd.Subcommands {
output.Subcommands[i] = outputCommand(name, sub)
i++
}
return output
}
func formatCommand(cmd *Command, depth int) string {
var s string
if depth > 0 {
indent := strings.Repeat(" ", depth-1)
s = fmt.Sprintf("%s%s\n", indent, cmd.Name)
}
for _, sub := range cmd.Subcommands {
s += formatCommand(&sub, depth+1)
}
return s
}
......@@ -110,4 +110,5 @@ Use "ipfs help <command>" for more information about a command.
func init() {
Root.Subcommands["daemon"] = daemonCmd
Root.Subcommands["commands"] = commandsCmd
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论