提交 c814478f 作者: Jeromy Johnson

Merge pull request #2773 from ipfs/feature/commands-flags

Fix commands --flags not showing flags via HTTP API
...@@ -17,8 +17,11 @@ import ( ...@@ -17,8 +17,11 @@ import (
type Command struct { type Command struct {
Name string Name string
Subcommands []Command Subcommands []Command
Options []cmds.Option Options []Option
ShowOptions bool }
type Option struct {
Names []string
} }
const ( const (
...@@ -37,15 +40,15 @@ func CommandsCmd(root *cmds.Command) *cmds.Command { ...@@ -37,15 +40,15 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
cmds.BoolOption(flagsOptionName, "f", "Show command flags").Default(false), cmds.BoolOption(flagsOptionName, "f", "Show command flags").Default(false),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
showOptions, _, _ := req.Option(flagsOptionName).Bool() rootCmd := cmd2outputCmd("ipfs", root)
rootCmd := cmd2outputCmd("ipfs", root, showOptions)
res.SetOutput(&rootCmd) res.SetOutput(&rootCmd)
}, },
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*Command) v := res.Output().(*Command)
showOptions, _, _ := res.Request().Option(flagsOptionName).Bool()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
for _, s := range cmdPathStrings(v) { for _, s := range cmdPathStrings(v, showOptions) {
buf.Write([]byte(s + "\n")) buf.Write([]byte(s + "\n"))
} }
return buf, nil return buf, nil
...@@ -55,35 +58,38 @@ func CommandsCmd(root *cmds.Command) *cmds.Command { ...@@ -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{ output := Command{
Name: name, Name: name,
Subcommands: make([]Command, len(cmd.Subcommands)), Subcommands: make([]Command, len(cmd.Subcommands)),
Options: cmd.Options, Options: opts,
ShowOptions: showOptions,
} }
i := 0 i := 0
for name, sub := range cmd.Subcommands { for name, sub := range cmd.Subcommands {
output.Subcommands[i] = cmd2outputCmd(name, sub, showOptions) output.Subcommands[i] = cmd2outputCmd(name, sub)
i++ i++
} }
return output return output
} }
func cmdPathStrings(cmd *Command) []string { func cmdPathStrings(cmd *Command, showOptions bool) []string {
var cmds []string var cmds []string
var recurse func(prefix string, cmd *Command) var recurse func(prefix string, cmd *Command)
recurse = func(prefix string, cmd *Command) { recurse = func(prefix string, cmd *Command) {
newPrefix := prefix + cmd.Name newPrefix := prefix + cmd.Name
cmds = append(cmds, newPrefix) cmds = append(cmds, newPrefix)
if prefix != "" && cmd.ShowOptions { if prefix != "" && showOptions {
for _, option := range cmd.Options { for _, options := range cmd.Options {
names := option.Names()
var cmdOpts []string var cmdOpts []string
for _, flag := range names { for _, flag := range options.Names {
if len(flag) == 1 { if len(flag) == 1 {
flag = "-" + flag flag = "-" + flag
} else { } else {
......
#!/bin/sh
test_description="Tests for various fxed issues and regressions."
. lib/test-lib.sh
test_init_ipfs
# Tests go here
test_done
#!/bin/sh
test_description="Tests for various fxed issues and regressions."
. lib/test-lib.sh
test_init_ipfs
test_launch_ipfs_daemon
# Tests go here
test_expect_sucess "commands command with flag flags works via HTTP API - #2301" '
curl "http://$API_ADDR/api/v0/commands?flags" | grep "verbose"
'
test_kill_ipfs_daemon
test_done
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论