-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (127 loc) · 3.39 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"log"
"os"
"time"
"godep.io/timemate/pkg/sync"
"godep.io/timemate/pkg/trackers"
"godep.io/timemate/pkg/utils"
"github.com/spf13/viper"
"github.com/urfave/cli"
)
func init() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/.toggl-sync")
viper.AddConfigPath("./config")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %w \n", err))
}
}
func updateEntries(trackerSvc trackers.TrackerApi, syncSvc sync.SyncApi, projects []interface{}, start time.Time, end time.Time) error {
log.Printf("Update entries for duration %s\n", end.Sub(start))
entries, err := trackerSvc.GetTimeEntries(start, end)
if err != nil {
return err
}
return syncSvc.Sync(sync.GroupByTask(projects, entries))
}
var (
// Populated by goreleaser during build
version = "master"
commit = "?"
date = ""
)
func main() {
app := cli.NewApp()
app.Version = fmt.Sprintf("%s (%s; %s)", version, commit, date)
app.Description = "Tiny service to sync time entries from toggl to jira"
app.Name = "toggl-sync"
app.Copyright = "TimeMate © 2021-2023"
app.Commands = []cli.Command{
{
Name: "sync",
Usage: "Sync time entries from timers with task trackers",
Flags: []cli.Flag{
cli.StringFlag{
Name: "period",
},
cli.BoolFlag{
Name: "service",
},
},
Action: func(c *cli.Context) error {
service := c.Bool("service")
t := viper.GetString("period.timeframe")
p := c.String("period")
if p != "" {
t = p
}
lookupTimeframe, err := utils.ParseDuration(t)
if err != nil {
log.Fatal(err)
}
e := viper.GetString("period.every")
if e == "" {
e = t
}
every, err := utils.ParseDuration(e)
if err != nil {
log.Fatal(err)
}
log.Printf("Sync started with config period: %s, every: %s", lookupTimeframe, every)
end := time.Now()
start := end.Add(-lookupTimeframe)
tr := viper.Get("tracker").([]interface{})
trackerSvc, err := trackers.NewTogglTracker((tr[0]).(map[interface{}]interface{}))
if err != nil {
log.Fatal(err)
}
sn := viper.Get("sync").([]interface{})
jiraConf := (sn[0]).(map[interface{}]interface{})
syncSvc, err := sync.NewJiraSync(jiraConf)
if err != nil {
log.Fatal(err)
}
err = updateEntries(trackerSvc, syncSvc, (jiraConf["projects"]).([]interface{}), start, end)
if err != nil {
log.Fatal(err)
}
lastRun := time.Now().Format("2006-01-02T15:04:05-0700")
if service {
ticker := time.NewTicker(time.Second)
quit := make(chan error, 1)
go func() {
for {
select {
case <-ticker.C:
nowFormatted := time.Now().Format("2006-01-02T15:04:05-0700")
now, err := time.Parse("2006-01-02T15:04:05-0700", nowFormatted)
last, err := time.Parse("2006-01-02T15:04:05-0700", lastRun)
if now.Sub(last).Seconds() > every.Seconds() {
err = updateEntries(trackerSvc, syncSvc, (jiraConf["projects"]).([]interface{}), start, end)
lastRun = time.Now().Format("2006-01-02T15:04:05-0700")
}
if err != nil {
quit <- err
}
}
}
}()
err = <-quit
log.Printf("%v", err)
log.Println("Quiting process")
ticker.Stop()
}
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}