2020-10-17 13:59:44 +00:00
|
|
|
package z
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"fmt"
|
2020-10-17 14:32:41 +00:00
|
|
|
"time"
|
2020-10-17 14:50:21 +00:00
|
|
|
"encoding/json"
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-17 19:06:25 +00:00
|
|
|
var sinceTime time.Time
|
|
|
|
var untilTime time.Time
|
2020-10-17 14:32:41 +00:00
|
|
|
|
2020-10-17 19:06:25 +00:00
|
|
|
if since != "" {
|
|
|
|
sinceTime, err = time.Parse(time.RFC3339, since)
|
2020-10-17 14:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s %+v\n", CharError, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 19:06:25 +00:00
|
|
|
if until != "" {
|
|
|
|
untilTime, err = time.Parse(time.RFC3339, until)
|
2020-10-17 14:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s %+v\n", CharError, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:50:21 +00:00
|
|
|
var filteredEntries []Entry
|
2020-10-17 19:06:25 +00:00
|
|
|
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
|
2020-10-17 14:32:41 +00:00
|
|
|
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":
|
2020-10-17 14:32:41 +00:00
|
|
|
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)
|
2021-02-07 18:47:31 +00:00
|
|
|
exportCmd.Flags().StringVar(&format, "format", "zeit", "Format to export, possible values: zeit, tyme")
|
2020-10-17 19:06:25 +00:00
|
|
|
exportCmd.Flags().StringVar(&since, "since", "", "Date/time to start the export from")
|
|
|
|
exportCmd.Flags().StringVar(&until, "until", "", "Date/time to export until")
|
2020-10-17 14:32:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|