forked from spezam/eventbridge-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_sources.go
89 lines (75 loc) · 1.92 KB
/
data_sources.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"strings"
"gopkg.in/yaml.v2"
)
type samTemplate struct {
Resources map[string]struct {
Type string `yaml:"Type"`
Properties struct {
FunctionName string `yaml:"FunctionName"`
Events map[string]struct {
Type string `yaml:"Type"`
Properties struct {
EventBusName string `yaml:"EventBusName,omitempty"`
InputPath string `yaml:"InputPath,omitempty"`
Pattern interface{} `yaml:"Pattern,omitempty"`
} `yaml:"Properties"`
} `yaml:"Events"`
} `yaml:"Properties"`
} `yaml:"Resources"`
}
// file://eventpattern.json
func dataFromFile(filepath string) (string, error) {
file := strings.Replace(filepath, "file://", "", -1)
e, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(e), nil
}
// sam://template.yaml/FunctionName
func dataFromSAM(sampath string) (string, error) {
function := path.Base(sampath)
template := strings.Replace(sampath, "sam://", "", -1)
template = strings.Replace(template, fmt.Sprintf("/%s", function), "", -1)
e, err := ioutil.ReadFile(template)
if err != nil {
return "", err
}
// unmarshal SAM template
b := &samTemplate{}
if err := yaml.Unmarshal([]byte(e), &b); err != nil {
return "", err
}
// find EventBridgeRule and marshal to JSON
var p []byte
for _, e := range b.Resources[function].Properties.Events {
if e.Type == "EventBridgeRule" {
if p, err = json.Marshal(convertMap(e.Properties.Pattern)); err != nil {
return "", err
}
}
}
return string(p), nil
}
// convert map[interface{}]interface{} to map[string]interface{}
func convertMap(i interface{}) interface{} {
switch x := i.(type) {
case map[interface{}]interface{}:
m := map[string]interface{}{}
for k, v := range x {
m[k.(string)] = convertMap(v)
}
return m
case []interface{}:
for i, v := range x {
x[i] = convertMap(v)
}
}
return i
}