-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
178 lines (152 loc) · 4.2 KB
/
Copy pathmain.go
File metadata and controls
178 lines (152 loc) · 4.2 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"JiraWorklogsImporter/clockify"
"JiraWorklogsImporter/converter"
"JiraWorklogsImporter/importer"
"JiraWorklogsImporter/jira"
"JiraWorklogsImporter/toggl"
"bufio"
"flag"
"fmt"
"github.com/joho/godotenv"
"os"
"strings"
"text/tabwriter"
"time"
)
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 !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 !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")
importStrategy, exists := os.LookupEnv("IMPORT_STRATEGY")
if !exists {
importStrategy = "csv_to_jira"
}
if importStrategy == "csv_to_jira" {
if csvFilePathToImport == "" {
fmt.Println("The CSV file is not provided. Use --import option.")
return
}
records, err = importer.ReadCSVFile(csvFilePathToImport)
if err != nil {
fmt.Println("Error reading CSV file:", err)
return
}
} else if importStrategy == "toggl_to_jira" {
records, err = toggl.ExportWorkLogs(
os.Getenv("TOGGL_API_TOKEN"),
os.Getenv("TOGGL_USER_ID"),
os.Getenv("TOGGL_CLIENT_ID"),
os.Getenv("TOGGL_WORKSPACE_ID"),
since,
until,
)
} else if importStrategy == "clockify_to_jira" {
records, err = clockify.ExportWorkLogs(
os.Getenv("CLOCKIFY_API_TOKEN"),
os.Getenv("CLOCKIFY_USER_ID"),
os.Getenv("CLOCKIFY_PROJECT_ID"),
os.Getenv("CLOCKIFY_WORKSPACE_ID"),
since,
until,
)
} else {
fmt.Println("The given import strategy is not supported.")
return
}
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
}
}
factory := converter.NewConverterFactory()
supportedConverter, err := factory.GetConverter(importStrategy)
if err != nil {
fmt.Println(err)
return
}
for recordNo, record := range records {
// Skip headers
if recordNo == 0 {
continue
}
convertedRecord, err := supportedConverter.Convert(record)
if err != nil {
fmt.Println(err)
continue
}
jira.ImportWorkLog(
atlassianDomain,
atlassianEmail,
atlassianApiToken,
convertedRecord.IssueIdOrKey,
convertedRecord.ContentText,
convertedRecord.StartedAtDateTime,
convertedRecord.TimeSpentSeconds,
recordNo,
)
}
tableWriter.Flush()
}
func checkDateFormat(date string) bool {
_, err := time.Parse("2006-01-02", date)
return err == nil
}