From a6d0fd287b75ffe1e41d55dbedc6ee71a4ac6c00 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 31 May 2016 16:55:30 +0200 Subject: [PATCH 1/2] 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 --- core/commands/commands.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/core/commands/commands.go b/core/commands/commands.go index 3d43a2186d7..e69659caf6f 100644 --- a/core/commands/commands.go +++ b/core/commands/commands.go @@ -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 { From 7167e93be4755573e3c2d7f7254ec375bf29b5db Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 31 May 2016 18:53:29 +0200 Subject: [PATCH 2/2] Add test to prevent future regressions License: MIT Signed-off-by: Jakub Sztandera --- .../t0500-issues-and-regressions-offline.sh | 11 ++++++++++ .../t0501-issues-and-regressions-online.sh | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 test/sharness/t0500-issues-and-regressions-offline.sh create mode 100755 test/sharness/t0501-issues-and-regressions-online.sh diff --git a/test/sharness/t0500-issues-and-regressions-offline.sh b/test/sharness/t0500-issues-and-regressions-offline.sh new file mode 100755 index 00000000000..596448d7211 --- /dev/null +++ b/test/sharness/t0500-issues-and-regressions-offline.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +test_description="Tests for various fxed issues and regressions." + +. lib/test-lib.sh + +test_init_ipfs + +# Tests go here + +test_done diff --git a/test/sharness/t0501-issues-and-regressions-online.sh b/test/sharness/t0501-issues-and-regressions-online.sh new file mode 100755 index 00000000000..1b7471a6fb8 --- /dev/null +++ b/test/sharness/t0501-issues-and-regressions-online.sh @@ -0,0 +1,20 @@ +#!/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 +