github.com/konsorten/ktn-build-info@v1.0.11/ver/version_info_actions.go (about) 1 package ver 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 8 log "github.com/sirupsen/logrus" 9 yaml "gopkg.in/yaml.v2" 10 ) 11 12 type versionInfoActionsYAML struct { 13 Inputs []map[string]string `yaml:"inputs"` 14 Outputs []map[string]string `yaml:"outputs"` 15 } 16 17 type VersionInfoAction struct { 18 Name string 19 Parameters map[string]string 20 } 21 22 type VersionInfoActions struct { 23 Inputs []VersionInfoAction 24 Outputs []VersionInfoAction 25 } 26 27 func ReadVersionInfoActions() (*VersionInfoActions, error) { 28 // check if the file exists 29 if _, err := os.Stat(VersionInfoYamlFilename); err != nil { 30 log.Debugf("No %v found", VersionInfoYamlFilename) 31 return nil, nil 32 } 33 34 // read the file 35 data, err := ioutil.ReadFile(VersionInfoYamlFilename) 36 37 if err != nil { 38 log.Errorf("Failed to read %v file: %v", VersionInfoYamlFilename, err) 39 return nil, err 40 } 41 42 // parse the file 43 viy := versionInfoActionsYAML{} 44 45 err = yaml.Unmarshal(data, &viy) 46 47 if err != nil { 48 log.Errorf("Failed to parse %v file: %v", VersionInfoYamlFilename, err) 49 return nil, err 50 } 51 52 // convert the information 53 var ret VersionInfoActions 54 55 for _, i := range viy.Inputs { 56 action := VersionInfoAction{ 57 Parameters: make(map[string]string), 58 } 59 60 for n, p := range i { 61 if n == "action" { 62 action.Name = p 63 } else { 64 action.Parameters[n] = p 65 } 66 } 67 68 // is action valid? 69 if action.Name == "" { 70 return nil, fmt.Errorf("Failed to get action name; missing 'action' property: %v", i) 71 } 72 73 // done 74 ret.Inputs = append(ret.Inputs, action) 75 } 76 77 for _, o := range viy.Outputs { 78 action := VersionInfoAction{ 79 Parameters: make(map[string]string), 80 } 81 82 for n, p := range o { 83 if n == "action" { 84 action.Name = p 85 } else { 86 action.Parameters[n] = p 87 } 88 } 89 90 // is action valid? 91 if action.Name == "" { 92 return nil, fmt.Errorf("Failed to get action name; missing 'action' property: %v", o) 93 } 94 95 // done 96 ret.Outputs = append(ret.Outputs, action) 97 } 98 99 return &ret, nil 100 }