add --json flag to stats command

This commit is contained in:
Danny van Kooten 2018-08-01 14:43:19 +02:00
parent 01474759c6
commit c71907033e
1 changed files with 14 additions and 0 deletions

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/urfave/cli" "github.com/urfave/cli"
"os"
"time" "time"
) )
@ -20,6 +22,10 @@ var statsCmd = cli.Command{
Name: "end-date", Name: "end-date",
Usage: "end date, expects a date in format 2006-01-02", 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 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("%s - %s\n", start.Format("Jan 01, 2006"), end.Format("Jan 01, 2006"))
fmt.Printf("===========================\n") fmt.Printf("===========================\n")
fmt.Printf("Visitors: \t%d\n", result.Visitors) fmt.Printf("Visitors: \t%d\n", result.Visitors)