Skip to content

Commit 945715f

Browse files
committed
:M: Adding source code and Makefile
1 parent 6de46e9 commit 945715f

11 files changed

Lines changed: 282 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.monova.history

.monova.config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"MajorKeys": [
3+
":major:", ":M:"
4+
],
5+
"MinorKeys": [
6+
":minor:", ":m:"
7+
],
8+
"PatchKeys": [
9+
":patch:", ":p:"
10+
]
11+
}

Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
PWD:=$(shell pwd)
2+
VERSION=0.0.0
3+
MONOVA:=$(shell which monova dot 2> /dev/null)
4+
5+
version:
6+
ifdef MONOVA
7+
override VERSION=$(shell monova)
8+
else
9+
$(info "Install monova (https://github.com/jsnjack/monova) to calculate version")
10+
endif
11+
12+
bin/template-renderer: version main.go cmd/*.go
13+
CGO_ENABLED=0 go build -ldflags="-X github.com/surfly/template-renderer/cmd.Version=${VERSION}" -o bin/template-renderer
14+
15+
test:
16+
cd cmd && go test
17+
18+
build: test bin/template-renderer
19+
20+
release: build
21+
grm release surfly/template-renderer -f bin/template-renderer -t "v`monova`"
22+
23+
.ONESHELL:
24+
clean:
25+
rm -rf bin/*
26+
27+
.PHONY: version release build test

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
# template-renderer
2+
3+
```
24
Standalone golang wrapper for rendering Jinja2 templates
5+
6+
Usage:
7+
template-renderer [flags]
8+
template-renderer [command]
9+
10+
Available Commands:
11+
help Help about any command
12+
version Print version
13+
14+
Flags:
15+
-h, --help help for template-renderer
16+
-i, --input string Input template file (Required)
17+
-o, --output string Outfile to write the outcome (Prints to stdout by default)
18+
19+
Use "template-renderer [command] --help" for more information about a command.
20+
```
21+
22+
Usage examples:
23+
- Templating to stdout:
24+
```
25+
./bin/template-renderer -i test.tpl
26+
testing value: foo
27+
```
28+
- Templating to an output file:
29+
```
30+
./bin/template-renderer -i test.tpl -o test.txt
31+
```

bin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

cmd/root.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/noirbizarre/gonja"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var inputFile string
13+
var outputFile string
14+
15+
// rootCmd represents the base command when called without any subcommands
16+
var rootCmd = &cobra.Command{
17+
Use: "template-renderer",
18+
Short: "Standalone golang wrapper for rendering Jinja2 templates",
19+
20+
// Uncomment the following line if your bare application
21+
// has an action associated with it:
22+
RunE: renderTemplate,
23+
}
24+
25+
func renderTemplate(cmd *cobra.Command, args []string) error {
26+
27+
var tpl = gonja.Must(gonja.FromFile(inputFile))
28+
29+
ctx := gonja.Context{}
30+
for _, element := range os.Environ() {
31+
variable := strings.Split(element, "=")
32+
ctx[variable[0]] = variable[1]
33+
}
34+
out, err := tpl.Execute(ctx)
35+
if err != nil {
36+
return err
37+
}
38+
if outputFile == "" {
39+
fmt.Printf(out)
40+
} else {
41+
err := os.WriteFile(outputFile, []byte(out), 0644)
42+
if err != nil {
43+
return err
44+
}
45+
}
46+
47+
return nil
48+
}
49+
50+
// Execute adds all child commands to the root command and sets flags appropriately.
51+
// This is called by main.main(). It only needs to happen once to the rootCmd.
52+
func Execute() {
53+
err := rootCmd.Execute()
54+
if err != nil {
55+
os.Exit(1)
56+
}
57+
}
58+
59+
func init() {
60+
// Here you will define your flags and configuration settings.
61+
// Cobra supports persistent flags, which, if defined here,
62+
// will be global for your application.
63+
64+
rootCmd.CompletionOptions.DisableDefaultCmd = true
65+
rootCmd.Flags().StringVarP(&inputFile, "input", "i", "", "Input template file (Required)")
66+
rootCmd.MarkFlagRequired("input")
67+
rootCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Outfile to write the outcome (Prints to stdout by default)")
68+
}
69+
70+

cmd/root_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestUtils_RenderTemplate(t *testing.T) {
9+
var templateString string = "{{ TEST_STRING | replace('this', 'that', 1) }}"
10+
var expectedOutput string = "Something better than that"
11+
os.Setenv("TEST_STRING", "Something better than this")
12+
13+
inputFile = "/tmp/template-renderer-test.in"
14+
outputFile = "/tmp/template-renderer-test.out"
15+
16+
os.Remove(inputFile)
17+
err := os.WriteFile(inputFile, []byte(templateString), 0644)
18+
if err != nil {
19+
t.Errorf("Unexpected error: %s", err)
20+
}
21+
defer os.Remove(inputFile)
22+
23+
renderTemplate(nil, []string{})
24+
defer os.Remove(outputFile)
25+
26+
b, err := os.ReadFile(outputFile)
27+
28+
if string(b) != expectedOutput {
29+
t.Errorf("Expected '%s', got '%s'", expectedOutput, string(b))
30+
}
31+
}
32+
33+

cmd/version.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Version is the version of the application calculated with monova
10+
var Version string
11+
12+
// versionCmd represents the version command
13+
var versionCmd = &cobra.Command{
14+
Use: "version",
15+
Short: "Print version",
16+
Run: func(cmd *cobra.Command, args []string) {
17+
fmt.Println(Version)
18+
},
19+
}
20+
21+
func init() {
22+
rootCmd.AddCommand(versionCmd)
23+
}

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/surfly/template-renderer
2+
3+
go 1.19
4+
5+
require (
6+
github.com/goph/emperror v0.17.1 // indirect
7+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
8+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
9+
github.com/noirbizarre/gonja v0.0.0-20200629003239-4d051fd0be61 // indirect
10+
github.com/pkg/errors v0.8.1 // indirect
11+
github.com/sirupsen/logrus v1.3.0 // indirect
12+
github.com/spf13/cobra v1.7.0 // indirect
13+
github.com/spf13/pflag v1.0.5 // indirect
14+
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 // indirect
15+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 // indirect
16+
)

go.sum

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
github.com/airbrake/gobrake v3.6.1+incompatible/go.mod h1:wM4gu3Cn0W0K7GUuVWnlXZU11AGBXMILnrdOU8Kn00o=
2+
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
3+
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
4+
github.com/bmuller/arrow v0.0.0-20180318014521-b14bfde8dff2/go.mod h1:+voQMVaya0tr8p3W33Qxj/dKOjZNCepW+k8JJvt91gk=
5+
github.com/bugsnag/bugsnag-go v1.4.0/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
6+
github.com/bugsnag/panicwrap v1.2.0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
7+
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
8+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
11+
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
12+
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
13+
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
14+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
15+
github.com/goph/emperror v0.17.1 h1:6lOybhIvG/BB6VGoWfdv30FVZeZFBBZ9VvgzGXLVkyY=
16+
github.com/goph/emperror v0.17.1/go.mod h1:+ZbQ+fUNO/6FNiUo0ujtMjhgad9Xa6fQL9KhH4LNHic=
17+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
18+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
19+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
20+
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
21+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
22+
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
23+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
24+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
25+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
26+
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
27+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
28+
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
29+
github.com/noirbizarre/gonja v0.0.0-20200629003239-4d051fd0be61 h1:8HaKr2WO2B5XKEFbJE9Z7W8mWC6+dL3jZCw53Dbl0oI=
30+
github.com/noirbizarre/gonja v0.0.0-20200629003239-4d051fd0be61/go.mod h1:WboHq+I9Ck8PwKsVFJNrpiRyngXhquRSTWBGwuSWOrg=
31+
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
32+
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
33+
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
34+
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
35+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
36+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
37+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
38+
github.com/rollbar/rollbar-go v1.0.2/go.mod h1:AcFs5f0I+c71bpHlXNNDbOWJiKwjFDtISeXco0L5PKQ=
39+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
40+
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
41+
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
42+
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
43+
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
44+
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
45+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
46+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
47+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
48+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
49+
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
50+
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
51+
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
52+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
53+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
54+
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
55+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
56+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
57+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
58+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
59+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
60+
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
61+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
62+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
63+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)