-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (123 loc) · 3.81 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
145
146
147
package main
import (
"JiraWorklogsImporter/importer"
"JiraWorklogsImporter/jira"
"JiraWorklogsImporter/toggl"
"bufio"
"flag"
"fmt"
"github.com/joho/godotenv"
"os"
"strings"
"text/tabwriter"
)
func main() {
var project string
var records [][]string
var csvFilePathToImport string
var since string
var until string
var nonInteractive bool
flag.StringVar(&project, "project", "", "A project name that will load .env.<project-name> file.")
flag.StringVar(&csvFilePathToImport, "import", "", "CSV file path to import.")
flag.StringVar(&since, "since", "", "Import work logs since date. Format YYYY-MM-DD.")
flag.StringVar(&until, "until", "", "Import work logs until date. Format YYYY-MM-DD.")
flag.BoolVar(&nonInteractive, "non-interactive", false, "Non-interactive mode.")
flag.BoolVar(&nonInteractive, "n", false, "An alias of --non-interactive.")
flag.Parse()
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file.")
return
}
if project != "" {
projectEnv := fmt.Sprintf(".env.%s", project)
err = godotenv.Load(projectEnv)
if err != nil {
fmt.Printf("Error loading %s file.\n", projectEnv)
return
}
}
optionsValidationFailed := false
if since == "" {
fmt.Println("Missing since option.")
optionsValidationFailed = true
} else if !toggl.CheckDateFormat(since) {
fmt.Println("Invalid since option. The date must be in YYYY-MM-DD format.")
optionsValidationFailed = true
}
if until == "" {
fmt.Println("Missing until option.")
optionsValidationFailed = true
} else if !toggl.CheckDateFormat(until) {
fmt.Println("Invalid until option. The date must be in YYYY-MM-DD format.")
optionsValidationFailed = true
}
if optionsValidationFailed {
return
}
atlassianDomain := os.Getenv("ATLASSIAN_DOMAIN")
atlassianEmail := os.Getenv("ATLASSIAN_EMAIL")
atlassianApiToken := os.Getenv("ATLASSIAN_API_TOKEN")
togglApiToken := os.Getenv("TOGGL_API_TOKEN")
togglUserId := os.Getenv("TOGGL_USER_ID")
togglClientId := os.Getenv("TOGGL_CLIENT_ID")
togglWorkspaceId := os.Getenv("TOGGL_WORKSPACE_ID")
descriptionRegex, exists := os.LookupEnv("DESCRIPTION_REGEX")
if !exists {
descriptionRegex = `^(.*?)\s*(?:\((.*?)\))?$`
}
if csvFilePathToImport != "" {
records, err = importer.ReadCSVFile(csvFilePathToImport)
if err != nil {
fmt.Println("Error reading CSV file:", err)
return
}
} else {
records, err = toggl.ExportWorkLogs(togglApiToken, togglUserId, togglClientId, togglWorkspaceId, since, until)
}
tableWriter := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
for _, record := range records {
fmt.Fprintln(tableWriter, strings.Join(record, "\t"))
}
tableWriter.Flush()
if nonInteractive == false {
fmt.Print("Please confirm the import [y/N]: ")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occurred while reading the input. Please try again.", err)
return
}
input = strings.TrimSpace(strings.ToLower(input))
confirmed := input == "y"
if confirmed == false {
return
}
}
for recordNo, record := range records {
// Skip headers
if recordNo == 0 {
continue
}
description := record[5]
durationString := record[11]
startedAtDateTime, err := toggl.ConvertDateFormat(record[7] + " " + record[8])
if err != nil {
fmt.Println(fmt.Sprintln(err))
continue
}
issueIdOrKey, contentText, err := toggl.ConvertToIssueIdAndContextText(description, descriptionRegex)
if err != nil {
fmt.Println(fmt.Sprintln(err))
continue
}
timeSpentSeconds, err := toggl.ConvertToSeconds(durationString)
if err != nil {
fmt.Println(fmt.Sprintln(err))
continue
}
jira.ImportWorkLog(atlassianDomain, atlassianEmail, atlassianApiToken, issueIdOrKey, contentText, startedAtDateTime, timeSpentSeconds, recordNo)
}
tableWriter.Flush()
}