From 127203781be01b3ea8aea6ff819745d11d5d137c Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Fri, 31 Jul 2020 16:13:16 -0600 Subject: [PATCH] global for verbose --- main.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index cb1fecf..3239550 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,8 @@ import ( "golang.org/x/sys/unix" ) +var verbose bool + // Push represents a message sent to the Pushover api type Push struct { Token string `json:"token"` @@ -36,8 +38,8 @@ type PushResponse struct { Errors []string `json:"errors,omitempty"` } -func msg(msg interface{}, quiet *bool) { - if !*quiet { +func msg(msg interface{}) { + if verbose { fmt.Println(msg) } } @@ -55,7 +57,7 @@ func main() { var url = flag.String("url", "", "url to send") var priority = flag.Int("pri", 0, "priority of message") var sound = flag.String("sound", "pushover", "sound") - var verbose = flag.Bool("v", false, "verbose") + flag.BoolVar(&verbose, "v", false, "verbose") buf := new(bytes.Buffer) flag.Parse() @@ -77,13 +79,13 @@ func main() { } if err := json.NewEncoder(buf).Encode(push); err != nil { - msg(err, verbose) + msg(err) os.Exit(1) } req, err = http.NewRequest("POST", pushURL, buf) if err != nil { - msg(fmt.Sprintf("can't POST: %s\n", err), verbose) + msg(fmt.Sprintf("can't POST: %s\n", err)) os.Exit(1) } @@ -91,7 +93,7 @@ func main() { res, err := client.Do(req) if err != nil { - msg(fmt.Sprintf("can't make request: %s\n", err), verbose) + msg(fmt.Sprintf("can't make request: %s\n", err)) os.Exit(1) } @@ -100,14 +102,14 @@ func main() { var resBody PushResponse if err = json.NewDecoder(res.Body).Decode(&resBody); err != nil { if err != nil { - msg(err, verbose) + msg(err) os.Exit(1) } } w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0) defer w.Flush() if len(resBody.Errors) > 0 { - if *verbose { + if verbose { fmt.Fprintf(w, "Errors:\t%s\n", strings.Join(resBody.Errors, ", ")) if resBody.User != "" { fmt.Fprintf(w, "User:\t%s\n", resBody.User) @@ -115,7 +117,7 @@ func main() { } os.Exit(1) } else { - if *verbose { + if verbose { fmt.Fprintf(w, "Request:\t%s\n", resBody.Request) fmt.Fprintf(w, "Status:\t%d\n", resBody.Status) }