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