Add -verbose option to enable logging
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2018-08-18 11:58:57 +02:00
parent c23802216e
commit c10bf83c41
Signed by: peter
GPG Key ID: 374322D56E5209E8
2 changed files with 15 additions and 6 deletions

View File

@ -19,6 +19,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"log" "log"
"net/url" "net/url"
@ -29,6 +30,10 @@ import (
"p83.nl/go/ekster/pkg/microsub" "p83.nl/go/ekster/pkg/microsub"
) )
var (
verbose = flag.Bool("verbose", false, "show verbose logging")
)
func init() { func init() {
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime) log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
} }
@ -102,6 +107,8 @@ func loadEndpoints(c *client.Client, me *url.URL, filename string) error {
} }
func main() { func main() {
flag.Parse()
configDir := fmt.Sprintf("%s/.config/microsub", os.Getenv("HOME")) configDir := fmt.Sprintf("%s/.config/microsub", os.Getenv("HOME"))
if len(os.Args) == 3 && os.Args[1] == "connect" { if len(os.Args) == 3 && os.Args[1] == "connect" {
@ -156,7 +163,9 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
performCommands(&c, os.Args[1:]) c.Logging = *verbose
performCommands(&c, flag.Args())
} }
func performCommands(sub microsub.Microsub, commands []string) { func performCommands(sub microsub.Microsub, commands []string) {

View File

@ -17,7 +17,7 @@ type Client struct {
MicrosubEndpoint *url.URL MicrosubEndpoint *url.URL
Token string Token string
logging bool Logging bool
} }
func (c *Client) microsubGetRequest(action string, args map[string]string) (*http.Response, error) { func (c *Client) microsubGetRequest(action string, args map[string]string) (*http.Response, error) {
@ -38,14 +38,14 @@ func (c *Client) microsubGetRequest(action string, args map[string]string) (*htt
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token)) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
if c.logging { if c.Logging {
x, _ := httputil.DumpRequestOut(req, true) x, _ := httputil.DumpRequestOut(req, true)
log.Printf("REQUEST:\n\n%s\n\n", x) log.Printf("REQUEST:\n\n%s\n\n", x)
} }
res, err := client.Do(req) res, err := client.Do(req)
if c.logging { if c.Logging {
x, _ := httputil.DumpResponse(res, true) x, _ := httputil.DumpResponse(res, true)
log.Printf("RESPONSE:\n\n%s\n\n", x) log.Printf("RESPONSE:\n\n%s\n\n", x)
} }
@ -71,14 +71,14 @@ func (c *Client) microsubPostRequest(action string, args map[string]string) (*ht
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token)) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
if c.logging { if c.Logging {
x, _ := httputil.DumpRequestOut(req, true) x, _ := httputil.DumpRequestOut(req, true)
log.Printf("REQUEST:\n\n%s\n\n", x) log.Printf("REQUEST:\n\n%s\n\n", x)
} }
res, err := client.Do(req) res, err := client.Do(req)
if c.logging { if c.Logging {
x, _ := httputil.DumpResponse(res, true) x, _ := httputil.DumpResponse(res, true)
log.Printf("RESPONSE:\n\n%s\n\n", x) log.Printf("RESPONSE:\n\n%s\n\n", x)
} }