提交 a6d0fd28 作者: Jakub Sztandera

Fix commands --flags not showing flags via HTTP API

It was caused by the cmds.Option struct being Interface not struct
The solution is to create struct and copy interesting data over.

Also removed the "ShowOptions" field from being sent via the HTTP API.

License: MIT
Signed-off-by: 's avatarJakub Sztandera <kubuxu@protonmail.ch>
上级 dcf75036
......@@ -17,8 +17,11 @@ import (
type Command struct {
Name string
Subcommands []Command
Options []cmds.Option
ShowOptions bool
Options []Option
}
type Option struct {
Names []string
}
const (
......@@ -37,15 +40,15 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
cmds.BoolOption(flagsOptionName, "f", "Show command flags").Default(false),
},
Run: func(req cmds.Request, res cmds.Response) {
showOptions, _, _ := req.Option(flagsOptionName).Bool()
rootCmd := cmd2outputCmd("ipfs", root, showOptions)
rootCmd := cmd2outputCmd("ipfs", root)
res.SetOutput(&rootCmd)
},
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) {
for _, s := range cmdPathStrings(v, showOptions) {
buf.Write([]byte(s + "\n"))
}
return buf, nil
......@@ -55,35 +58,38 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
}
}
func cmd2outputCmd(name string, cmd *cmds.Command, showOptions bool) Command {
func cmd2outputCmd(name string, cmd *cmds.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)),
Options: cmd.Options,
ShowOptions: showOptions,
Options: opts,
}
i := 0
for name, sub := range cmd.Subcommands {
output.Subcommands[i] = cmd2outputCmd(name, sub, showOptions)
output.Subcommands[i] = cmd2outputCmd(name, sub)
i++
}
return output
}
func cmdPathStrings(cmd *Command) []string {
func cmdPathStrings(cmd *Command, showOptions bool) []string {
var cmds []string
var recurse func(prefix string, cmd *Command)
recurse = func(prefix string, cmd *Command) {
newPrefix := prefix + cmd.Name
cmds = append(cmds, newPrefix)
if prefix != "" && cmd.ShowOptions {
for _, option := range cmd.Options {
names := option.Names()
if prefix != "" && showOptions {
for _, options := range cmd.Options {
var cmdOpts []string
for _, flag := range names {
for _, flag := range options.Names {
if len(flag) == 1 {
flag = "-" + flag
} else {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论