Implemented GetFilteredEntries and extended export command

This commit is contained in:
マリウス 2020-10-17 15:32:41 +01:00
parent 64a17e4db9
commit df06d63fa8
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F
4 changed files with 87 additions and 6 deletions

View file

@ -169,7 +169,7 @@ The following formats are supported as of right now:
#### Tyme 3 JSON #### Tyme 3 JSON
It's possible to import JSON exports from [Tyme 3](https://www.tyme-app.com). It is possible to import JSON exports from [Tyme 3](https://www.tyme-app.com).
It is important that the JSON is exported with the following options set/unset: It is important that the JSON is exported with the following options set/unset:
![Tyme 3 JSON export](documentation/tyme3json.png) ![Tyme 3 JSON export](documentation/tyme3json.png)
@ -194,3 +194,25 @@ Import a Tyme 3 JSON export:
```sh ```sh
zeit import --tyme ./tyme.export.json zeit import --tyme ./tyme.export.json
``` ```
### Export tracked activities
```sh
zeit export --help
```
The following formats are supported as of right now:
#### Tyme 3 JSON
It is possible to export JSON compatible to the Tyme 3 JSON format. Fields that
are not available in *zeit* will be filled with dummy values, e.g.
`Billing: "UNBILLED"`.
#### Examples:
Export a Tyme 3 JSON:
```sh
zeit export --tyme --project "my project" --since "2020-04-01T15:04:05+07:00" --until "2020-04-04T15:04:05+07:00"
```

View file

@ -153,3 +153,29 @@ func (entry *Entry) GetOutput() (string) {
return fmt.Sprintf("%s %s on %s from %s to %s (%sh)", color.FgGray.Render(entry.ID), color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(entry.Finish.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(trackDiffOut.Format("15:04"))) return fmt.Sprintf("%s %s on %s from %s to %s (%sh)", color.FgGray.Render(entry.ID), color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(entry.Finish.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(trackDiffOut.Format("15:04")))
} }
func GetFilteredEntries(entries *[]Entry, project string, task string, since time.Time, until time.Time) ([]*Entry, error) {
var filteredEntries []*Entry
for _, entry := range *entries {
if project != "" && GetIdFromName(entry.Project) != GetIdFromName(project) {
continue
}
if task != "" && GetIdFromName(entry.Task) != GetIdFromName(task) {
continue
}
if since.IsZero() == false && since.Before(entry.Begin) == false && since.Equal(entry.Begin) == false {
continue
}
if until.IsZero() == false && until.After(entry.Finish) == false && until.Equal(entry.Finish) == false {
continue
}
filteredEntries = append(filteredEntries, &entry)
}
return filteredEntries, nil
}

View file

@ -3,11 +3,14 @@ package z
import ( import (
"os" "os"
"fmt" "fmt"
// "time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func exportTymeJson(user string, entries []Entry) (string, error) { var exportSince string
var exportUntil string
func exportTymeJson(user string, entries []*Entry) (string, error) {
tyme := Tyme{} tyme := Tyme{}
err := tyme.FromEntries(entries) err := tyme.FromEntries(entries)
@ -35,9 +38,35 @@ var exportCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
var since time.Time
var until time.Time
if exportSince != "" {
since, err = time.Parse(time.RFC3339, exportSince)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
if exportUntil != "" {
until, err = time.Parse(time.RFC3339, exportUntil)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
var filteredEntries []*Entry
filteredEntries, err = GetFilteredEntries(&entries, project, task, since, until)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
var output string = "" var output string = ""
if formatTymeJson == true { if formatTymeJson == true {
output, err = exportTymeJson(user, entries) output, err = exportTymeJson(user, filteredEntries)
if err != nil { if err != nil {
fmt.Printf("%s %+v\n", CharError, err) fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1) os.Exit(1)
@ -56,6 +85,10 @@ var exportCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(exportCmd) rootCmd.AddCommand(exportCmd)
exportCmd.Flags().BoolVar(&formatTymeJson, "tyme", false, "Export to Tyme 3 JSON") exportCmd.Flags().BoolVar(&formatTymeJson, "tyme", false, "Export to Tyme 3 JSON")
exportCmd.Flags().StringVar(&exportSince, "since", "", "Date/time to start the export from")
exportCmd.Flags().StringVar(&exportUntil, "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")
var err error var err error
database, err = InitDatabase() database, err = InitDatabase()

View file

@ -5,7 +5,7 @@ import (
// "fmt" // "fmt"
"os" "os"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
// "time" "time"
) )
var formatTymeJson bool var formatTymeJson bool
@ -50,7 +50,7 @@ func (tyme *Tyme) Load(filename string) error {
return nil return nil
} }
func (tyme *Tyme) FromEntries(entries []Entry) error { func (tyme *Tyme) FromEntries(entries []*Entry) error {
for _, entry := range entries { for _, entry := range entries {
duration := decimal.NewFromFloat(entry.Finish.Sub(entry.Begin).Minutes()) duration := decimal.NewFromFloat(entry.Finish.Sub(entry.Begin).Minutes())