add multi params with try

This commit is contained in:
Celogeek 2021-12-19 20:09:19 +01:00
parent 4e436b96d9
commit ad8253ff18
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 15 additions and 6 deletions

View File

@ -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

View File

@ -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:","`
}
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
}