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
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
@ -54,9 +57,26 @@ func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error {
Result: resp,
}
err = json.NewDecoder(r.Body).Decode(&Result)
if err != nil {
return err
if os.Getenv("DEBUG") == "1" {
newBody := &bytes.Buffer{}
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" {

View File

@ -1,6 +1,7 @@
package piwigocli
import (
"net/url"
"os"
"github.com/celogeek/piwigo-cli/internal/piwigo"
@ -29,7 +30,7 @@ func (c *GetInfosCommand) Execute(args []string) error {
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
}
@ -48,5 +49,4 @@ func (c *GetInfosCommand) Execute(args []string) error {
func init() {
parser.AddCommand("getinfos", "Get general information", "", &getInfosCommand)
}

View File

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

View File

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

View File

@ -17,14 +17,14 @@ type StatusResponse struct {
}
func (c *StatusCommand) Execute(args []string) error {
Piwigo := piwigo.Piwigo{}
if err := Piwigo.LoadConfig(); err != nil {
p := piwigo.Piwigo{}
if err := p.LoadConfig(); err != nil {
return err
}
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
}