@cryptotaxi247 / kubo / commits / a6d0fd287

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: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed May 31, 2016 at 16:55 UTC a6d0fd287b75ffe1e41d55dbedc6ee71a4ac6c00
1 file changed +20 -14
core/commands/commands.go
+20 -14
@@ -17,8 +17,11 @@ import (
17 type Command struct {
18 Name string
19 Subcommands []Command
20 - Options []cmds.Option
21 - ShowOptions bool
20 + Options []Option
21 +}
22 +
23 +type Option struct {
24 + Names []string
25 }
26
27 const (
@@ -37,15 +40,15 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
40 cmds.BoolOption(flagsOptionName, "f", "Show command flags").Default(false),
41 },
42 Run: func(req cmds.Request, res cmds.Response) {
40 - showOptions, _, _ := req.Option(flagsOptionName).Bool()
41 - rootCmd := cmd2outputCmd("ipfs", root, showOptions)
43 + rootCmd := cmd2outputCmd("ipfs", root)
44 res.SetOutput(&rootCmd)
45 },
46 Marshalers: cmds.MarshalerMap{
47 cmds.Text: func(res cmds.Response) (io.Reader, error) {
48 v := res.Output().(*Command)
49 + showOptions, _, _ := res.Request().Option(flagsOptionName).Bool()
50 buf := new(bytes.Buffer)
48 - for _, s := range cmdPathStrings(v) {
51 + for _, s := range cmdPathStrings(v, showOptions) {
52 buf.Write([]byte(s + "\n"))
53 }
54 return buf, nil
@@ -55,35 +58,38 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
58 }
59 }
60
58 -func cmd2outputCmd(name string, cmd *cmds.Command, showOptions bool) Command {
61 +func cmd2outputCmd(name string, cmd *cmds.Command) Command {
62 + opts := make([]Option, len(cmd.Options))
63 + for i, opt := range cmd.Options {
64 + opts[i] = Option{opt.Names()}
65 + }
66 +
67 output := Command{
68 Name: name,
69 Subcommands: make([]Command, len(cmd.Subcommands)),
62 - Options: cmd.Options,
63 - ShowOptions: showOptions,
70 + Options: opts,
71 }
72
73 i := 0
74 for name, sub := range cmd.Subcommands {
68 - output.Subcommands[i] = cmd2outputCmd(name, sub, showOptions)
75 + output.Subcommands[i] = cmd2outputCmd(name, sub)
76 i++
77 }
78
79 return output
80 }
81
75 -func cmdPathStrings(cmd *Command) []string {
82 +func cmdPathStrings(cmd *Command, showOptions bool) []string {
83 var cmds []string
84
85 var recurse func(prefix string, cmd *Command)
86 recurse = func(prefix string, cmd *Command) {
87 newPrefix := prefix + cmd.Name
88 cmds = append(cmds, newPrefix)
82 - if prefix != "" && cmd.ShowOptions {
83 - for _, option := range cmd.Options {
84 - names := option.Names()
89 + if prefix != "" && showOptions {
90 + for _, options := range cmd.Options {
91 var cmdOpts []string
86 - for _, flag := range names {
92 + for _, flag := range options.Names {
93 if len(flag) == 1 {
94 flag = "-" + flag
95 } else {