zeit/z/exportCmd.go

113 lines
2.7 KiB
Go
Raw Permalink Normal View History

2020-10-17 13:59:44 +00:00
package z
import (
"os"
"fmt"
"time"
2020-10-17 14:50:21 +00:00
"encoding/json"
2022-04-01 21:46:21 +00:00
"github.com/jinzhu/now"
2020-10-17 13:59:44 +00:00
"github.com/spf13/cobra"
)
2020-10-17 14:50:21 +00:00
func exportZeitJson(user string, entries []Entry) (string, error) {
stringified, err := json.Marshal(entries)
if err != nil {
return "", err
}
2020-10-17 13:59:44 +00:00
2020-10-17 14:50:21 +00:00
return string(stringified), nil
}
2020-10-17 13:59:44 +00:00
2020-10-17 14:50:21 +00:00
func exportTymeJson(user string, entries []Entry) (string, error) {
tyme := Tyme{}
err := tyme.FromEntries(entries)
if err != nil {
return "", err
}
return tyme.Stringify(), nil
2020-10-17 13:59:44 +00:00
}
var exportCmd = &cobra.Command{
Use: "export ([flags])",
Short: "Export tracked activities",
Long: "Export tracked activities to various formats.",
// Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var entries []Entry
var err error
user := GetCurrentUser()
entries, err = database.ListEntries(user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
var sinceTime time.Time
var untilTime time.Time
if since != "" {
2022-04-01 21:46:21 +00:00
sinceTime, err = now.Parse(since)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
if until != "" {
2022-04-01 21:46:21 +00:00
untilTime, err = now.Parse(until)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
2020-10-17 14:50:21 +00:00
var filteredEntries []Entry
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
2020-10-17 13:59:44 +00:00
var output string = ""
2020-10-17 14:50:21 +00:00
switch(format) {
case "zeit":
output, err = exportZeitJson(user, filteredEntries)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
case "tyme":
output, err = exportTymeJson(user, filteredEntries)
2020-10-17 13:59:44 +00:00
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
2020-10-17 14:50:21 +00:00
default:
2020-10-17 13:59:44 +00:00
fmt.Printf("%s specify an export format; see `zeit export --help` for more info\n", CharError)
os.Exit(1)
}
fmt.Printf("%s\n", output)
return
},
}
func init() {
rootCmd.AddCommand(exportCmd)
exportCmd.Flags().StringVar(&format, "format", "zeit", "Format to export, possible values: zeit, tyme")
exportCmd.Flags().StringVar(&since, "since", "", "Date/time to start the export from")
exportCmd.Flags().StringVar(&until, "until", "", "Date/time to export until")
exportCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be exported")
exportCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be exported")
2020-10-17 13:59:44 +00:00
var err error
database, err = InitDatabase()
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}