mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-25 23:21:54 +08:00
cmds2/commands: better sorting
This commit is contained in:
@ -1,7 +1,8 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"bytes"
|
||||||
|
"sort"
|
||||||
|
|
||||||
cmds "github.com/jbenet/go-ipfs/commands"
|
cmds "github.com/jbenet/go-ipfs/commands"
|
||||||
)
|
)
|
||||||
@ -21,21 +22,24 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Run: func(req cmds.Request) (interface{}, error) {
|
Run: func(req cmds.Request) (interface{}, error) {
|
||||||
root := outputCommand("ipfs", root)
|
root := cmd2outputCmd("ipfs", root)
|
||||||
return &root, nil
|
return &root, nil
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
v := res.Output().(*Command)
|
v := res.Output().(*Command)
|
||||||
s := formatCommand("", v)
|
var buf bytes.Buffer
|
||||||
return []byte(s), nil
|
for _, s := range cmdPathStrings(v) {
|
||||||
|
buf.Write([]byte(s + "\n"))
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Type: &Command{},
|
Type: &Command{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputCommand(name string, cmd *cmds.Command) Command {
|
func cmd2outputCmd(name string, cmd *cmds.Command) Command {
|
||||||
output := Command{
|
output := Command{
|
||||||
Name: name,
|
Name: name,
|
||||||
Subcommands: make([]Command, len(cmd.Subcommands)),
|
Subcommands: make([]Command, len(cmd.Subcommands)),
|
||||||
@ -43,23 +47,25 @@ func outputCommand(name string, cmd *cmds.Command) Command {
|
|||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for name, sub := range cmd.Subcommands {
|
for name, sub := range cmd.Subcommands {
|
||||||
output.Subcommands[i] = outputCommand(name, sub)
|
output.Subcommands[i] = cmd2outputCmd(name, sub)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatCommand(prefix string, cmd *Command) string {
|
func cmdPathStrings(cmd *Command) []string {
|
||||||
if len(prefix) > 0 {
|
var cmds []string
|
||||||
prefix += " "
|
|
||||||
}
|
|
||||||
s := fmt.Sprintf("%s%s\n", prefix, cmd.Name)
|
|
||||||
|
|
||||||
prefix += cmd.Name
|
var recurse func(prefix string, cmd *Command)
|
||||||
for _, sub := range cmd.Subcommands {
|
recurse = func(prefix string, cmd *Command) {
|
||||||
s += formatCommand(prefix, &sub)
|
cmds = append(cmds, prefix+cmd.Name)
|
||||||
|
for _, sub := range cmd.Subcommands {
|
||||||
|
recurse(prefix+cmd.Name+" ", &sub)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
recurse("", cmd)
|
||||||
|
sort.Sort(sort.StringSlice(cmds))
|
||||||
|
return cmds
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user