This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
/
show.go
80 lines (63 loc) · 1.88 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cmd
import (
"encoding/json"
"fmt"
"os"
"text/template"
"time"
"github.com/Luzifer/worktime/schema"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// showCmd represents the show command
var showCmd = &cobra.Command{
Use: "show [day]",
Short: "Display a summary of the given / current day",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
args = []string{time.Now().Format("2006-01-02")}
}
day, err := parseTime("2006-01-02", args[0])
if err != nil {
return fmt.Errorf("'day' parameter seems to have a wrong format: %s", err)
}
doc, err := schema.LoadDay(cfg.couchDB, day, false)
if err != nil {
return err
}
overtime, err := schema.GetOvertime(cfg.couchDB, day)
if err != nil {
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
}
tpl, err := template.New("show").Parse(string(tplSrc))
if err != nil {
return err
}
return tpl.Execute(os.Stdout, map[string]interface{}{
"day": doc,
"overtime": time.Duration(overtime.Value * float64(time.Hour)),
})
},
}
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
// and all subcommands, e.g.:
// showCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// showCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}