mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-17 19:54:25 +08:00
cmd: use go-ipfs-cmds
License: MIT Signed-off-by: keks <keks@cryptoscope.co>
This commit is contained in:
@ -6,18 +6,48 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
oldcmds "github.com/ipfs/go-ipfs/commands"
|
||||
e "github.com/ipfs/go-ipfs/core/commands/e"
|
||||
|
||||
cmds "gx/ipfs/QmQVvuDwXUGbtYmbmTcbLtGRYXnEbymaR2zEj38GVysqWe/go-ipfs-cmds"
|
||||
"gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
type commandEncoder struct {
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
func (e *commandEncoder) Encode(v interface{}) error {
|
||||
var (
|
||||
cmd *Command
|
||||
ok bool
|
||||
)
|
||||
|
||||
if cmd, ok = v.(*Command); !ok {
|
||||
return fmt.Errorf(`core/commands: uenxpected type %T, expected *"core/commands".Command`, v)
|
||||
}
|
||||
|
||||
for _, s := range cmdPathStrings(cmd, cmd.showOpts) {
|
||||
_, err := e.w.Write([]byte(s + "\n"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
Subcommands []Command
|
||||
Options []Option
|
||||
|
||||
showOpts bool
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
@ -32,26 +62,24 @@ const (
|
||||
// and returns a command that lists the subcommands in that root
|
||||
func CommandsCmd(root *cmds.Command) *cmds.Command {
|
||||
return &cmds.Command{
|
||||
Helptext: cmds.HelpText{
|
||||
Helptext: cmdkit.HelpText{
|
||||
Tagline: "List all available commands.",
|
||||
ShortDescription: `Lists all available commands (and subcommands) and exits.`,
|
||||
},
|
||||
Options: []cmds.Option{
|
||||
cmds.BoolOption(flagsOptionName, "f", "Show command flags").Default(false),
|
||||
Options: []cmdkit.Option{
|
||||
cmdkit.BoolOption(flagsOptionName, "f", "Show command flags").Default(false),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
Run: func(req cmds.Request, res cmds.ResponseEmitter) {
|
||||
rootCmd := cmd2outputCmd("ipfs", root)
|
||||
res.SetOutput(&rootCmd)
|
||||
rootCmd.showOpts, _, _ = req.Option(flagsOptionName).Bool()
|
||||
err := res.Emit(&rootCmd)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||
v := res.Output().(*Command)
|
||||
showOptions, _, _ := res.Request().Option(flagsOptionName).Bool()
|
||||
buf := new(bytes.Buffer)
|
||||
for _, s := range cmdPathStrings(v, showOptions) {
|
||||
buf.Write([]byte(s + "\n"))
|
||||
}
|
||||
return buf, nil
|
||||
Encoders: cmds.EncoderMap{
|
||||
cmds.Text: func(req cmds.Request) func(io.Writer) cmds.Encoder {
|
||||
return func(w io.Writer) cmds.Encoder { return &commandEncoder{w} }
|
||||
},
|
||||
},
|
||||
Type: Command{},
|
||||
@ -64,6 +92,43 @@ func cmd2outputCmd(name string, cmd *cmds.Command) Command {
|
||||
opts[i] = Option{opt.Names()}
|
||||
}
|
||||
|
||||
output := Command{
|
||||
Name: name,
|
||||
Subcommands: make([]Command, len(cmd.Subcommands)+len(cmd.OldSubcommands)),
|
||||
Options: opts,
|
||||
}
|
||||
|
||||
// we need to keep track of names because a name *might* be used by both a Subcommand and an OldSubscommand.
|
||||
names := make(map[string]struct{})
|
||||
|
||||
i := 0
|
||||
for name, sub := range cmd.Subcommands {
|
||||
names[name] = struct{}{}
|
||||
output.Subcommands[i] = cmd2outputCmd(name, sub)
|
||||
i++
|
||||
}
|
||||
|
||||
for name, sub := range cmd.OldSubcommands {
|
||||
if _, ok := names[name]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
names[name] = struct{}{}
|
||||
output.Subcommands[i] = oldCmd2outputCmd(name, sub)
|
||||
i++
|
||||
}
|
||||
|
||||
// trucate to the amount of names we actually have
|
||||
output.Subcommands = output.Subcommands[:len(names)]
|
||||
return output
|
||||
}
|
||||
|
||||
func oldCmd2outputCmd(name string, cmd *oldcmds.Command) Command {
|
||||
opts := make([]Option, len(cmd.Options))
|
||||
for i, opt := range cmd.Options {
|
||||
opts[i] = Option{opt.Names()}
|
||||
}
|
||||
|
||||
output := Command{
|
||||
Name: name,
|
||||
Subcommands: make([]Command, len(cmd.Subcommands)),
|
||||
@ -72,7 +137,7 @@ func cmd2outputCmd(name string, cmd *cmds.Command) Command {
|
||||
|
||||
i := 0
|
||||
for name, sub := range cmd.Subcommands {
|
||||
output.Subcommands[i] = cmd2outputCmd(name, sub)
|
||||
output.Subcommands[i] = oldCmd2outputCmd(name, sub)
|
||||
i++
|
||||
}
|
||||
|
||||
@ -109,3 +174,21 @@ func cmdPathStrings(cmd *Command, showOptions bool) []string {
|
||||
sort.Sort(sort.StringSlice(cmds))
|
||||
return cmds
|
||||
}
|
||||
|
||||
// changes here will also need to be applied at
|
||||
// - ./dag/dag.go
|
||||
// - ./object/object.go
|
||||
// - ./files/files.go
|
||||
// - ./unixfs/unixfs.go
|
||||
func unwrapOutput(i interface{}) (interface{}, error) {
|
||||
var (
|
||||
ch <-chan interface{}
|
||||
ok bool
|
||||
)
|
||||
|
||||
if ch, ok = i.(<-chan interface{}); !ok {
|
||||
return nil, e.TypeErr(ch, i)
|
||||
}
|
||||
|
||||
return <-ch, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user