first commit
This commit is contained in:
36
internal/utils/yaml_utils.go
Normal file
36
internal/utils/yaml_utils.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user