2016-09-24 12:59:16 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2016-09-24 13:21:37 +00:00
|
|
|
"encoding/json"
|
2016-09-24 12:59:16 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Luzifer/worktime/schema"
|
|
|
|
"github.com/spf13/cobra"
|
2016-09-24 13:21:37 +00:00
|
|
|
"github.com/spf13/viper"
|
2016-09-24 12:59:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 := time.Parse("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
|
|
|
|
}
|
|
|
|
|
2016-09-24 13:21:37 +00:00
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2016-09-24 12:59:16 +00:00
|
|
|
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)
|
|
|
|
|
2016-09-24 13:21:37 +00:00
|
|
|
showCmd.Flags().Bool("json", false, "Prints day in JSON instead of human readable text")
|
|
|
|
viper.BindPFlag("json", showCmd.Flags().Lookup("json"))
|
|
|
|
|
2016-09-24 12:59:16 +00:00
|
|
|
// 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")
|
|
|
|
|
|
|
|
}
|