37 lines
672 B
Go
37 lines
672 B
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"gopkg.in/yaml.v3"
|
|
"os"
|
|
)
|
|
|
|
func YamlHeader() string {
|
|
return "# vi: set tabstop=2 shiftwidth=2 expandtab :\n---\n"
|
|
}
|
|
|
|
func WriteYaml(data interface{}, yamlFile string) error {
|
|
buffer := &bytes.Buffer{}
|
|
buffer.WriteString(YamlHeader())
|
|
|
|
yamlEncoder := yaml.NewEncoder(buffer)
|
|
yamlEncoder.SetIndent(2)
|
|
|
|
if err := yamlEncoder.Encode(data); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.WriteFile(yamlFile, buffer.Bytes(), 0644); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func WriteEmptyYaml(yamlFile string) error {
|
|
buffer := &bytes.Buffer{}
|
|
buffer.WriteString(YamlHeader())
|
|
|
|
return os.WriteFile(yamlFile, buffer.Bytes(), 0644)
|
|
}
|