1
0
mirror of https://github.com/TomWright/dasel.git synced 2022-05-22 02:32:45 +03:00

Add WriteFile

This commit is contained in:
beatcracker
2021-12-08 19:58:19 +03:00
parent 0a2b0418b6
commit 39c58821b0

33
node.go
View File

@@ -2,6 +2,7 @@ package dasel
import (
"fmt"
"os"
"reflect"
"regexp"
@@ -130,7 +131,7 @@ func New(value interface{}) *Node {
return rootNode
}
// NewFromFile returns a new root node by parsing file.
// NewFromFile returns a new root node by parsing file using specified read parser.
func NewFromFile(path, parser string) (*Node, error) {
readParser, err := storage.NewReadParserFromString(parser)
if err != nil {
@@ -145,6 +146,36 @@ func NewFromFile(path, parser string) (*Node, error) {
return New(data), nil
}
// WriteFile writes data to disk using specified write parser and options.
// TODO: use io.Writer insted of file path? This should help with tests...
func WriteFile(data interface{}, file string, parser string, compact, escapeHTML bool) error {
writeParser, err := storage.NewWriteParserFromString(parser)
if err != nil {
return err
}
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("could not open output file: %w", err)
}
defer f.Close()
writeOptions := []storage.ReadWriteOption{
storage.EscapeHTMLOption(escapeHTML),
}
if compact {
writeOptions = append(writeOptions, storage.PrettyPrintOption(false))
}
if err := storage.Write(writeParser, data, nil, f, writeOptions...); err != nil {
return fmt.Errorf("could not write to output file: %w", err)
}
return nil
}
func (n *Node) setValue(newValue interface{}) {
n.Value = reflect.ValueOf(newValue)
if n.Selector.Type == "ROOT" {