Skip to content

Commit 5836508

Browse files
committed
initial work
0 parents  commit 5836508

12 files changed

Lines changed: 278 additions & 0 deletions

File tree

.circleci/config.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
version: 2.1
2+
executors:
3+
base:
4+
docker:
5+
- image: circleci/golang:1.12
6+
working_directory: /go/src/github.com/spatialcurrent/gotmpl
7+
jobs:
8+
pre_deps_golang:
9+
executor: base
10+
steps:
11+
- checkout
12+
- run: go get -d ./...
13+
- run: go get github.com/inconshreveable/mousetrap # for windows CLI builds
14+
- run: sudo chown -R circleci /go/src
15+
- save_cache:
16+
key: v1-go-src-{{ .Branch }}-{{ .Revision }}
17+
paths:
18+
- /go/src
19+
test:
20+
executor: base
21+
steps:
22+
- run: sudo chown -R circleci /go/src
23+
- restore_cache:
24+
keys:
25+
- v1-go-src-{{ .Branch }}-{{ .Revision }}
26+
- run:
27+
name: Install gometalinter
28+
command: |
29+
go get -u github.com/alecthomas/gometalinter
30+
gometalinter --install
31+
- run:
32+
name: Test
33+
command: bash scripts/test.sh
34+
validate:
35+
executor: base
36+
steps:
37+
- run: sudo chown -R circleci /go/src
38+
- restore_cache:
39+
keys:
40+
- v1-go-src-{{ .Branch }}-{{ .Revision }}
41+
- run:
42+
name: Install Dig
43+
command: sudo apt update && sudo apt install dnsutils
44+
- run:
45+
name: "Update ~/.ssh/known_hosts"
46+
command: |
47+
mkdir ~/.ssh/
48+
touch ~/.ssh/known_hosts
49+
for ip in $(dig @8.8.8.8 github.com +short); do ssh-keyscan github.com,$ip; ssh-keyscan $ip; done 2>/dev/null >> ~/.ssh/known_hosts
50+
- run: go get github.com/spatialcurrent/go-header/...
51+
- run: go install github.com/spatialcurrent/go-header/cmd/goheader
52+
- run: goheader fix --fix-year 2019 --exit-code-on-changes 1 --verbose
53+
build_cli:
54+
executor: base
55+
steps:
56+
- run: sudo chown -R circleci /go/src
57+
- restore_cache:
58+
keys:
59+
- v1-go-src-{{ .Branch }}-{{ .Revision }}
60+
- run: bash scripts/build_cli.sh
61+
- store_artifacts:
62+
path: bin
63+
destination: /
64+
workflows:
65+
main:
66+
jobs:
67+
- pre_deps_golang
68+
- test:
69+
requires:
70+
- pre_deps_golang
71+
- validate:
72+
requires:
73+
- pre_deps_golang
74+
- build_cli:
75+
requires:
76+
- pre_deps_golang

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
secret/
2+
bin/
3+
*.so
4+
*.h

AUTHORS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gotmpl is maintained by Spatial Current, Inc.
2+
3+
Authors:
4+
5+
* Patrick Dufour (pjdufour)

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributing to gotmpl
2+
3+
## Contributor License Agreement
4+
5+
Thank you for your interest in contributing. You will first need to agree to the license. Simply post the following paragraph, with "your name" and "your github account" substituted as needed, to the first issue [#1](https://github.com/spatialcurrent/gotmpl/issues/1). Otherwise, you can email us [here](mailto:opensource@spatialcurrent.io?subject=CLA).
6+
7+
I, **< YOUR NAME > (< YOUR GITHUB ACCOUNT >)**, agree to the license terms. My contributions to this repo are granted to **Spatial Current, Inc.** under a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable license and/or copyright is transferred.
8+
9+
## Versioning
10+
11+
This library is still in alpha (0.x.x), so versioning is not semantic yet.
12+
13+
## Authors
14+
15+
See [AUTHORS](https://github.com/spatialcurrent/gotmpl/blob/master/AUTHORS) for a list of contributors.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Spatial Current, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[![CircleCI](https://circleci.com/gh/spatialcurrent/gotmpl/tree/master.svg?style=svg)](https://circleci.com/gh/spatialcurrent/gotmpl/tree/master) [![Go Report Card](https://goreportcard.com/badge/spatialcurrent/gotmpl)](https://goreportcard.com/report/spatialcurrent/gotmpl) [![GoDoc](https://godoc.org/github.com/spatialcurrent/gotmpl?status.svg)](https://godoc.org/github.com/spatialcurrent/gotmpl) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://github.com/spatialcurrent/gotmpl/blob/master/LICENSE)
2+
3+
# gotmpl
4+
5+
# Description
6+
7+
**gotmpl** is a super simple command line program for rendering templates. **gotmpl** uses environment variables as its context and [go-adaptive-functions](https://github.com/spatialcurrent/go-adaptive-functions) for its functions.
8+
9+
# Installation
10+
11+
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.
12+
13+
If you do have go already installed, you can just run using `go run main.go` or install with `bash scripts/install.sh`
14+
15+
# Usage
16+
17+
See the few examples below.
18+
19+
**Note**: Since Go templates add the piped value to the end of the positional argument array, **gotmpl** reorders the piped value to the beginning of the argument array, so **go-adaptive-functions** can be used. This leads to a more seamless pattern, particularly for functions such as `split`, `join`, etc.
20+
21+
# Examples
22+
23+
**Get Shell**
24+
25+
```shell
26+
echo '{{ split .SHELL "/" | last }}' | go run main.go
27+
```
28+
29+
**Clean Path**
30+
31+
```shell
32+
echo '{{ .PATH | split ":" | set | array | sort | join ":" }}' | go run main.go
33+
```
34+
35+
# Contributing
36+
37+
[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.
38+
39+
# License
40+
41+
This work is distributed under the **MIT License**. See **LICENSE** file.

main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"strings"
8+
"text/template"
9+
)
10+
11+
import (
12+
"github.com/spatialcurrent/go-adaptive-functions/af"
13+
)
14+
15+
func main() {
16+
17+
fi, err := os.Stdin.Stat()
18+
if err != nil {
19+
panic(err)
20+
}
21+
22+
if fi.Mode()&os.ModeNamedPipe == 0 && !fi.Mode().IsRegular() {
23+
fmt.Println("Usage: TEMPLATE_TEXT | gotmpl")
24+
fmt.Println("Usage: gotmpl < TEMPLATE_FILE")
25+
return
26+
}
27+
28+
text, err := ioutil.ReadAll(os.Stdin)
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
ctx := map[string]string{}
34+
for _, str := range os.Environ() {
35+
parts := strings.SplitN(str, "=", 2)
36+
ctx[parts[0]] = parts[1]
37+
}
38+
39+
funcs := map[string]interface{}{}
40+
for _, f := range af.Functions {
41+
f := f
42+
for _, alias := range f.Aliases {
43+
alias := alias
44+
funcs[alias] = func(args ...interface{}) (interface{}, error) {
45+
if len(args) <= 1 {
46+
return f.ValidateRun(args)
47+
}
48+
return f.ValidateRun(append([]interface{}{args[len(args)-1]}, args[0:len(args)-1]...))
49+
}
50+
}
51+
}
52+
53+
tmpl, err := template.New("main").Funcs(funcs).Parse(string(text))
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
err = tmpl.Execute(os.Stdout, ctx)
59+
if err != nil {
60+
panic(err)
61+
}
62+
}

scripts/build_cli.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3+
set -eu
4+
DEST=${1:-$DIR/../bin}
5+
mkdir -p $DEST
6+
echo "******************"
7+
echo "Building program for gotmpl"
8+
cd $DEST
9+
for GOOS in linux darwin windows; do
10+
GOOS=${GOOS} GOARCH=amd64 go build -o "gotmpl_${GOOS}_amd64" github.com/spatialcurrent/gotmpl
11+
done
12+
if [[ "$?" != 0 ]] ; then
13+
echo "Error building program for gotmpl"
14+
exit 1
15+
fi
16+
echo "Executables built at $DEST"

scripts/format.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3+
cd $DIR/..
4+
echo "******************"
5+
echo "Formatting"
6+
go fmt $(go list ./... )

scripts/install.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3+
set -eu
4+
cd $DIR/..
5+
pkgs=$(go list ./...)
6+
echo "******************"
7+
echo "Installing programs"
8+
for pkg in "${pkgs[@]}"; do
9+
echo "Installing $(basename $pkg) from $pkg"
10+
go install $pkg
11+
done

0 commit comments

Comments
 (0)