From 5c439f89535ed82d38c2dacf11d81b34ad3cbe44 Mon Sep 17 00:00:00 2001 From: Marcel Ludwig Date: Sat, 2 Sep 2017 10:25:46 +0200 Subject: [PATCH] added inline option 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 --- .travis.yml | 15 ++++++-------- README.md | 15 ++++++++++---- cli/cli.go | 59 ++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index 333bef1..0eb96bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,26 +1,25 @@ 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 @@ -28,5 +27,3 @@ matrix: fast_finish: true allow_failures: - go: tip - exclude: - - go: 1.6.3 \ No newline at end of file diff --git a/README.md b/README.md index 2b0e15d..b09ce23 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cli/cli.go b/cli/cli.go index a75be6f..77f9150 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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) +}