mirror of
https://github.com/Luzifer/ansible-role-version.git
synced 2024-12-23 02:51:20 +00:00
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
|
||
|
yaml "gopkg.in/yaml.v2"
|
||
|
)
|
||
|
|
||
|
type ansibleRoleDefinition struct {
|
||
|
Name string `yaml:"name"`
|
||
|
Src string `yaml:"src"`
|
||
|
Version string `yaml:"version"`
|
||
|
}
|
||
|
|
||
|
func patchRoleFile(rolesFile string, updates map[string]string) error {
|
||
|
var (
|
||
|
inFileContent []byte
|
||
|
err error
|
||
|
)
|
||
|
if inFileContent, err = ioutil.ReadFile(rolesFile); err != nil {
|
||
|
return fmt.Errorf("Roles file not found: %s", err)
|
||
|
}
|
||
|
|
||
|
in := []ansibleRoleDefinition{}
|
||
|
if err = yaml.Unmarshal(inFileContent, &in); err != nil {
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if inFileContent, err = yaml.Marshal(in); err != nil {
|
||
|
return fmt.Errorf("Unable to marshal roles file: %s", err)
|
||
|
}
|
||
|
|
||
|
buf := new(bytes.Buffer)
|
||
|
buf.Write([]byte("---\n\n"))
|
||
|
buf.Write(inFileContent)
|
||
|
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
|
||
|
}
|