2018-01-10 23:02:48 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-03-19 17:15:49 +00:00
|
|
|
"os"
|
2018-01-10 23:02:48 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ansibleRoleDefinition struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Src string `yaml:"src"`
|
|
|
|
Version string `yaml:"version"`
|
|
|
|
}
|
|
|
|
|
2018-03-19 17:15:49 +00:00
|
|
|
func getRoleDefinitions(rolesFile string) ([]ansibleRoleDefinition, error) {
|
|
|
|
rf, err := os.Open(rolesFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rf.Close()
|
|
|
|
|
|
|
|
def := []ansibleRoleDefinition{}
|
|
|
|
return def, yaml.NewDecoder(rf).Decode(&def)
|
|
|
|
}
|
|
|
|
|
2018-01-10 23:02:48 +00:00
|
|
|
func patchRoleFile(rolesFile string, updates map[string]string) error {
|
2018-03-19 17:15:49 +00:00
|
|
|
inFile, err := os.Open(rolesFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to open roles files: %s", err)
|
2018-01-10 23:02:48 +00:00
|
|
|
}
|
2018-03-19 17:15:49 +00:00
|
|
|
defer inFile.Close()
|
2018-01-10 23:02:48 +00:00
|
|
|
|
|
|
|
in := []ansibleRoleDefinition{}
|
2018-03-19 17:15:49 +00:00
|
|
|
if err = yaml.NewDecoder(inFile).Decode(&in); err != nil {
|
2018-01-10 23:02:48 +00:00
|
|
|
return fmt.Errorf("Unable to parse roles file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for roleName, roleVersion := range updates {
|
|
|
|
for i := range in {
|
|
|
|
if in[i].Name == roleName {
|
|
|
|
in[i].Version = roleVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 17:15:49 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte("---\n\n"))
|
|
|
|
|
|
|
|
if err := yaml.NewEncoder(buf).Encode(in); err != nil {
|
2018-01-10 23:02:48 +00:00
|
|
|
return fmt.Errorf("Unable to marshal roles file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Write([]byte("\n...\n"))
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(rolesFile, buf.Bytes(), 0644); err != nil {
|
|
|
|
return fmt.Errorf("Unable to write roles file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|