This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
/
version_info_actions.go
100 lines (79 loc) · 1.91 KB
/
version_info_actions.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
package ver
import (
"fmt"
"io/ioutil"
"os"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
type versionInfoActionsYAML struct {
Inputs []map[string]string `yaml:"inputs"`
Outputs []map[string]string `yaml:"outputs"`
}
type VersionInfoAction struct {
Name string
Parameters map[string]string
}
type VersionInfoActions struct {
Inputs []VersionInfoAction
Outputs []VersionInfoAction
}
func ReadVersionInfoActions() (*VersionInfoActions, error) {
// check if the file exists
if _, err := os.Stat(VersionInfoYamlFilename); err != nil {
log.Debugf("No %v found", VersionInfoYamlFilename)
return nil, nil
}
// read the file
data, err := ioutil.ReadFile(VersionInfoYamlFilename)
if err != nil {
log.Errorf("Failed to read %v file: %v", VersionInfoYamlFilename, err)
return nil, err
}
// parse the file
viy := versionInfoActionsYAML{}
err = yaml.Unmarshal(data, &viy)
if err != nil {
log.Errorf("Failed to parse %v file: %v", VersionInfoYamlFilename, err)
return nil, err
}
// convert the information
var ret VersionInfoActions
for _, i := range viy.Inputs {
action := VersionInfoAction{
Parameters: make(map[string]string),
}
for n, p := range i {
if n == "action" {
action.Name = p
} else {
action.Parameters[n] = p
}
}
// is action valid?
if action.Name == "" {
return nil, fmt.Errorf("Failed to get action name; missing 'action' property: %v", i)
}
// done
ret.Inputs = append(ret.Inputs, action)
}
for _, o := range viy.Outputs {
action := VersionInfoAction{
Parameters: make(map[string]string),
}
for n, p := range o {
if n == "action" {
action.Name = p
} else {
action.Parameters[n] = p
}
}
// is action valid?
if action.Name == "" {
return nil, fmt.Errorf("Failed to get action name; missing 'action' property: %v", o)
}
// done
ret.Outputs = append(ret.Outputs, action)
}
return &ret, nil
}