-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5c7c0a2
Showing
16 changed files
with
736 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist/ | ||
tmpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Make sure to check the documentation at http://goreleaser.com | ||
before: | ||
hooks: | ||
# You may remove this if you don't use go modules. | ||
- go mod tidy | ||
builds: | ||
- env: | ||
- CGO_ENABLED=0 | ||
goos: | ||
- linux | ||
- windows | ||
- darwin | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
archives: | ||
- format: zip | ||
checksum: | ||
name_template: "checksums.txt" | ||
snapshot: | ||
name_template: "{{ .Tag }}-next" | ||
changelog: | ||
sort: asc | ||
filters: | ||
exclude: | ||
- "^docs:" | ||
- "^test:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright 2022 Krakozaure | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# tmpl | ||
|
||
`tmpl` allows to apply variables from JSON/TOML/YAML files, | ||
environment variables or CLI arguments to template files using Golang | ||
`text/template` and functions from the Sprig project. | ||
|
||
## Project status | ||
|
||
This project is in a very early stage. Things might break ! | ||
|
||
## Usage | ||
|
||
For simplicity, everything is output on `stdout`. To write the result in a | ||
file, use shell redirection. | ||
|
||
If a variable is not found and the strict mode is disabled (default disabled), | ||
each missing value will be replaced with an empty string. | ||
|
||
- Print help/usage message. | ||
|
||
```sh | ||
# $ tmpl | ||
# $ tmpl -h | ||
$ tmpl --help | ||
USAGE: tmpl [OPTIONS] INPUT | ||
|
||
INPUT is a template file or '-' for stdin | ||
|
||
OPTIONS: | ||
-e load variables from environment (default true) | ||
-f value | ||
load variables from JSON/TOML/YAML files (format: file path) | ||
-s exit on any error during template processing (default false) | ||
-v value | ||
use one or more variables from the command line (format: name=value) | ||
``` | ||
|
||
- `stdin` and environment variables. | ||
|
||
```sh | ||
$ echo "Editor = {{ .Env.EDITOR }}, Shell = {{ .Env.SHELL }}" | tmpl - | ||
Editor = nvim, Shell = /bin/bash | ||
``` | ||
|
||
- `stdin` and CLI variables. | ||
|
||
```sh | ||
$ echo "Hello, {{ .foo }} !" | tmpl -v foo=bar - | ||
Hello, bar ! | ||
``` | ||
|
||
- Sample from this repository | ||
|
||
Output is not pasted here because of its length. | ||
|
||
```sh | ||
$ tmpl -f ./stores/data.yaml ./inputs/sample.txt.tmpl | ||
``` | ||
|
||
## Configuration | ||
|
||
No configuration needed. Everything is done on the command line. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Allow multiple string values for one flag | ||
type stringsArray []string | ||
|
||
func (s *stringsArray) String() string { | ||
return fmt.Sprintf("%v", *s) | ||
} | ||
|
||
func (s *stringsArray) Set(value string) error { | ||
*s = append(*s, value) | ||
return nil | ||
} | ||
|
||
// Flags | ||
var ( | ||
VarsList stringsArray | ||
FilesList stringsArray | ||
UseEnv bool | ||
Strict bool | ||
) | ||
|
||
func initFlags() { | ||
|
||
flag.BoolVar( | ||
&UseEnv, | ||
"e", | ||
true, | ||
"load variables from environment", | ||
) | ||
flag.Var( | ||
&FilesList, | ||
"f", | ||
"load variables from JSON/TOML/YAML files (format: file path)", | ||
) | ||
flag.Var( | ||
&VarsList, | ||
"v", | ||
"use one or more variables from the command line (format: name=value)", | ||
) | ||
|
||
flag.BoolVar( | ||
&Strict, | ||
"s", | ||
false, | ||
"exit on any error during template processing (default false)", | ||
) | ||
|
||
flag.Usage = func() { | ||
fmt.Fprintf( | ||
os.Stderr, | ||
`USAGE: %s [OPTIONS] INPUT | ||
INPUT is a template file or '-' for stdin | ||
OPTIONS: | ||
`, | ||
os.Args[0], | ||
) | ||
flag.PrintDefaults() | ||
} | ||
|
||
flag.Parse() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var ctx = make(map[string]interface{}) | ||
|
||
func loadContext(input string) { | ||
ctx["__includeDir__"] = getIncludeDir(input) | ||
|
||
if UseEnv { | ||
ctx["Env"] = getEnvVariables() | ||
} | ||
|
||
for _, file := range FilesList { | ||
for k, v := range getFileVariables(file) { | ||
ctx[k] = v | ||
} | ||
} | ||
|
||
for k, v := range getCliVariables() { | ||
ctx[k] = v | ||
} | ||
} | ||
|
||
func getIncludeDir(input string) string { | ||
// If input is stdin, | ||
// template paths are relative to the current working directory | ||
// else relative to the input directory | ||
if input == "-" { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return "." | ||
} else { | ||
return cwd | ||
} | ||
} else { | ||
return filepath.Dir(input) | ||
} | ||
} | ||
|
||
func getEnvVariables() map[string]string { | ||
vars := make(map[string]string) | ||
for _, env := range os.Environ() { | ||
kv := strings.SplitN(env, "=", 2) | ||
vars[kv[0]] = kv[1] | ||
} | ||
return vars | ||
} | ||
|
||
func getFileVariables(file string) map[string]interface{} { | ||
vars := make(map[string]interface{}) | ||
|
||
bytes, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
fmt.Fprint(os.Stderr, fmt.Errorf("unable to read file\n%v\n", err)) | ||
return vars | ||
} | ||
|
||
if strings.HasSuffix(file, ".json") { | ||
err = json.Unmarshal(bytes, &vars) | ||
} else if strings.HasSuffix(file, ".toml") { | ||
err = toml.Unmarshal(bytes, &vars) | ||
} else if strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml") { | ||
err = yaml.Unmarshal(bytes, &vars) | ||
} else { | ||
err = fmt.Errorf("bad file type: %s", file) | ||
} | ||
if err != nil { | ||
fmt.Fprint(os.Stderr, fmt.Errorf("unable to load data\n%v\n", err)) | ||
} | ||
return vars | ||
} | ||
|
||
func getCliVariables() map[string]string { | ||
vars := make(map[string]string) | ||
for _, pair := range VarsList { | ||
kv := strings.SplitN(pair, "=", 2) | ||
|
||
v := kv[1] | ||
if strings.HasPrefix(v, "\"") && strings.HasSuffix(v, "\"") { | ||
v = v[1 : len(v)-1] | ||
} else if strings.HasPrefix(v, "'") && strings.HasSuffix(v, "'") { | ||
v = v[1 : len(v)-1] | ||
} | ||
|
||
vars[kv[0]] = v | ||
} | ||
return vars | ||
} |
Oops, something went wrong.