-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv2table.go
More file actions
77 lines (62 loc) · 1.97 KB
/
csv2table.go
File metadata and controls
77 lines (62 loc) · 1.97 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
// Package csv2table provides a way to import csv files to corresponding database tables
// while providing different way to convert csv data to match your database definition.
//
// Currently it provides mysql implementation only.
package csv2table
import (
"fmt"
"github.com/spf13/viper"
)
// DbService is the interface that needs to be implemented by various databases in order to offer csv2table support
type DbService interface {
// Start is the first method called when a new csv file is processed.
// The main configuration (csv2table.toml) is merged with file based configuration and passed as the viper.Viper param
Start(fileName string, v *viper.Viper) error
// End is called after the csv file has beed completely processed
End() error
// ProcessHeader is called for the 1st line of the csv file
ProcessHeader(header []string) error
// ProcessLine is called for each subsequent csv line
ProcessLine(line []string) error
}
// ImportFileStatus holds import status for each imported file
type ImportFileStatus struct {
FileName string // processed filename
Error error // error or nil if success
RowCount int // processed rows
}
// UnmarshallConfig reads generic (non db provider) configuration
func UnmarshallConfig(v *viper.Viper) error {
// email configuration
err := v.UnmarshalKey("email", &emailConfig)
if err != nil {
return fmt.Errorf("unable to unmarshall loaded configuration email, %v", err)
}
return nil
}
// AfterImport is called after all files were processed
func AfterImport(statuses []ImportFileStatus) error {
errors := false
var status ImportFileStatus
for _, status = range statuses {
if status.Error != nil {
errors = true
break
}
}
// if email is not configured there's nothing else to do
if !emailConfigured() {
return nil
}
var err error
if !errors {
if emailConfig.SendOnSuccess {
err = sendEmailSuccess(statuses)
}
} else {
if emailConfig.SendOnError {
err = sendEmailError(statuses)
}
}
return err
}