-
Notifications
You must be signed in to change notification settings - Fork 28
/
impl.go
94 lines (75 loc) · 1.48 KB
/
impl.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
package draft
import (
"encoding/csv"
"io"
"strings"
"github.com/rakyll/statik/fs"
)
func setImpl(com *Component) {
if s := strings.TrimSpace(com.Impl); len(s) > 0 {
return
}
impl := getCloudImpl(com.Provider, com.Kind)
if len(impl) > 0 {
com.Impl = impl
}
}
func getCloudImpl(provider, kind string) string {
switch strings.TrimSpace(strings.ToLower(provider)) {
case "aws":
return awsImpl()(kind)
case "google":
return googleImpl()(kind)
case "azure":
return azureImpl()(kind)
default:
return defaultImpl()(kind)
}
}
func awsImpl() func(string) string {
dict, _ := readCsvFile("/aws.csv")
return func(key string) string {
return dict[key]
}
}
func googleImpl() func(string) string {
dict, _ := readCsvFile("/google.csv")
return func(key string) string {
return dict[key]
}
}
func azureImpl() func(string) string {
dict, _ := readCsvFile("/azure.csv")
return func(key string) string {
return dict[key]
}
}
func defaultImpl() func(string) string {
dict, _ := readCsvFile("/default.csv")
return func(key string) string {
return dict[key]
}
}
func readCsvFile(filePath string) (map[string]string, error) {
dict := map[string]string{}
sfs, err := fs.New()
if err != nil {
return dict, err
}
f, err := sfs.Open(filePath)
if err != nil {
return dict, err
}
defer f.Close()
csvr := csv.NewReader(f)
for {
row, err := csvr.Read()
if err != nil {
if err == io.EOF {
err = nil
}
return dict, err
}
dict[row[0]] = row[1]
}
}