1
0
Fork 0
mirror of https://github.com/Luzifer/worktime.git synced 2024-12-22 22:11:16 +00:00

Add json output to show command

This commit is contained in:
Knut Ahlers 2016-09-24 15:21:37 +02:00
parent fdd1331cc4
commit aac25b8d51
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"text/template" "text/template"
@ -8,6 +9,7 @@ import (
"github.com/Luzifer/worktime/schema" "github.com/Luzifer/worktime/schema"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
// showCmd represents the show command // showCmd represents the show command
@ -34,6 +36,13 @@ var showCmd = &cobra.Command{
return err 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") tplSrc, err := Asset("templates/show.tpl")
if err != nil { if err != nil {
return err return err
@ -55,6 +64,9 @@ var showCmd = &cobra.Command{
func init() { func init() {
RootCmd.AddCommand(showCmd) 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. // Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command // Cobra supports Persistent Flags which will work for this command