From ea02e7848975cac1b0800635904c0d79160a3c00 Mon Sep 17 00:00:00 2001 From: RuneImp Date: Sun, 24 May 2020 23:05:09 -0700 Subject: [PATCH] Added Debug and updated testing --- CHANGELOG.md | 7 +++ Justfile | 4 ++ README.md | 2 +- cmd/templar/main.go | 23 ++++--- go.mod | 3 +- go.sum | 10 +++ templar.go | 80 ++++++++++++++++++++++-- templar_test.go | 144 +++++++++++++++++--------------------------- 8 files changed, 169 insertions(+), 104 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 096c696..25ef35b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ------ diff --git a/Justfile b/Justfile index 86bc59d..13787f9 100644 --- a/Justfile +++ b/Justfile @@ -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. @@ -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 diff --git a/README.md b/README.md index 95165e5..dfcb65e 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/cmd/templar/main.go b/cmd/templar/main.go index 69efd09..a36deb3 100644 --- a/cmd/templar/main.go +++ b/cmd/templar/main.go @@ -1,6 +1,4 @@ -/* - * PACKAGE - */ +// PACKAGE package main /* @@ -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." @@ -26,6 +24,9 @@ const ( CLIName = "templar" ) +/* + * CONSTANTS + */ const ( ErrorENVParsing = iota + 50 ErrorINIParsing @@ -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) @@ -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) @@ -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...) diff --git a/go.mod b/go.mod index 565f06c..7e7e0c3 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 02daa28..9990f2a 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -13,12 +14,17 @@ 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= @@ -26,5 +32,9 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn 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= diff --git a/templar.go b/templar.go index 788d4a0..67f707a 100644 --- a/templar.go +++ b/templar.go @@ -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" ) @@ -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 @@ -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() @@ -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] @@ -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": @@ -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) @@ -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 @@ -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 { diff --git a/templar_test.go b/templar_test.go index 5f8456e..3d5d906 100644 --- a/templar_test.go +++ b/templar_test.go @@ -1,12 +1,13 @@ package templar import ( - "os" - "testing" + "fmt" + "os" + "testing" ) -var noDotEnvExpectation = `Hello Oedipus! -How do you like it in "/Users/runeimp/dev/apps/templar"? +var exampleDotEnvExpectation = `Hello Oedipus! +How do you like it in "/Users/runeimp/Dropbox/Profile/Home/dev/apps/templar"? ENV_FILE_COMMENT == '' ENV_FILE_VAR == 'The Bard' @@ -23,7 +24,7 @@ DEFAULT.global_ini == ` var dotEnvExpectation = `Hello Horatio! -How do you like it in "/Users/runeimp/dev/apps/templar"? +How do you like it in "/Users/runeimp/Dropbox/Profile/Home/dev/apps/templar"? ENV_FILE_COMMENT == '' ENV_FILE_VAR == '.env Ninja!' @@ -40,7 +41,7 @@ DEFAULT.global_ini == ` var dotEnvAndINIExpectation = `Hello Hamlet! -How do you like it in "/Users/runeimp/dev/apps/templar"? +How do you like it in "/Users/runeimp/Dropbox/Profile/Home/dev/apps/templar"? ENV_FILE_COMMENT == '' ENV_FILE_VAR == '.env Ninja!' @@ -57,7 +58,7 @@ DEFAULT.global_ini == true ` var dotEnvAndJSONExpectation = `Hello Horatio! -How do you like it in "/Users/runeimp/dev/apps/templar"? +How do you like it in "/Users/runeimp/Dropbox/Profile/Home/dev/apps/templar"? ENV_FILE_COMMENT == '' ENV_FILE_VAR == '.env Ninja!' @@ -73,86 +74,53 @@ DEFAULT.global_ini == ` +var noDotEnvExpectation = `Hello runeimp! +How do you like it in "/Users/runeimp/Dropbox/Profile/Home/dev/apps/templar"? + + ENV_FILE_COMMENT == '' + ENV_FILE_VAR == '' + CLI_ENV_VAR == 'Sound and fury' + CLI_VAR == 'As you like it' + boolean == + one.two.three == + numbers.two == + all == + words.all == + POSIX == +DEFAULT.global_ini == + +` + func TestTemplar(t *testing.T) { - tests := []struct { - name string - checkDotEnv bool - data []string - template string - want string - }{ - {name: ".env", template: "example.tmpl", checkDotEnv: true, want: dotEnvExpectation}, - {name: ".env and example.ini", template: "example.tmpl", checkDotEnv: true, data: []string{"example.ini"}, want: dotEnvAndINIExpectation}, - {name: ".env and example.json", template: "example.tmpl", checkDotEnv: true, data: []string{"example.json"}, want: dotEnvAndJSONExpectation}, - {name: "example.env", template: "example.tmpl", checkDotEnv: false, data: []string{"example.env"}, want: noDotEnvExpectation}, - } - - for _, tc := range tests { - Reinitialize() - os.Setenv("CLI_ENV_VAR", "Sound and fury") - os.Setenv("CLI_VAR", "As you like it") - if len(tc.data) == 0 { - InitData(tc.checkDotEnv) - } else { - for _, file := range tc.data { - InitData(tc.checkDotEnv, file) - } - } - got, _ := Render(tc.template) - if tc.want != got { - t.Fatalf(`"%s": expected: %v, got: %v`, tc.name, tc.want, got) - } - } + tests := []struct { + name string + checkDotEnv bool + data []string + template string + want string + }{ + {name: ".env", template: "example.tmpl", checkDotEnv: true, want: dotEnvExpectation}, + {name: ".env and example.ini", template: "example.tmpl", checkDotEnv: true, data: []string{"example.ini"}, want: dotEnvAndINIExpectation}, + {name: ".env and example.json", template: "example.tmpl", checkDotEnv: true, data: []string{"example.json"}, want: dotEnvAndJSONExpectation}, + {name: "example.env", template: "example.tmpl", checkDotEnv: false, data: []string{"example.env"}, want: exampleDotEnvExpectation}, + {name: "no.env", template: "example.tmpl", checkDotEnv: false, want: noDotEnvExpectation}, + } + + for _, tc := range tests { + debug := DebugWarn + Reinitialize(debug) + os.Setenv("CLI_ENV_VAR", "Sound and fury") + os.Setenv("CLI_VAR", "As you like it") + if len(tc.data) == 0 { + InitData(tc.checkDotEnv) + } else { + for _, file := range tc.data { + InitData(tc.checkDotEnv, file) + } + } + got, _ := Render(tc.template) + if tc.want != got { + t.Fatalf(fmt.Sprintf("%q:\n\texpected: %v\n\tgot: %v\n\t | tc.checkDotEnv = %t\n", tc.name, tc.want, got, tc.checkDotEnv)) + } + } } - -// func TestDotEnv(t *testing.T) { -// Reinitialize() -// os.Setenv("CLI_ENV_VAR", "Sound and fury") -// os.Setenv("CLI_VAR", "As you like it") -// checkDotEnv := true -// want := dotEnvExpectation -// InitData(checkDotEnv) -// got, _ := Render("example.tmpl") -// if got != want { -// t.Fatalf("expected: %v, got: %v", want, got) -// } -// } - -// func TestDotEnvAndINI(t *testing.T) { -// Reinitialize() -// os.Setenv("CLI_ENV_VAR", "Sound and fury") -// os.Setenv("CLI_VAR", "As you like it") -// checkDotEnv := true -// want := dotEnvAndINIExpectation -// InitData(checkDotEnv, "example.ini") -// got, _ := Render("example.tmpl") -// if got != want { -// t.Fatalf("expected: %v, got: %v", want, got) -// } -// } - -// func TestDotEnvAndJSON(t *testing.T) { -// Reinitialize() -// os.Setenv("CLI_ENV_VAR", "Sound and fury") -// os.Setenv("CLI_VAR", "As you like it") -// checkDotEnv := true -// want := dotEnvAndJSONExpectation -// InitData(checkDotEnv, "example.json") -// got, _ := Render("example.tmpl") -// if got != want { -// t.Fatalf("expected: %v, got: %v", want, got) -// } -// } - -// func TestNoDotEnv(t *testing.T) { -// Reinitialize() -// os.Setenv("CLI_ENV_VAR", "Sound and fury") -// os.Setenv("CLI_VAR", "As you like it") -// checkDotEnv := false -// want := noDotEnvExpectation -// InitData(checkDotEnv, "example.env") -// got, _ := Render("example.tmpl") -// if got != want { -// t.Fatalf("expected: %v, got: %v", want, got) -// } -// }