Skip to content

Commit

Permalink
Added TOML and YAML support
Browse files Browse the repository at this point in the history
  • Loading branch information
runeimp committed May 26, 2020
1 parent ea02e78 commit 90b4946
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ v0.2.0
------

Added `Debug` attribute
Added TOML and YAML support
Temporarily switched to `github.com/runeimp/gotenv` from `github.com/subosito/gotenv`


Expand Down
7 changes: 7 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ run +args="":
just _term-wipe
just test-{{cmd}} "{{data}}"

# Run Go Unit Tests
@test-coverage +data='':
just _term-wipe
echo "You need to run:"
echo "go test -coverprofile=c.out"
echo "go tool cover -func=c.out"

# Test with debug enabled
test-debug +data="example.env":
@# CLI_ENV_VAR="Sound and fury" go run cmd/templar/main.go example.tmpl --data-file example.env CLI_VAR="As you like it" --debug
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Features
* [x] Option to exclude automatic loading of a local `.env` file
* [x] INI file data
* [x] JSON file data
* [ ] YAML file data
* [ ] TOML file data
* [x] YAML file data
* [x] TOML file data
* [ ] XML file data?
* [ ] CSV file data?
* [ ] SQLite database query?
Expand Down Expand Up @@ -69,7 +69,17 @@ Test Suite

Test uses Go's normal unit testing tools at this time.

Test Coverage: 83.9%
Test Coverage: 78.1%

It was 83.9% but I added more stuff to test. :angel:


ToDo
----

* [ ] Add MVVM features so that the data input can processed into a ViewModel to limit or manipulate the input in simple ways for the View (template)
* [ ] Complete the data input options
* [ ] Optimize the data processing. Make it more memory friendly on constrained systems and allow for say referencing (for instance) a terabyte sized CSV file for data input.



Expand Down
40 changes: 35 additions & 5 deletions cmd/templar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
* APP CONSTANTS
*/
const (
AppDesc = "Command line templating system based on Mustache template engine and data supplied by environment variables, ENV, INI, and JSON files. And soon YAML, and TOML files as well."
AppDesc = "Command line templating system based on Mustache template engine and data supplied by environment variables, ENV, INI, JSON, YAML, and TOML files. And soon CSV, XML, and SQLite files as well."
AppName = "Templar"
AppVersion = "2.0.0"
AppVersion = "2.1.0"
CLIName = "templar"
)

Expand All @@ -33,6 +33,8 @@ const (
ErrorJSONParsing
ErrorTemplateMissing
ErrorTemplateRendering
ErrorTOMLParsing
ErrorYAMLParsing
)

/*
Expand Down Expand Up @@ -60,7 +62,9 @@ var cli struct {
DataFile []string `short:"f" help:"Use the specified DATA_FILE to populate the template environment. File type determined by the extension on the file name." placeholder:"DATA_FILE" type:"existingfile"`
DataINI []string `short:"i" help:"Use the specified INI_FILE (regardless of it's file extension) to populate the template environment." placeholder:"INI_FILE" type:"existingfile"`
DataJSON []string `short:"j" help:"Use the specified JSON_FILE (regardless of it's file extension) to populate the template environment." placeholder:"JSON_FILE" type:"existingfile"`
Debug bool `help:"Show debug info on stderr." hidden`
DataTOML []string `short:"t" help:"Use the specified TOML_FILE (regardless of it's file extension) to populate the template environment." placeholder:"TOML_FILE" type:"existingfile"`
DataYAML []string `short:"y" help:"Use the specified YAML_FILE (regardless of it's file extension) to populate the template environment." placeholder:"YAML_FILE" type:"existingfile"`
Debug int `help:"Show debug info on stderr." default:"0" hidden`
NoDotenv bool `short:"n" help:"Do not load a local .env file if present."`
OutputFile string `short:"o" help:"Output to the specified file." placeholder:"FILE" sep:' ' type:"file"`
Template string `arg optional help:"Specify the template file to render." type:"existingfile"`
Expand All @@ -77,6 +81,8 @@ var (
envFiles []string
iniFiles []string
jsonFiles []string
tomlFiles []string
yamlFiles []string
)

/*
Expand All @@ -96,7 +102,7 @@ func init() {
* MAIN ENTRYPOINT
*/
func main() {
if cli.Debug {
if cli.Debug > templar.DebugOff {
fmt.Println(AppLabel)
}

Expand All @@ -122,6 +128,10 @@ func main() {
iniFiles = append(iniFiles, file)
case ".JSON":
jsonFiles = append(jsonFiles, file)
case ".TOML":
tomlFiles = append(tomlFiles, file)
case ".YAML":
yamlFiles = append(yamlFiles, file)
default:
fmt.Errorf("Unknown data file type: %q", ext)
}
Expand All @@ -137,16 +147,24 @@ func main() {
for _, file := range cli.DataJSON {
jsonFiles = append(jsonFiles, file)
}
for _, file := range cli.DataTOML {
tomlFiles = append(tomlFiles, file)
}
for _, file := range cli.DataYAML {
yamlFiles = append(yamlFiles, file)
}

// templar.Data.Template = cli.Template
checkDotEnv := !cli.NoDotenv

// fmt.Printf("templar.main() | cli.Template = %q\n", cli.Template)
// fmt.Printf("templar.main() | templar.Data = %#v\n", templar.Data)
if cli.Debug {
if cli.Debug > templar.DebugOff {
fmt.Fprintf(os.Stderr, "templar.main() | envFiles = %#v\n", envFiles)
fmt.Fprintf(os.Stderr, "templar.main() | iniFiles = %#v\n", iniFiles)
fmt.Fprintf(os.Stderr, "templar.main() | jsonFiles = %#v\n", jsonFiles)
fmt.Fprintf(os.Stderr, "templar.main() | tomlFiles = %#v\n", tomlFiles)
fmt.Fprintf(os.Stderr, "templar.main() | yamlFiles = %#v\n", yamlFiles)
fmt.Fprintf(os.Stderr, "templar.main() | template = %q\n", cli.Template)
fmt.Fprintf(os.Stderr, "templar.main() | checkDotEnv = %t\n", checkDotEnv)
}
Expand All @@ -169,6 +187,18 @@ func main() {
os.Exit(ErrorJSONParsing)
}

err = templar.InitData(checkDotEnv, tomlFiles...)
if err != nil {
fmt.Fprintf(os.Stderr, "TOML File Parsing Error: %s\n", err.Error())
os.Exit(ErrorTOMLParsing)
}

err = templar.InitData(checkDotEnv, yamlFiles...)
if err != nil {
fmt.Fprintf(os.Stderr, "YAML File Parsing Error: %s\n", err.Error())
os.Exit(ErrorYAMLParsing)
}

if len(cli.OutputFile) > 0 {
_, err := templar.RenderToFile(cli.OutputFile, cli.Template)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all = ["your", "base", "are", "belong", "to", "us"]
boolean = false
# void =

[one]
two-point-one = 2.1

[one.two]
five = "six"
four = true
three = 4
9 changes: 9 additions & 0 deletions example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
all: ["your", "base", "are", "belong", "to", "us"]
one:
two:
three: 4
four: true
five: six
two-point-one: 2.1
boolean: false
void: null
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.13
require (
github.com/alecthomas/kong v0.2.1
github.com/cbroglie/mustache v1.0.1
github.com/pelletier/go-toml v1.8.0
github.com/runeimp/gotenv v1.3.0
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
gopkg.in/ini.v1 v1.51.0
gopkg.in/yaml.v2 v2.3.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/kong v0.2.1 h1:V1tLBhyQBC4rsbXbcOvm3GBaytJSwRNX69fp1WJxbqQ=
github.com/alecthomas/kong v0.2.1/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI=
github.com/cbroglie/mustache v1.0.1 h1:ivMg8MguXq/rrz2eu3tw6g3b16+PQhoTn6EZAhst2mw=
Expand All @@ -10,6 +12,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.8.0 h1:Keo9qb7iRJs2voHvunFtuuYFsbWeOBh8/P9v/kVMFtw=
github.com/pelletier/go-toml v1.8.0/go.mod h1:D6yutnOGMveHEPV7VQOuvI/gXY61bv+9bAOTRnLElKs=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -38,3 +42,5 @@ gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit 90b4946

Please sign in to comment.