From 945715f5ca58564d97ff5138f9925b2e00f0782d Mon Sep 17 00:00:00 2001 From: Lucas Santos Date: Mon, 3 Jul 2023 16:18:24 +0000 Subject: [PATCH] :M: Adding source code and Makefile --- .gitignore | 1 + .monova.config | 11 ++++++++ Makefile | 27 +++++++++++++++++++ README.md | 29 ++++++++++++++++++++ bin/.gitignore | 2 ++ cmd/root.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root_test.go | 33 +++++++++++++++++++++++ cmd/version.go | 23 ++++++++++++++++ go.mod | 16 +++++++++++ go.sum | 63 +++++++++++++++++++++++++++++++++++++++++++ main.go | 7 +++++ 11 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 .monova.config create mode 100644 Makefile create mode 100644 bin/.gitignore create mode 100644 cmd/root.go create mode 100644 cmd/root_test.go create mode 100644 cmd/version.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7538b1e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.monova.history diff --git a/.monova.config b/.monova.config new file mode 100644 index 0000000..bc31816 --- /dev/null +++ b/.monova.config @@ -0,0 +1,11 @@ +{ + "MajorKeys": [ + ":major:", ":M:" + ], + "MinorKeys": [ + ":minor:", ":m:" + ], + "PatchKeys": [ + ":patch:", ":p:" + ] +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cf69db8 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +PWD:=$(shell pwd) +VERSION=0.0.0 +MONOVA:=$(shell which monova dot 2> /dev/null) + +version: +ifdef MONOVA +override VERSION=$(shell monova) +else + $(info "Install monova (https://github.com/jsnjack/monova) to calculate version") +endif + +bin/template-renderer: version main.go cmd/*.go + CGO_ENABLED=0 go build -ldflags="-X github.com/surfly/template-renderer/cmd.Version=${VERSION}" -o bin/template-renderer + +test: + cd cmd && go test + +build: test bin/template-renderer + +release: build + grm release surfly/template-renderer -f bin/template-renderer -t "v`monova`" + +.ONESHELL: +clean: + rm -rf bin/* + +.PHONY: version release build test diff --git a/README.md b/README.md index 5a78276..0b18a3a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ # template-renderer + +``` Standalone golang wrapper for rendering Jinja2 templates + +Usage: + template-renderer [flags] + template-renderer [command] + +Available Commands: + help Help about any command + version Print version + +Flags: + -h, --help help for template-renderer + -i, --input string Input template file (Required) + -o, --output string Outfile to write the outcome (Prints to stdout by default) + +Use "template-renderer [command] --help" for more information about a command. +``` + +Usage examples: +- Templating to stdout: +``` +./bin/template-renderer -i test.tpl +testing value: foo +``` +- Templating to an output file: +``` +./bin/template-renderer -i test.tpl -o test.txt +``` diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..6f3f84d --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,70 @@ +package cmd + +import ( + "os" + "fmt" + "strings" + + "github.com/noirbizarre/gonja" + "github.com/spf13/cobra" +) + +var inputFile string +var outputFile string + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "template-renderer", + Short: "Standalone golang wrapper for rendering Jinja2 templates", + + // Uncomment the following line if your bare application + // has an action associated with it: + RunE: renderTemplate, +} + +func renderTemplate(cmd *cobra.Command, args []string) error { + + var tpl = gonja.Must(gonja.FromFile(inputFile)) + + ctx := gonja.Context{} + for _, element := range os.Environ() { + variable := strings.Split(element, "=") + ctx[variable[0]] = variable[1] + } + out, err := tpl.Execute(ctx) + if err != nil { + return err + } + if outputFile == "" { + fmt.Printf(out) + } else { + err := os.WriteFile(outputFile, []byte(out), 0644) + if err != nil { + return err + } + } + + return nil +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + rootCmd.CompletionOptions.DisableDefaultCmd = true + rootCmd.Flags().StringVarP(&inputFile, "input", "i", "", "Input template file (Required)") + rootCmd.MarkFlagRequired("input") + rootCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Outfile to write the outcome (Prints to stdout by default)") +} + + diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..89bb45e --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "os" + "testing" +) + +func TestUtils_RenderTemplate(t *testing.T) { + var templateString string = "{{ TEST_STRING | replace('this', 'that', 1) }}" + var expectedOutput string = "Something better than that" + os.Setenv("TEST_STRING", "Something better than this") + + inputFile = "/tmp/template-renderer-test.in" + outputFile = "/tmp/template-renderer-test.out" + + os.Remove(inputFile) + err := os.WriteFile(inputFile, []byte(templateString), 0644) + if err != nil { + t.Errorf("Unexpected error: %s", err) + } + defer os.Remove(inputFile) + + renderTemplate(nil, []string{}) + defer os.Remove(outputFile) + + b, err := os.ReadFile(outputFile) + + if string(b) != expectedOutput { + t.Errorf("Expected '%s', got '%s'", expectedOutput, string(b)) + } +} + + diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..816f3dc --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// Version is the version of the application calculated with monova +var Version string + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(Version) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..072c409 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module github.com/surfly/template-renderer + +go 1.19 + +require ( + github.com/goph/emperror v0.17.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect + github.com/noirbizarre/gonja v0.0.0-20200629003239-4d051fd0be61 // indirect + github.com/pkg/errors v0.8.1 // indirect + github.com/sirupsen/logrus v1.3.0 // indirect + github.com/spf13/cobra v1.7.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 // indirect + golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cef78bf --- /dev/null +++ b/go.sum @@ -0,0 +1,63 @@ +github.com/airbrake/gobrake v3.6.1+incompatible/go.mod h1:wM4gu3Cn0W0K7GUuVWnlXZU11AGBXMILnrdOU8Kn00o= +github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= +github.com/bmuller/arrow v0.0.0-20180318014521-b14bfde8dff2/go.mod h1:+voQMVaya0tr8p3W33Qxj/dKOjZNCepW+k8JJvt91gk= +github.com/bugsnag/bugsnag-go v1.4.0/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8= +github.com/bugsnag/panicwrap v1.2.0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE= +github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= +github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98= +github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/goph/emperror v0.17.1 h1:6lOybhIvG/BB6VGoWfdv30FVZeZFBBZ9VvgzGXLVkyY= +github.com/goph/emperror v0.17.1/go.mod h1:+ZbQ+fUNO/6FNiUo0ujtMjhgad9Xa6fQL9KhH4LNHic= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/noirbizarre/gonja v0.0.0-20200629003239-4d051fd0be61 h1:8HaKr2WO2B5XKEFbJE9Z7W8mWC6+dL3jZCw53Dbl0oI= +github.com/noirbizarre/gonja v0.0.0-20200629003239-4d051fd0be61/go.mod h1:WboHq+I9Ck8PwKsVFJNrpiRyngXhquRSTWBGwuSWOrg= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rollbar/rollbar-go v1.0.2/go.mod h1:AcFs5f0I+c71bpHlXNNDbOWJiKwjFDtISeXco0L5PKQ= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= +github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..894db31 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/surfly/template-renderer/cmd" + +func main() { + cmd.Execute() +}