1
0
Fork 0
mirror of https://github.com/Luzifer/ansible-role-version.git synced 2024-12-23 02:51:20 +00:00
ansible-role-version/cmd/helper.go
Knut Ahlers 2db953925e
Migrate to cobra to support more future commands
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-01-11 00:02:48 +01:00

53 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
}