dati

A Go library/binary to parse & execute data against template langauges.
git clone git://src.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 90c911163d8b5a9fa7cc42396ed3e8920892abc5
parent 5c1ed4bfc2d22283f2aa04255fa532aa0a988c5b
Author: gearsix <gearsix@tuta.io>
Date:   Tue, 13 Apr 2021 11:24:57 +0100

refactored cmd/suti.go to work with new API

- Data is now defined here
- added loadFilePaths for parsing paths as a list of filepaths
- added mergeData
- suti_test now run build before running suti to catch compile errors

Diffstat:
Mcmd/suti.go | 86++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Mcmd/suti_test.sh | 22+++++++++++++++-------
2 files changed, 89 insertions(+), 19 deletions(-)

diff --git a/cmd/suti.go b/cmd/suti.go @@ -19,14 +19,18 @@ package main import ( "bufio" + "bytes" "fmt" "io" + "io/fs" "notabug.org/gearsix/suti" "os" "path/filepath" "strings" ) +type Data map[string]interface{} + type options struct { RootPath string PartialPaths []string @@ -83,23 +87,39 @@ func init() { } func main() { - data, err := suti.LoadDataFiles("", opts.GlobalDataPaths...) - assert(err, "failed to load global data files") - global, conflicts := suti.MergeData(data...) - for _, key := range conflicts { - warn(nil, "merge conflict for global data key: '%s'", key) + var err error + var global Data + var data []Data + var template suti.Template + var out bytes.Buffer + + opts.GlobalDataPaths = loadFilePaths(opts.GlobalDataPaths...) + for _, path := range opts.GlobalDataPaths { + var d Data + err = suti.LoadDataFile(path, &d) + assert(err, "failed to load global data '%s'", path) + data = append(data, d) } + global = mergeData(data) - data, err = suti.LoadDataFiles(opts.SortData, opts.DataPaths...) - assert(err, "failed to load data files") - - super, err := suti.GenerateSuperData(opts.DataKey, global, data) - assert(err, "failed to generate super data") + opts.DataPaths = loadFilePaths(opts.DataPaths...) + opts.DataPaths, err = suti.SortFileList(opts.DataPaths, opts.SortData) + if err != nil { + warn(err, "failed to sort data files") + } + data = make([]Data, 0) + for _, path := range opts.DataPaths { + var d Data + err = suti.LoadDataFile(path, &d) + assert(err, "failed to load data '%s'", path) + data = append(data, d) + } + global[opts.DataKey] = data - template, err := suti.LoadTemplateFile(opts.RootPath, opts.PartialPaths...) + template, err = suti.LoadTemplateFile(opts.RootPath, opts.PartialPaths...) assert(err, "unable to load templates") - out, err := template.Execute(super) + out, err = template.Execute(global) assert(err, "failed to execute template '%s'", opts.RootPath) fmt.Print(out.String()) @@ -234,3 +254,45 @@ func setDefaultOptions(o options) options { } return o } + +// load glob & dir filepaths as individual filepaths +func loadFilePaths(paths ...string) (filepaths []string) { + for _, path := range paths { + var err error + if strings.Contains(path, "*") { + var glob []string + glob, err = filepath.Glob(path) + assert(err, "failed to glob '%s'", path) + for _, p := range glob { + filepaths = append(filepaths, p) + } + } else { + err = filepath.Walk(path, + func(p string, info fs.FileInfo, e error) error { + if e == nil && !info.IsDir() { + filepaths = append(filepaths, p) + } + return e + }) + } + if err != nil { + assert(err, "failed to load filepaths for '%s'", path) + } + } + return +} + +func mergeData(data []Data) (merged Data) { + merged = make(Data) + for _, d := range data { + for key, val := range d { + if merged[key] == nil { + merged[key] = val + } else { + warn(nil, "merge conflict for global data key: '%s'", key) + } + } + } + return +} + diff --git a/cmd/suti_test.sh b/cmd/suti_test.sh @@ -1,12 +1,20 @@ #!/bin/bash # if nothing prints, the test passed -diff="diff" +diff="diff -bs" -go run suti.go -cfg ../examples/suti.cfg -r ../examples/template/html.hmpl > out.html -$diff out.html ../examples/out.html -rm out.html +go build -o suti suti.go -go run suti.go -cfg ../examples/suti.cfg -r ../examples/template/txt.mst > out.txt -$diff out.txt ../examples/out.txt -rm out.txt +if [ -e suti ]; then + ./suti -cfg ../examples/suti.cfg -r ../examples/template/html.hmpl > out.html + $diff out.html ../examples/out.html + rm out.html + + ./suti -cfg ../examples/suti.cfg -r ../examples/template/txt.mst > out.txt + $diff out.txt ../examples/out.txt + rm out.txt + + rm suti + + echo "if files are identical, TEST PASS" +fi