global for verbose

This commit is contained in:
Aaron Bieber 2020-07-31 16:13:16 -06:00
parent 31e4f5317b
commit 127203781b

20
main.go
View File

@ -14,6 +14,8 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
var verbose bool
// Push represents a message sent to the Pushover api // Push represents a message sent to the Pushover api
type Push struct { type Push struct {
Token string `json:"token"` Token string `json:"token"`
@ -36,8 +38,8 @@ type PushResponse struct {
Errors []string `json:"errors,omitempty"` Errors []string `json:"errors,omitempty"`
} }
func msg(msg interface{}, quiet *bool) { func msg(msg interface{}) {
if !*quiet { if verbose {
fmt.Println(msg) fmt.Println(msg)
} }
} }
@ -55,7 +57,7 @@ func main() {
var url = flag.String("url", "", "url to send") var url = flag.String("url", "", "url to send")
var priority = flag.Int("pri", 0, "priority of message") var priority = flag.Int("pri", 0, "priority of message")
var sound = flag.String("sound", "pushover", "sound") var sound = flag.String("sound", "pushover", "sound")
var verbose = flag.Bool("v", false, "verbose") flag.BoolVar(&verbose, "v", false, "verbose")
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
flag.Parse() flag.Parse()
@ -77,13 +79,13 @@ func main() {
} }
if err := json.NewEncoder(buf).Encode(push); err != nil { if err := json.NewEncoder(buf).Encode(push); err != nil {
msg(err, verbose) msg(err)
os.Exit(1) os.Exit(1)
} }
req, err = http.NewRequest("POST", pushURL, buf) req, err = http.NewRequest("POST", pushURL, buf)
if err != nil { 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) os.Exit(1)
} }
@ -91,7 +93,7 @@ func main() {
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { 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) os.Exit(1)
} }
@ -100,14 +102,14 @@ func main() {
var resBody PushResponse var resBody PushResponse
if err = json.NewDecoder(res.Body).Decode(&resBody); err != nil { if err = json.NewDecoder(res.Body).Decode(&resBody); err != nil {
if err != nil { if err != nil {
msg(err, verbose) msg(err)
os.Exit(1) os.Exit(1)
} }
} }
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
defer w.Flush() defer w.Flush()
if len(resBody.Errors) > 0 { if len(resBody.Errors) > 0 {
if *verbose { if verbose {
fmt.Fprintf(w, "Errors:\t%s\n", strings.Join(resBody.Errors, ", ")) fmt.Fprintf(w, "Errors:\t%s\n", strings.Join(resBody.Errors, ", "))
if resBody.User != "" { if resBody.User != "" {
fmt.Fprintf(w, "User:\t%s\n", resBody.User) fmt.Fprintf(w, "User:\t%s\n", resBody.User)
@ -115,7 +117,7 @@ func main() {
} }
os.Exit(1) os.Exit(1)
} else { } else {
if *verbose { if verbose {
fmt.Fprintf(w, "Request:\t%s\n", resBody.Request) fmt.Fprintf(w, "Request:\t%s\n", resBody.Request)
fmt.Fprintf(w, "Status:\t%d\n", resBody.Status) fmt.Fprintf(w, "Status:\t%d\n", resBody.Status)
} }