Skip to content

Commit

Permalink
added inline option
Browse files Browse the repository at this point in the history
excluded windows from travis
uploading linux and darwin bins on release
removed caps restriction to env vars

removed osx from travis builds too

fixed missing err handling for writer flush
  • Loading branch information
malud committed Sep 2, 2017
1 parent dc043db commit 5c439f8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
15 changes: 6 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
language: go
env:
- GIMME_OS=linux GIMME_ARCH=amd64
- GIMME_OS=darwin GIMME_ARCH=amd64
- GIMME_OS=windows GIMME_ARCH=amd64
# - GIMME_OS=darwin GIMME_ARCH=amd64
# - GIMME_OS=windows GIMME_ARCH=amd64
sudo: required
go:
- 1.6.3
- 1.7
- 1.8.3
- 1.9
- tip
install:
- go get -d -v -t ./...
script:
- export GOMAXPROCS=$(nproc) GOOS=$GIMME_OS GOARCH=$GIMME_ARCH
# TODO test seems to fail on non linux platforms @ travis
# - go test -v -timeout=30s ./...
- go test -v -timeout=30s ./...
- go build -o tg ./cli/cli.go
deploy:
provider: releases
api_key:
secure: lS4Qrv2X+BRhUBUTgFPI1EfzvJDOMFDhHlNhCa1CNT4faA8HDVcLj79HUz6NenD6voT+FgV2UgmPRD/Yx+UkoTkbKAsZGf3j/XlRK3zeb8zPf7ZPulgMGKPVOeoe9S7XLtLL+/Yhu9v3BMpM+qIgto6Pqa1CJgtfh27yGZTwxLQ11rblQ2RCHSxxckZJ+/e7G7J1rR8kSReXULw2QkH9F1YW4Yp725d6v40cM9ReyuGhbBMrwZtPoTQfmasN82JMmWURLZrhvGbPGUQ0msO0xwXae6wQQ6kbn2q5j5PJzrZ8ChgyOMFYEIOFd9U8BAqaLZdbXuqkpw1vEVl59XGO6tZ/d9pQMKSO7Eo4tf+ZbR6/AlQrlWX0zJ0at62vyqLPFgB5ymArOf3xrrTmPor4KKC4X08ibGlJ0Y1HVus2lXxmfUDzdaEdWN6X8AjpHXkp8YA+PWIGi0fNCaStV7rMCeKvztX139SLkBo8aPfXb6qJa91b4u3Cud5j3JpbGEjyHxpjnTvzsdXl5dycz07Pg+qjTukufC0yUVAqYlDgdo2z+F1QSUzPyZIw30xisFfIPnguD9i//9Gw+ENrFbEaOqKqyJKOzeKk9Fxg00/mpvaf2Kfy2sFFpvO8S4d4c7lJ6CyJuf0NJ8nC4ZImpvCYyLsHNOThpObZpdDui/D+XbQ=
file: tg
file:
- tg
skip_cleanup: true
on:
tags: true
matrix:
fast_finish: true
allow_failures:
- go: tip
exclude:
- go: 1.6.3
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ Environment variable based template engine like Jinja2 for your container config

## Usage

Define variables with the format `{{ VAR_IABLE }}` and set them in your environment before **tg** execution.
Define variables with the format `{{ VAR_IABLE }}` inside your template files and set them in your environment before **tg** execution.

Currently with stdin/stdout support.
```
Currently with stdin/stdout and inline support.

```bash
# pipe
TESTVAR=foo cat /opt/templates/file1.ext | tg > /dest/config/file1.ext
# or inline
TESTVAR=foo tg -i /dest/config/file1.ext
```

However, using the inline option on your templates will overwrite them.
It is recommended to use this option on resettable files.

### TODO
* flags for file in/out
* prefix flag for env var e.g. SERVICE_XXX

### Licence
### License

MIT
59 changes: 43 additions & 16 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@
package main

import (
"fmt"
"github.com/malud/temgo/temgo"
"bufio"
"flag"
"io/ioutil"
"os"
"strings"

"github.com/malud/temgo/temgo"
)

var envVars = make(temgo.EnvVars)
var inlineFlag = flag.String("i", "", "-i filename")

// Initialize and filter all non upper case variables.
func init() {
for _, e := range os.Environ() {
string := strings.Split(e, "=")
if strings.Compare(string[0], strings.ToUpper(string[0])) == 0 {
envVars[string[0]] = string[1]
}
envVars[string[0]] = string[1]
}

if !flag.Parsed() {
flag.Parse()
}
}

func main() {
var writeErr error
input := os.Stdin
bytes, err := ioutil.ReadAll(input)
if err != nil {
_, writeErr = os.Stderr.WriteString(fmt.Sprintf("Could not read: %v", err))
var rw *bufio.ReadWriter
var file *os.File
if *inlineFlag != "" {
var err error
file, err = os.OpenFile(*inlineFlag, os.O_RDWR, 644)
must(err)
defer file.Close()
rw = bufio.NewReadWriter(bufio.NewReader(file), bufio.NewWriter(file))
} else {
rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
}

bytes, err := ioutil.ReadAll(rw)
must(err)

if temgo.ContainsVariable(bytes) {
str := envVars.ReplaceVariables(bytes)
_, err = os.Stdout.Write(str)
if err != nil {
_, writeErr = os.Stderr.WriteString(fmt.Sprintf("Could not write: %v", err))
if file != nil {
truncate(file)
}
_, err := rw.Write(str)
must(err)
must(rw.Flush())
}
if writeErr != nil {
panic(writeErr)
}

// fatal
func must(err error) {
if err != nil {
println("Err:", err.Error())
os.Exit(1)
}
}

func truncate(file *os.File) {
err := file.Truncate(0)
must(err)
_, err = file.Seek(0, 0)
must(err)
}

1 comment on commit 5c439f8

@malud
Copy link
Owner Author

@malud malud commented on 5c439f8 Sep 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added inline edit #1

Please sign in to comment.