debug output

This commit is contained in:
Celogeek 2021-12-15 22:30:09 +01:00
parent 29880be5b6
commit 025991160b
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
5 changed files with 36 additions and 15 deletions

View File

@ -1,10 +1,13 @@
package piwigo package piwigo
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strings" "strings"
) )
@ -54,9 +57,26 @@ func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error {
Result: resp, Result: resp,
} }
err = json.NewDecoder(r.Body).Decode(&Result) if os.Getenv("DEBUG") == "1" {
if err != nil { newBody := &bytes.Buffer{}
return err tee := io.TeeReader(r.Body, newBody)
var RawResult map[string]interface{}
err = json.NewDecoder(tee).Decode(&RawResult)
if err != nil {
return err
}
DumpResponse(RawResult)
err = json.NewDecoder(newBody).Decode(&Result)
if err != nil {
return err
}
} else {
err = json.NewDecoder(r.Body).Decode(&Result)
if err != nil {
return err
}
} }
if Result.Stat != "ok" { if Result.Stat != "ok" {

View File

@ -1,6 +1,7 @@
package piwigocli package piwigocli
import ( import (
"net/url"
"os" "os"
"github.com/celogeek/piwigo-cli/internal/piwigo" "github.com/celogeek/piwigo-cli/internal/piwigo"
@ -29,7 +30,7 @@ func (c *GetInfosCommand) Execute(args []string) error {
var resp GetInfosResponse var resp GetInfosResponse
if err := p.Post("pwg.getInfos", nil, &resp); err != nil { if err := p.Post("pwg.getInfos", &url.Values{}, &resp); err != nil {
return err return err
} }
@ -48,5 +49,4 @@ func (c *GetInfosCommand) Execute(args []string) error {
func init() { func init() {
parser.AddCommand("getinfos", "Get general information", "", &getInfosCommand) parser.AddCommand("getinfos", "Get general information", "", &getInfosCommand)
} }

View File

@ -6,7 +6,8 @@ import (
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
) )
type Options struct{} type Options struct {
}
var options Options var options Options

View File

@ -8,21 +8,21 @@ import (
) )
type LoginCommand struct { type LoginCommand struct {
Url string `short:"u" long:"url" description:"Url of the instance"` Url string `short:"u" long:"url" description:"Url of the instance" required:"true"`
Login string `short:"l" long:"login" description:"Login"` Login string `short:"l" long:"login" description:"Login" required:"true"`
Password string `short:"p" long:"password" description:"Password"` Password string `short:"p" long:"password" description:"Password" required:"true"`
} }
func (c *LoginCommand) Execute(args []string) error { func (c *LoginCommand) Execute(args []string) error {
fmt.Printf("Login on %s...\n", c.Url) fmt.Printf("Login on %s...\n", c.Url)
Piwigo := piwigo.Piwigo{ p := piwigo.Piwigo{
Url: c.Url, Url: c.Url,
} }
result := false result := false
err := Piwigo.Post("pwg.session.login", &url.Values{ err := p.Post("pwg.session.login", &url.Values{
"username": []string{c.Login}, "username": []string{c.Login},
"password": []string{c.Password}, "password": []string{c.Password},
}, &result) }, &result)
@ -30,7 +30,7 @@ func (c *LoginCommand) Execute(args []string) error {
return err return err
} }
err = Piwigo.SaveConfig() err = p.SaveConfig()
if err != nil { if err != nil {
return err return err
} }

View File

@ -17,14 +17,14 @@ type StatusResponse struct {
} }
func (c *StatusCommand) Execute(args []string) error { func (c *StatusCommand) Execute(args []string) error {
Piwigo := piwigo.Piwigo{} p := piwigo.Piwigo{}
if err := Piwigo.LoadConfig(); err != nil { if err := p.LoadConfig(); err != nil {
return err return err
} }
resp := &StatusResponse{} resp := &StatusResponse{}
if err := Piwigo.Post("pwg.session.getStatus", nil, &resp); err != nil { if err := p.Post("pwg.session.getStatus", nil, &resp); err != nil {
return err return err
} }