From ad8253ff1889e7dc8d5145a74ee394b4ed540543 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 19 Dec 2021 20:09:19 +0100 Subject: [PATCH] add multi params with try --- internal/piwigocli/method.go | 2 +- internal/piwigocli/method_try.go | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/internal/piwigocli/method.go b/internal/piwigocli/method.go index 37ed616..7b304aa 100644 --- a/internal/piwigocli/method.go +++ b/internal/piwigocli/method.go @@ -3,7 +3,7 @@ package piwigocli type MethodGroup struct { List MethodListCommand `command:"list" description:"List of available methods"` Details MethodDetailsCommand `command:"details" description:"Details of a method"` - Try MethodTryCommand `command:"try" description:"Test a method"` + Try MethodTryCommand `command:"try" description:"Test a method. Parameters after the command as k=v, can be repeated like k=v1 k=v2."` } var methodGroup MethodGroup diff --git a/internal/piwigocli/method_try.go b/internal/piwigocli/method_try.go index d211de4..138783f 100644 --- a/internal/piwigocli/method_try.go +++ b/internal/piwigocli/method_try.go @@ -1,14 +1,15 @@ package piwigocli import ( + "errors" "net/url" + "strings" "github.com/celogeek/piwigo-cli/internal/piwigo" ) type MethodTryCommand struct { - MethodName string `short:"m" long:"method-name" description:"Method name to test"` - MethodParams url.Values `short:"p" long:"params" description:"Parameter for the method" env-delim:","` + MethodName string `short:"m" long:"method-name" description:"Method name to test"` } func (c *MethodTryCommand) Execute(args []string) error { @@ -23,13 +24,21 @@ func (c *MethodTryCommand) Execute(args []string) error { } var result map[string]interface{} + params := &url.Values{} + for _, arg := range args { + r := strings.SplitN(arg, "=", 2) + if len(r) != 2 { + return errors.New("args should be key=value") + } + params.Add(r[0], r[1]) + } - if err := p.Post(c.MethodName, &c.MethodParams, &result); err != nil { + if err := p.Post(c.MethodName, params, &result); err != nil { + piwigo.DumpResponse(params) return err } piwigo.DumpResponse(result) - piwigo.DumpResponse(c.MethodParams) - + piwigo.DumpResponse(params) return nil }