forked from andreaskoch/togglcsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
96 lines (74 loc) · 2.9 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Package main contains the Toggl⥃CSV commandline utility for importing
// and exporting Toggl time records.
package main
import (
"fmt"
"io"
"time"
"github.com/jinzhu/now"
"gopkg.in/alecthomas/kingpin.v2"
)
// exportDateFormat defines the date format for start and end dates of the export command.
const exportDateFormat = "2006-01-02"
func init() {
now.FirstDayMonday = true
}
type togglCli struct {
importerFactory func(apiToken string) CSVImporter
exporterFactory func(apiToken string) CSVExporter
}
// Execute parses the given arguments and performs the selected action.
func (cli *togglCli) Execute(input io.Reader, output, errorOutput io.Writer, args []string) (success bool) {
app := kingpin.New(applicationName, "Toggl⥃CSV is an csv-based import/export utility for Toggl time tracking data (see: https://github.com/andreaskoch/togglcsv)")
app.Version(applicationVersion)
app.Writer(errorOutput)
app.Terminate(func(int) {
return
})
// export
exportCommand := app.Command("export", "Export your Toggl time tracking records as CSV")
exportAPIToken := exportCommand.Arg("token", "The Toggl API token of the source account").Required().String()
exportStartDate := exportCommand.Arg("startdate", "The start date (e.g. \"2006-01-26\")").Required().String()
exportEndDate := exportCommand.Arg("enddate", "The start date (e.g. \"2006-01-26\")").String()
// import
importCommand := app.Command("import", "Import CSV-based time tracking records into Toggl from stdin")
importAPIToken := importCommand.Arg("token", "The Toggl API token of the target account").Required().String()
command, err := app.Parse(args)
if err != nil {
app.Fatalf("%s", err.Error())
}
switch command {
// export
case exportCommand.FullCommand():
// start date (required)
startDate, startDateError := time.Parse(exportDateFormat, *exportStartDate)
if startDateError != nil {
app.Fatalf("Failed to parse the given start date %q. %s", *exportStartDate, startDateError.Error())
}
// end date (optional)
now := time.Now()
endDate := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.UTC) // use current date as the default
if len(*exportEndDate) > 0 {
endDateParsed, endDateError := time.Parse(exportDateFormat, *exportEndDate)
if endDateError != nil {
app.Fatalf("Failed to parse the given end date %q. %s", *exportEndDate, endDateError.Error())
}
endDate = endDateParsed
}
exporter := cli.exporterFactory(*exportAPIToken)
if exportError := exporter.Export(startDate, endDate, output); exportError != nil {
fmt.Fprintf(errorOutput, "Error: %s\n", exportError.Error())
return false
}
return true
// import
case importCommand.FullCommand():
importer := cli.importerFactory(*importAPIToken)
if importError := importer.Import(input); importError != nil {
fmt.Fprintf(errorOutput, "Error: %s\n", importError.Error())
return false
}
return true
}
return false
}