Archived
1
This repository has been archived on 2025-08-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
playbookctl/internal/utils/yaml_utils.go
2024-12-23 01:55:48 +03:00

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