-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicmanager-export.go
252 lines (209 loc) · 6.44 KB
/
nicmanager-export.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"image/color"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
// Domain struct represents a domain entry from the API
type Domain struct {
Name string `json:"name"`
OrderStatus string `json:"order_status"`
OrderDateTime string `json:"order_datetime"`
RegistrationDateTime string `json:"registration_datetime"`
DeleteDateTime string `json:"delete_datetime"`
}
func main() {
a := app.NewWithID("witte.io.nicmanager-export")
w := a.NewWindow("Nicmanager Exporter") // main app name shown in process list
uiTitle := widget.NewLabel("Nicmanager Exporter")
//TODO: Multiline text der die Verwendung dokumentiert
/*
Nicmanager Credentials
Username [ ] | Password [ ]
Checkbox Inventory cutoff [ ]
Output file [ ]
[ Start ]
-----
Progress
*/
// TODO: Validator in separate Funktionen auslagern
uiCredUsername := widget.NewEntry()
uiCredUsername.SetPlaceHolder("account.user")
uiCredUsername.Validator = validation.NewRegexp("^[a-z0-9_.-]+$", "Darf nicht leer sein")
uiCredPassword := widget.NewPasswordEntry()
uiCredPassword.SetPlaceHolder("supergeheim")
uiCredPassword.Validator = validation.NewRegexp("^.+$", "Darf nicht leer sein")
uiCutoffDate := widget.NewEntry()
uiCutoffDate.SetPlaceHolder("2020-03-01")
//TODO Validation mit Regex plus time.Parse bauen
uiCutoffDate.Validator = validation.NewRegexp("^20[0-9]{2}-[0-9]{2}-[0-9]{2}$", "Datum muss das Format YYYY-MM-DD haben")
uiFilename := widget.NewEntry()
uiFilename.SetPlaceHolder("Export_12345.csv")
uiFilename.Validator = validation.NewRegexp("^[a-zA-Z0-9_ -]+.csv$", "Der Dateiname muss auf .csv enden und die Datei darf noch nicht existieren")
obscureProgress := widget.NewProgressBarInfinite()
obscureProgress.Hide()
statusMessage := canvas.NewText("", theme.TextColor())
statusMessage.Hide()
var uiForm = &widget.Form{
Items: []*widget.FormItem{
{Text: "Benutzer", Widget: uiCredUsername},
{Text: "Passwort", Widget: uiCredPassword},
{Text: "Stichtag", Widget: uiCutoffDate},
{Text: "Zieldatei", Widget: uiFilename},
},
OnSubmit: func() {
// show progressbar
obscureProgress.Show()
// open output file
// TODO: more checks needed
outFile, fErr := os.Create(uiFilename.Text)
if fErr != nil {
log.Fatal(fErr)
}
defer outFile.Close()
// TODO: Korrekterweise sollte das Datum immer mit 23:59:59 geparst werden
cutoffDate, dtErr := time.Parse("2006-01-02", uiCutoffDate.Text)
if dtErr != nil {
log.Fatal(dtErr)
}
// fetch data from API and write to output file
recordsWritten, err := fetchAndWrite(
uiCredUsername.Text,
uiCredPassword.Text,
cutoffDate,
outFile,
)
if err != nil {
log.Fatal(err)
}
statusMessage.Text = fmt.Sprintf("%d Zeilen geschrieben", recordsWritten)
statusMessage.Show()
// clear fields to disable submit button
uiCutoffDate.SetText("")
// hide progressbar
obscureProgress.Hide()
},
}
w.SetContent(container.NewVBox(
uiTitle,
canvas.NewLine(theme.TextColor()),
uiForm,
obscureProgress,
statusMessage,
layout.NewSpacer(),
canvas.NewText("© 2021-2023", color.White),
))
w.Resize(fyne.NewSize(300, 500))
w.ShowAndRun()
}
func fetchAndWrite(login string, password string, cutoffDate time.Time, outFile *os.File) (int, error) {
// init vars
var morePages bool = true
var recordsWritten int = 0
var headerWritten bool = false
// contact API
client := http.Client{}
csvWriter := csv.NewWriter(outFile)
defer csvWriter.Flush()
for pageNo := 1; morePages; pageNo++ {
log.Println("requesting pageno " + fmt.Sprintf("%d", pageNo))
fulldoc, err := fetchNicmanagerAPI(client, login, password, pageNo)
if err != nil {
log.Fatal(err)
}
// TODO: this needs to go into a separate function
var domainList []Domain
jsonErr := json.Unmarshal(fulldoc, &domainList)
if jsonErr != nil {
log.Fatal(jsonErr)
}
//TODO: Irgendwo hier gehen Daten verloren. Im JSON sind noch Daten drin, hier nicht mehr
for _, rowData := range domainList {
if !headerWritten {
csvWriter.Write([]string{
"Domain",
"Order Date",
"Reg Date",
"Close Date",
})
headerWritten = true
}
// parse dates
dateOrd, _ := parseAPIdate(rowData.OrderDateTime)
dateReg, _ := parseAPIdate(rowData.RegistrationDateTime)
// format Delete date for output
dateDelFmt := ""
if rowData.DeleteDateTime != "" {
parsedDate, _ := parseAPIdate(rowData.DeleteDateTime)
dateDelFmt = parsedDate.Format("2006-01-02")
}
//log.Printf("Dateldel: %s (%s) - Cutoff_Unix: %d", rowData.DeleteDateTime, dateDelFmt, cutoffDate.Unix())
if rowData.IsBelowCutoff(cutoffDate) {
csvWriter.Write([]string{
rowData.Name,
dateOrd.Format("2006-01-02"),
dateReg.Format("2006-01-02"),
dateDelFmt,
})
recordsWritten++
log.Println("Written") // DEBUG
}
log.Println("---") //DEBUG
}
// do we have more pages?
morePages = (len(domainList) == 100)
}
return recordsWritten, nil
}
func parseAPIdate(dateString string) (time.Time, error) {
//DEBUG log.Println("parsing date: " + dateString)
return time.Parse("2006-01-02T15:04:05Z", dateString)
}
// IsBelowCutoff filters for records without delete date or with delete date after cutoff
func (d *Domain) IsBelowCutoff(cutoffDate time.Time) bool {
if d.DeleteDateTime != "" {
parseDelDate, _ := parseAPIdate(d.DeleteDateTime)
if parseDelDate.Unix() > cutoffDate.Unix() {
return true
}
} else {
return true
}
return false
}
func fetchNicmanagerAPI(client http.Client, login string, password string, pageNo int) ([]byte, error) {
var apiURL string = "https://api.nicmanager.com/v1/domains?limit=100&page="
req, rErr := http.NewRequest("GET", apiURL+fmt.Sprintf("%d", pageNo), nil)
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(login, password)
if rErr != nil {
log.Fatal(rErr)
}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// DEBUG remove later
//fmt.Println("Header output:")
//spew.Dump(res.Header)
// convert response into string
return ioutil.ReadAll(res.Body)
}