From c71907033e7e1dbe25917cb88993b4477e481e2f Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Wed, 1 Aug 2018 14:43:19 +0200 Subject: [PATCH] add --json flag to stats command --- cmd/fathom/stats.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/fathom/stats.go b/cmd/fathom/stats.go index 1a3a6bb..4b67b1e 100644 --- a/cmd/fathom/stats.go +++ b/cmd/fathom/stats.go @@ -1,9 +1,11 @@ package main import ( + "encoding/json" "errors" "fmt" "github.com/urfave/cli" + "os" "time" ) @@ -20,6 +22,10 @@ var statsCmd = cli.Command{ Name: "end-date", Usage: "end date, expects a date in format 2006-01-02", }, + cli.BoolFlag{ + Name: "json", + Usage: "get a json response", + }, }, } @@ -39,6 +45,14 @@ func stats(c *cli.Context) error { return err } + if c.Bool("json") { + if err := json.NewEncoder(os.Stdout).Encode(result); err != nil { + return err + } + + return nil + } + fmt.Printf("%s - %s\n", start.Format("Jan 01, 2006"), end.Format("Jan 01, 2006")) fmt.Printf("===========================\n") fmt.Printf("Visitors: \t%d\n", result.Visitors)