-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
45 lines (41 loc) · 1.12 KB
/
csv.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
package main
import (
"encoding/csv"
"fmt"
"os"
)
const (
HeaderSourceAccount = "Source User"
HeaderSourcePassword = "Source Password"
HeaderRemoteAccount = "Remote User"
HeaderRemotePassword = "Remote Password"
)
func ReadCSV(path string) (Accounts, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
reader := csv.NewReader(f)
c, err := reader.ReadAll()
if err != nil {
return nil, err
}
accounts := make(Accounts, 0, len(c)-1)
for idx, row := range c {
if idx == 0 {
switch {
case row[0] != HeaderSourceAccount:
return nil, fmt.Errorf("first column in csv should be: %v", HeaderSourceAccount)
case row[1] != HeaderSourcePassword:
return nil, fmt.Errorf("second column in csv should be: %v", HeaderSourcePassword)
case row[2] != HeaderRemoteAccount:
return nil, fmt.Errorf("third column in csv should be: %v", HeaderRemoteAccount)
case row[3] != HeaderRemotePassword:
return nil, fmt.Errorf("fourth column in csv should be: %v", HeaderRemotePassword)
}
continue
}
accounts = append(accounts, NewSourceRemote(row, "Account", idx))
}
return accounts, nil
}