Implemented export command
This commit is contained in:
parent
47810387dd
commit
1bae202468
3 changed files with 110 additions and 5 deletions
66
z/export.go
Normal file
66
z/export.go
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
package z
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
// "time"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func exportTymeJson(user string, entries []Entry) (string, error) {
|
||||||
|
|
||||||
|
tyme := Tyme{}
|
||||||
|
err := tyme.FromEntries(entries)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tyme.Stringify(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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 output string = ""
|
||||||
|
if formatTymeJson == true {
|
||||||
|
output, err = exportTymeJson(user, entries)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s %+v\n", CharError, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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().BoolVar(&formatTymeJson, "tyme", false, "Export to Tyme 3 JSON")
|
||||||
|
|
||||||
|
var err error
|
||||||
|
database, err = InitDatabase()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s %+v\n", CharError, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,8 +9,6 @@ import (
|
||||||
"github.com/cnf/structhash"
|
"github.com/cnf/structhash"
|
||||||
)
|
)
|
||||||
|
|
||||||
var formatTymeJson bool
|
|
||||||
|
|
||||||
func importTymeJson(user string, file string) ([]Entry, error) {
|
func importTymeJson(user string, file string) ([]Entry, error) {
|
||||||
var entries []Entry
|
var entries []Entry
|
||||||
|
|
||||||
|
@ -53,7 +51,7 @@ func importTymeJson(user string, file string) ([]Entry, error) {
|
||||||
var importCmd = &cobra.Command{
|
var importCmd = &cobra.Command{
|
||||||
Use: "import ([flags]) [file]",
|
Use: "import ([flags]) [file]",
|
||||||
Short: "Import tracked activities",
|
Short: "Import tracked activities",
|
||||||
Long: "Import tracked activities from various sources.",
|
Long: "Import tracked activities from various formats.",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var entries []Entry
|
var entries []Entry
|
||||||
|
|
|
@ -4,15 +4,18 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
// "fmt"
|
// "fmt"
|
||||||
"os"
|
"os"
|
||||||
// "github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
// "time"
|
// "time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var formatTymeJson bool
|
||||||
|
|
||||||
type TymeEntry struct {
|
type TymeEntry struct {
|
||||||
Billing string `json:"billing"` // "UNBILLED",
|
Billing string `json:"billing"` // "UNBILLED",
|
||||||
Category string `json:"category"` // "Client",
|
Category string `json:"category"` // "Client",
|
||||||
Distance string `json:"distance"` // "0",
|
Distance string `json:"distance"` // "0",
|
||||||
Duration string `json:"duration"` // "15",
|
Duration string `json:"duration"` // "15",
|
||||||
|
Start string `json:"start"` // "2020-09-01T08:45:00+01:00",
|
||||||
End string `json:"end"` // "2020-09-01T08:57:00+01:00",
|
End string `json:"end"` // "2020-09-01T08:57:00+01:00",
|
||||||
Note string `json:"note"` // "",
|
Note string `json:"note"` // "",
|
||||||
Project string `json:"project"` // "Project",
|
Project string `json:"project"` // "Project",
|
||||||
|
@ -20,7 +23,6 @@ type TymeEntry struct {
|
||||||
Rate string `json:"rate"` // "140",
|
Rate string `json:"rate"` // "140",
|
||||||
RoundingMethod string `json:"rounding_method"` // "NEAREST",
|
RoundingMethod string `json:"rounding_method"` // "NEAREST",
|
||||||
RoundingMinutes int `json:"rounding_minutes"` // 15,
|
RoundingMinutes int `json:"rounding_minutes"` // 15,
|
||||||
Start string `json:"start"` // "2020-09-01T08:45:00+01:00",
|
|
||||||
Subtask string `json:"subtask"` // "",
|
Subtask string `json:"subtask"` // "",
|
||||||
Sum string `json:"sum"` // "35",
|
Sum string `json:"sum"` // "35",
|
||||||
Task string `json:"task"` // "Development",
|
Task string `json:"task"` // "Development",
|
||||||
|
@ -47,3 +49,42 @@ func (tyme *Tyme) Load(filename string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tyme *Tyme) FromEntries(entries []Entry) error {
|
||||||
|
for _, entry := range entries {
|
||||||
|
duration := decimal.NewFromFloat(entry.Finish.Sub(entry.Begin).Minutes())
|
||||||
|
|
||||||
|
tymeEntry := TymeEntry{
|
||||||
|
Billing: "UNBILLED",
|
||||||
|
Category: "",
|
||||||
|
Distance: "0",
|
||||||
|
Duration: duration.StringFixed(0),
|
||||||
|
Start: entry.Begin.Format("2006-01-02T15:04:05-0700"),
|
||||||
|
End: entry.Finish.Format("2006-01-02T15:04:05-0700"),
|
||||||
|
Note: entry.Notes,
|
||||||
|
Project: entry.Project,
|
||||||
|
Quantity: "0",
|
||||||
|
Rate: "0",
|
||||||
|
RoundingMethod: "NEAREST",
|
||||||
|
RoundingMinutes: 15,
|
||||||
|
Subtask: "",
|
||||||
|
Sum: "0",
|
||||||
|
Task: entry.Task,
|
||||||
|
Type: "timed",
|
||||||
|
User: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
tyme.Data = append(tyme.Data, tymeEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tyme *Tyme) Stringify() string {
|
||||||
|
stringified, err := json.Marshal(tyme)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(stringified)
|
||||||
|
}
|
Loading…
Reference in a new issue