Skip to content

Commit

Permalink
Added Debug and updated testing
Browse files Browse the repository at this point in the history
  • Loading branch information
runeimp committed May 25, 2020
1 parent 8205522 commit ea02e78
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 104 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Templar ChangeLog
=================

v0.2.0
------

Added `Debug` attribute
Temporarily switched to `github.com/runeimp/gotenv` from `github.com/subosito/gotenv`


v0.1.1
------

Expand Down
4 changes: 4 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#

alias ver := version
# set load-dotenv := false # Not supported yet
# load-dotenv := false

# Like in make the first recipe is used by default.
# I like listing all the recipes by default.
Expand Down Expand Up @@ -76,6 +78,8 @@ dist-test:

# Run the command line app
run +args="":
just _term-wipe
# NOTE: Just itself ALWAYS loads a .env file if present
go run cmd/templar/main.go {{args}}

# Run a test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Templar
=======

Templar Command Line Tool v2.0.0
Templar Library v0.1.1
Templar Library v0.2.0

Command line templating system written in Go. Though the initial idea was written in BASH. And that was cool; but that version had serious limitations as well. Go to the rescue!

Expand Down
23 changes: 13 additions & 10 deletions cmd/templar/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* PACKAGE
*/
// PACKAGE
package main

/*
Expand All @@ -17,7 +15,7 @@ import (
)

/*
* CONSTANTS
* 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."
Expand All @@ -26,6 +24,9 @@ const (
CLIName = "templar"
)

/*
* CONSTANTS
*/
const (
ErrorENVParsing = iota + 50
ErrorINIParsing
Expand Down Expand Up @@ -109,6 +110,8 @@ func main() {
os.Exit(ErrorTemplateMissing)
}

templar.Debug = cli.Debug

// fmt.Printf("templar.main() | cli.DataFile = %q\n", cli.DataFile)
for _, file := range cli.DataFile {
ext := path.Ext(file)
Expand All @@ -120,7 +123,7 @@ func main() {
case ".JSON":
jsonFiles = append(jsonFiles, file)
default:
fmt.Errorf("Unknown data file type: %q\n", ext)
fmt.Errorf("Unknown data file type: %q", ext)
}
}
// fmt.Printf("templar.main() | templar.Data = %#v\n", templar.Data)
Expand All @@ -141,11 +144,11 @@ func main() {
// fmt.Printf("templar.main() | cli.Template = %q\n", cli.Template)
// fmt.Printf("templar.main() | templar.Data = %#v\n", templar.Data)
if cli.Debug {
fmt.Printf("templar.main() | envFiles = %#v\n", envFiles)
fmt.Printf("templar.main() | iniFiles = %#v\n", iniFiles)
fmt.Printf("templar.main() | jsonFiles = %#v\n", jsonFiles)
fmt.Printf("templar.main() | template = %q\n", cli.Template)
fmt.Printf("templar.main() | checkDotEnv = %t\n", checkDotEnv)
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() | template = %q\n", cli.Template)
fmt.Fprintf(os.Stderr, "templar.main() | checkDotEnv = %t\n", checkDotEnv)
}

err := templar.InitData(checkDotEnv, envFiles...)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.13
require (
github.com/alecthomas/kong v0.2.1
github.com/cbroglie/mustache v1.0.1
github.com/runeimp/gotenv v1.3.0
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
github.com/subosito/gotenv v1.2.0
github.com/subosito/gotenv v1.2.0 // indirect
gopkg.in/ini.v1 v1.51.0
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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=
github.com/cbroglie/mustache v1.0.1/go.mod h1:R/RUa+SobQ14qkP4jtx5Vke5sDytONDQXNLPY/PO69g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
Expand All @@ -13,18 +14,27 @@ 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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/runeimp/gotenv v1.3.0 h1:G8nyaB/v0l/8RLWr4JgYDjcFkB0MC3zw2BAm1iXP1eg=
github.com/runeimp/gotenv v1.3.0/go.mod h1:FO1U4BwxNeuduJ9KJf16W+MIZcVBoFwdGIVt+rVfYdk=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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=
80 changes: 76 additions & 4 deletions templar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"strings"

"github.com/cbroglie/mustache"
"github.com/subosito/gotenv"
// "github.com/subosito/gotenv"
"github.com/runeimp/gotenv"
ini "gopkg.in/ini.v1"
)

Expand All @@ -21,20 +22,35 @@ const (
Name = "Templar"

// Version denotes the library version
Version = "0.1.1"
Version = "0.2.0"
)

/*
* CONSTANTS
*/
const (
DebugOff = 0
DebugLog = 1
DebugInfo = 2
DebugWarn = 3
DebugError = 4
)

/*
* VARIABLES
*/
var (
// envBackupData map[string]string
dataProvider map[string]interface{}
Debug = DebugWarn
initialized = false
)

/*
* TYPES
*/

// Data is a collection for all external data
var Data struct {
INIFile []string
JSONFile []string
Expand All @@ -44,11 +60,48 @@ var Data struct {
/*
* METHODS
*/
// func envBackup() {
// if Debug <= DebugWarn {
// fmt.Fprintln(os.Stderr, "templar.envBackup()")
// }

// envBackupData = make(map[string]string)

// for _, envar := range os.Environ() {
// kv := strings.Split(envar, "=")
// k := kv[0]
// v := kv[1]
// if Debug <= DebugLog {
// fmt.Fprintf(os.Stderr, "templar.envBackup() | envBackupData[%q] = %q\n", k, v)
// }

// envBackupData[k] = v
// }
// }

// func envReset() {
// if Debug <= DebugWarn {
// fmt.Fprintln(os.Stderr, "templar.envReset()")
// }
// os.Clearenv()
// for k, v := range envBackupData {
// if Debug <= DebugLog {
// fmt.Fprintf(os.Stderr, "templar.envReset() | os.Setenv(%q, %q)\n", k, v)
// }
// os.Setenv(k, v)
// }
// }

func init() {
dataProvider = make(map[string]interface{})
// envBackup()
}

// InitData initializes the template environment with external data
func InitData(checkDotEnv bool, files ...string) (err error) {
if Debug <= DebugInfo {
fmt.Fprintf(os.Stderr, "templar.InitData() | checkDotEnv = %t | initialized = %t\n", checkDotEnv, initialized)
}
if initialized == false {
if checkDotEnv {
gotenv.OverLoad()
Expand All @@ -65,6 +118,9 @@ func InitData(checkDotEnv bool, files ...string) (err error) {
}

func parseEnvironment() {
if Debug <= DebugInfo {
fmt.Fprintf(os.Stderr, "templar.parseEnvironment() | initialized = %t\n", initialized)
}
for _, base := range os.Environ() {
pair := strings.SplitN(base, "=", 2)
k := pair[0]
Expand All @@ -74,10 +130,16 @@ func parseEnvironment() {
}

func parseFileData(file string) (err error) {
if Debug <= DebugInfo {
fmt.Fprintf(os.Stderr, "templar.parseFileData() | file = %q\n", file)
}
if len(file) > 0 {
ext := path.Ext(file)
switch strings.ToUpper(ext) {
case ".ENV":
if Debug <= DebugInfo {
fmt.Fprintf(os.Stderr, "templar.parseFileData() | .ENV | gotenv.OverLoad(%q)\n", file)
}
err = gotenv.OverLoad(file)
parseEnvironment()
case ".INI":
Expand All @@ -91,13 +153,14 @@ func parseFileData(file string) (err error) {
return err
}
default:
fmt.Errorf("Unknown data file type: %q\n", ext)
fmt.Errorf("Unknown data file type: %q", ext)
}
}

return err
}

// ParseINI loads INI file data into the dataProvider
func ParseINI(file string) (err error) {
var iniData *ini.File
iniData, err = ini.Load(file)
Expand All @@ -122,6 +185,7 @@ func ParseINI(file string) (err error) {
return err
}

// ParseJSON loads JSON file data into the dataProvider
func ParseJSON(file string) (err error) {
var jsonData []byte

Expand All @@ -136,16 +200,24 @@ func ParseJSON(file string) (err error) {
return err
}

func Reinitialize() {
// Reinitialize resets the dataProvider
func Reinitialize(debug int) {
Debug = debug
if Debug <= DebugInfo {
fmt.Fprintf(os.Stderr, "templar.Reinitialize() | debug = %d | initialized = %t\n", debug, initialized)
}
dataProvider = make(map[string]interface{})
gotenv.Reset()
initialized = false
}

// Render handles template rendering
func Render(template string) (output string, err error) {
output, err = mustache.RenderFile(template, dataProvider)
return output, err
}

// RenderToFile handles rendering templates to file
func RenderToFile(filename, template string) (output string, err error) {
output, err = mustache.RenderFile(template, dataProvider)
if err != nil {
Expand Down
Loading

0 comments on commit ea02e78

Please sign in to comment.