From aac25b8d514e0e2ade9667fdf5bb11673840712a Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 24 Sep 2016 15:21:37 +0200 Subject: [PATCH] Add json output to show command --- cmd/show.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cmd/show.go b/cmd/show.go index 632c522..9249d15 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -1,6 +1,7 @@ package cmd import ( + "encoding/json" "fmt" "os" "text/template" @@ -8,6 +9,7 @@ import ( "github.com/Luzifer/worktime/schema" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // showCmd represents the show command @@ -34,6 +36,13 @@ var showCmd = &cobra.Command{ return err } + if viper.GetBool("json") { + return json.NewEncoder(os.Stdout).Encode(struct { + Day schema.Day `json:"day"` + Overtime float64 `json:"overtime"` + }{Day: *doc, Overtime: overtime.Value}) + } + tplSrc, err := Asset("templates/show.tpl") if err != nil { return err @@ -55,6 +64,9 @@ var showCmd = &cobra.Command{ func init() { RootCmd.AddCommand(showCmd) + showCmd.Flags().Bool("json", false, "Prints day in JSON instead of human readable text") + viper.BindPFlag("json", showCmd.Flags().Lookup("json")) + // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command