Skip to content

Commit ab3c14e

Browse files
committed
use cobra and makefile
1 parent 54b04d9 commit ab3c14e

7 files changed

Lines changed: 124 additions & 77 deletions

File tree

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# =================================================================
2+
#
3+
# Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
4+
# Released as open source under the MIT License. See LICENSE file.
5+
#
6+
# =================================================================
7+
8+
bin/gotmpl_darwin_amd64:
9+
GOOS=darwin GOARCH=amd64 go build -o bin/gotmpl_darwin_amd64 $$(go list ./...)
10+
11+
bin/gotmpl_linux_amd64:
12+
GOOS=linux GOARCH=amd64 go build -o bin/gotmpl_linux_amd64 $$(go list ./...)
13+
14+
bin/gotmpl_windows_amd64.exe:
15+
GOOS=windows GOARCH=amd64 go build -o bin/gotmpl_windows_amd64.exe $$(go list ./...)
16+
17+
bin/gotmpl_linux_arm64:
18+
GOOS=linux GOARCH=arm64 go build -o bin/gotmpl_linux_arm64 $$(go list ./...)
19+
20+
build: \
21+
bin/gotmpl_darwin_amd64 \
22+
bin/gotmpl_linux_amd64 \
23+
bin/gotmpl_windows_amd64.exe \
24+
bin/gotmpl_linux_arm64
25+
26+
fmt:
27+
go fmt $$(go list ./... )
28+
29+
install:
30+
go install $$(go list ./...)
31+
32+
test:
33+
bash scripts/test.sh
34+
35+
clean:
36+
rm -fr bin

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
No installation is required. Just grab a [release](https://github.com/spatialcurrent/gotmpl/releases). You might want to rename your binary to just `gotmpl` for convenience.
1212

13-
If you do have go already installed, you can just run using `go run main.go` or install with `bash scripts/install.sh`
13+
If you do have go already installed, you can just run using `go run main.go` or install with `make install`
1414

1515
# Usage
1616

@@ -38,6 +38,10 @@ echo '{{ .PATH | split ":" | set | array | sort | join ":" }}' | gotmpl
3838
echo '{{ with $items := .data | parse "csv" }}<table style="text-align:left;font-size:16px;"><tr><th>Time</th><th>Title</th>{{ range $items }}<tr><td>{{ .Time }}</td><td>{{ .Title }}</td></tr>{{ end }}</table>{{ end }}' | data=$(cat timetable.csv) gotmpl
3939
```
4040

41+
# Testing
42+
43+
Run tests with `make test` (or `bash scripts/test.sh`), which runs unit tests, `go vet`, `go vet with shadow`, [errcheck](https://github.com/kisielk/errcheck), [ineffassign](https://github.com/gordonklaus/ineffassign), [staticcheck](https://staticcheck.io/), and [misspell](https://github.com/client9/misspell).
44+
4145
# Contributing
4246

4347
[Spatial Current, Inc.](https://spatialcurrent.io) is currently accepting pull requests for this repository. We'd love to have your contributions! Please see [Contributing.md](https://github.com/spatialcurrent/gotmpl/blob/master/CONTRIBUTING.md) for how to get started.

main.go

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,15 @@ import (
1717

1818
import (
1919
"github.com/pkg/errors"
20+
"github.com/spf13/cobra"
2021
)
2122

2223
import (
2324
"github.com/spatialcurrent/go-adaptive-functions/af"
2425
"github.com/spatialcurrent/go-simple-serializer/gss"
2526
)
2627

27-
func main() {
28-
29-
fi, err := os.Stdin.Stat()
30-
if err != nil {
31-
panic(err)
32-
}
33-
34-
if fi.Mode()&os.ModeNamedPipe == 0 && !fi.Mode().IsRegular() {
35-
fmt.Println("Usage: TEMPLATE_TEXT | gotmpl")
36-
fmt.Println("Usage: gotmpl < TEMPLATE_FILE")
37-
return
38-
}
39-
40-
text, err := ioutil.ReadAll(os.Stdin)
41-
if err != nil {
42-
panic(err)
43-
}
44-
45-
ctx := map[string]string{}
46-
for _, str := range os.Environ() {
47-
parts := strings.SplitN(str, "=", 2)
48-
ctx[parts[0]] = parts[1]
49-
}
50-
28+
func initFunctions() map[string]interface{} {
5129
funcs := map[string]interface{}{
5230
"parse": func(args ...interface{}) (interface{}, error) {
5331
if len(args) != 2 {
@@ -75,6 +53,7 @@ func main() {
7553
return nil, errors.New("invalid arguments for parse " + fmt.Sprint(args))
7654
},
7755
}
56+
7857
for _, f := range af.Functions {
7958
f := f
8059
for _, alias := range f.Aliases {
@@ -88,13 +67,60 @@ func main() {
8867
}
8968
}
9069

91-
tmpl, err := template.New("main").Funcs(funcs).Parse(string(text))
92-
if err != nil {
93-
panic(err)
70+
return funcs
71+
}
72+
73+
func main() {
74+
cmd := &cobra.Command{
75+
Use: "gotmpl [k=v]... < template_file",
76+
DisableFlagsInUseLine: true,
77+
Short: "gotmpl",
78+
Long: `gotmpl is a super simple command line program for rendering templates that uses environment variables and command line arguments as its context variables. The template is read from stdin.`,
79+
RunE: func(cmd *cobra.Command, args []string) error {
80+
81+
fi, err := os.Stdin.Stat()
82+
if err != nil {
83+
return err
84+
}
85+
86+
if fi.Mode()&os.ModeNamedPipe == 0 && !fi.Mode().IsRegular() {
87+
return cmd.Usage()
88+
}
89+
90+
text, err := ioutil.ReadAll(os.Stdin)
91+
if err != nil {
92+
return err
93+
}
94+
95+
ctx := map[string]string{}
96+
97+
// load context from environment variables
98+
for _, str := range os.Environ() {
99+
parts := strings.SplitN(str, "=", 2)
100+
ctx[parts[0]] = parts[1]
101+
}
102+
103+
// load context from command line arguments
104+
for _, str := range args {
105+
parts := strings.SplitN(str, "=", 2)
106+
ctx[parts[0]] = parts[1]
107+
}
108+
109+
tmpl, err := template.New("main").Funcs(initFunctions()).Parse(string(text))
110+
if err != nil {
111+
return err
112+
}
113+
114+
err = tmpl.Execute(os.Stdout, ctx)
115+
if err != nil {
116+
return err
117+
}
118+
119+
return nil
120+
},
94121
}
95122

96-
err = tmpl.Execute(os.Stdout, ctx)
97-
if err != nil {
123+
if err := cmd.Execute(); err != nil {
98124
panic(err)
99125
}
100126
}

scripts/build_cli.sh

Lines changed: 0 additions & 16 deletions
This file was deleted.

scripts/format.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/install.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

scripts/test.sh

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
#!/bin/bash
2+
3+
# =================================================================
4+
#
5+
# Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
6+
# Released as open source under the MIT License. See LICENSE file.
7+
#
8+
# =================================================================
9+
210
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
311
set -eu
412
cd $DIR/..
5-
pkgs=$(go list ./... )
13+
pkgs=$(go list ./... | grep -v /vendor/ | tr "\n" " ")
614
echo "******************"
7-
echo "Running unit tests for: "
8-
for pkg in "${pkgs[@]}"; do
9-
go test -p 1 -count 1 -short $pkgs
10-
done
15+
echo "Running unit tests"
16+
go test -p 1 -count 1 -short $pkgs
1117
echo "******************"
12-
echo "Using gometalinter with misspell, vet, ineffassign, and gosec"
13-
gometalinter \
14-
--misspell-locale=US \
15-
--disable-all \
16-
--enable=misspell \
17-
--enable=vet \
18-
--enable=ineffassign \
19-
--enable=gosec \
20-
./...
18+
echo "Running go vet"
19+
go vet $pkgs
20+
echo "******************"
21+
echo "Running go vet with shadow"
22+
go vet -vettool=$(which shadow) $pkgs
23+
echo "******************"
24+
echo "Running errcheck"
25+
errcheck ${pkgs}
26+
echo "******************"
27+
echo "Running ineffassign"
28+
find . -name '*.go' | xargs ineffassign
29+
echo "******************"
30+
echo "Running staticcheck"
31+
staticcheck -checks all ${pkgs}
32+
echo "******************"
33+
echo "Running misspell"
34+
misspell -locale US -error *.md *.go

0 commit comments

Comments
 (0)