├── testdata ├── pwd.tpl └── sprig.tpl ├── _scripts └── cross-build.sh ├── Dockerfile ├── glide.yaml ├── env_test.go ├── env.go ├── tpl.go ├── .gitignore ├── glide.lock ├── README.md ├── main.go ├── tpl_test.go ├── Makefile ├── .travis.yml └── LICENSE /testdata/pwd.tpl: -------------------------------------------------------------------------------- 1 | The current working directory is {{.PWD}} 2 | -------------------------------------------------------------------------------- /testdata/sprig.tpl: -------------------------------------------------------------------------------- 1 | Hello {{ "world!" | upper | repeat 5 }} 2 | -------------------------------------------------------------------------------- /_scripts/cross-build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | make -C .. build-cross 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.3 2 | COPY envtpl . 3 | RUN mv envtpl /bin 4 | CMD envtpl 5 | -------------------------------------------------------------------------------- /glide.yaml: -------------------------------------------------------------------------------- 1 | package: main 2 | import: 3 | - package: github.com/Masterminds/sprig 4 | - package: github.com/arschles/assert 5 | -------------------------------------------------------------------------------- /env_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/arschles/assert" 8 | ) 9 | 10 | func TestCollectEnv(t *testing.T) { 11 | envMap := collectEnv() 12 | pwd, ok := envMap["PWD"] 13 | assert.True(t, ok, "'PWD' not found in the env") 14 | wd, err := os.Getwd() 15 | assert.NoErr(t, err) 16 | assert.Equal(t, pwd, wd, "working dir") 17 | } 18 | -------------------------------------------------------------------------------- /env.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "strings" 6 | ) 7 | 8 | func collectEnv() map[string]string { 9 | envMap := make(map[string]string) 10 | envStrs := os.Environ() 11 | for _, envStr := range envStrs { 12 | spl := strings.SplitN(envStr, "=", 2) 13 | if len(spl) != 2 { 14 | continue 15 | } 16 | envMap[spl[0]] = spl[1] 17 | } 18 | return envMap 19 | } 20 | -------------------------------------------------------------------------------- /tpl.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "text/template" 6 | 7 | "github.com/Masterminds/sprig" 8 | ) 9 | 10 | func createTpl(tplName, fileName string) (*template.Template, error) { 11 | return template.New(tplName).Funcs(sprig.TxtFuncMap()).ParseFiles(fileName) 12 | } 13 | 14 | func renderTpl(tpl *template.Template, w io.Writer, data interface{}) error { 15 | return tpl.Execute(w, data) 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | envtpl 26 | 27 | vendor/ 28 | crossbuild 29 | -------------------------------------------------------------------------------- /glide.lock: -------------------------------------------------------------------------------- 1 | hash: 3de6518c74a099c6b494cac024246f76fbb18fb187cb85cd0b35e7d2a9226433 2 | updated: 2016-04-22T14:14:50.485712326-07:00 3 | imports: 4 | - name: github.com/aokoli/goutils 5 | version: 9c37978a95bd5c709a15883b6242714ea6709e64 6 | - name: github.com/arschles/assert 7 | version: d21ecd4884be33397d1298c3738b2b4937c7f499 8 | - name: github.com/Masterminds/sprig 9 | version: e6494bc7e81206ba6db404d2fd96500ffc453407 10 | devImports: [] 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # env-tpl 2 | 3 | [![Build Status](https://travis-ci.org/arschles/envtpl.svg?branch=master)](https://travis-ci.org/arschles/envtpl) [![Docker Repository on Quay](https://quay.io/repository/arschles/envtpl/status?token=f8121f5f-0c36-4241-8f33-aa3423ca519f "Docker Repository on Quay")](https://quay.io/repository/arschles/envtpl) 4 | 5 | Render Go templates from Environment Variables. Let's say you have the following template file called `pwd.tpl`: 6 | 7 | ``` 8 | The current working directory is {{.PWD}} 9 | ``` 10 | 11 | Run `envtpl -in pwd.tpl`, and you'll see the following printed to STDOUT: 12 | 13 | ``` 14 | The current working directory is /path/you/executed/from 15 | ``` 16 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "path/filepath" 8 | ) 9 | 10 | func main() { 11 | in := flag.String("in", "", "the template to render") 12 | flag.Parse() 13 | if *in == "" { 14 | fmt.Fprintln(os.Stderr, "Error: the input file was empty") 15 | flag.Usage() 16 | os.Exit(1) 17 | } 18 | 19 | tpl, err := createTpl(filepath.Base(*in), *in) 20 | if err != nil { 21 | fmt.Fprintf(os.Stderr, "Error: parsing template %s (%s)\n", *in, err) 22 | os.Exit(1) 23 | } 24 | 25 | envMap := collectEnv() 26 | 27 | if err := renderTpl(tpl, os.Stdout, envMap); err != nil { 28 | fmt.Fprintf(os.Stderr, "Rendering template (%s)", err) 29 | os.Exit(1) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tpl_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "os" 6 | "path/filepath" 7 | "testing" 8 | 9 | "github.com/arschles/assert" 10 | ) 11 | 12 | func testDataDir() (string, error) { 13 | wd, err := os.Getwd() 14 | if err != nil { 15 | return "", err 16 | } 17 | return filepath.Join(wd, "testdata"), nil 18 | } 19 | 20 | func TestCreateAndRenderTpl(t *testing.T) { 21 | td, err := testDataDir() 22 | assert.NoErr(t, err) 23 | templateNames := []string{ 24 | "sprig.tpl", 25 | "pwd.tpl", 26 | } 27 | envs := collectEnv() 28 | for i, tplName := range templateNames { 29 | fName := filepath.Join(td, tplName) 30 | tpl, err := createTpl(filepath.Base(fName), fName) 31 | if err != nil { 32 | t.Errorf("Error creating template %d (%s)", i, err) 33 | continue 34 | } 35 | buf := new(bytes.Buffer) 36 | if err := renderTpl(tpl, buf, envs); err != nil { 37 | t.Errorf("Error rendering template %d (%s)", i, err) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHORT_NAME := envtpl 2 | REPO_PATH := github.com/arschles/${SHORT_NAME} 3 | DEV_ENV_IMAGE := quay.io/deis/go-dev:0.11.0 4 | DEV_ENV_WORK_DIR := /go/src/${REPO_PATH} 5 | DEV_ENV_PREFIX := docker run --rm -v ${CURDIR}:${DEV_ENV_WORK_DIR} -w ${DEV_ENV_WORK_DIR} 6 | DEV_ENV_CMD := ${DEV_ENV_PREFIX} ${DEV_ENV_IMAGE} 7 | 8 | DOCKER_REPO ?= quay.io/ 9 | DOCKER_ORG ?= arschles 10 | DOCKER_IMAGE ?= envtpl 11 | DOCKER_TAG ?= devel 12 | DOCKER_IMAGE := ${DOCKER_REPO}${DOCKER_ORG}/${DOCKER_IMAGE}:${DOCKER_TAG} 13 | 14 | bootstrap: 15 | ${DEV_ENV_CMD} glide install 16 | 17 | glideup: 18 | ${DEV_ENV_CMD} glide up 19 | 20 | test: 21 | ${DEV_ENV_CMD} go test $$(glide nv) 22 | 23 | build: 24 | ${DEV_ENV_PREFIX} -e CGO_ENABLED=0 ${DEV_ENV_IMAGE} go build -a -installsuffix cgo -ldflags '-s' -o envtpl 25 | 26 | build-cross: 27 | ${DEV_ENV_PREFIX} -e CGO_ENABLED=0 ${DEV_ENV_IMAGE} gox -output="crossbuild/envtpl_{{.OS}}_{{.Arch}}" 28 | 29 | docker-build: 30 | docker build --rm -t ${DOCKER_IMAGE} . 31 | 32 | docker-push: 33 | docker push ${DOCKER_IMAGE} 34 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: generic 2 | branches: 3 | only: 4 | - master 5 | cache: 6 | directories: 7 | - vendor 8 | services: 9 | - docker 10 | sudo: required 11 | install: 12 | - make bootstrap 13 | script: 14 | - make test 15 | deploy: 16 | - provider: script 17 | script: _scripts/cross-build.sh 18 | on: 19 | branch: master 20 | tags: true 21 | - provider: releases 22 | api_key: 23 | secure: fBaBLaVK5bSBxl8VDoFtWlFi7Sfq+sDyT2YdiyEpMcZ+/WQWzERLDYYEJD8CHal8r7d7vHGN1PbYNkC22aNDTItpRGkTKu1zjLBVhnfmCDGEGyzREgNPOzJgxzgpw2GArfC1FZWxYLrxpgGqpp1bj6me0L6QnB6EQYmrb743ERm2Mr6rWLUJ0uAeRG+1/Z2fiqJumETa59pz7Frv9TJLoMU3Y9zp6QoWtJ43imwZyE6PuWW+2SoIwhVJ29jqXT5I7RrkkYbJmSGwQmwEFSr2Is7qMv4UEgKfuJptqxc0JBY8zzgj2MiDO0a27BKszG6LtET+JyATu2WeEK0OuXobnXouTAsD8rynZ3Iv+o5PMAzZyMCznmTvkZrOeUML3mFyfM7Ssf03VM2Pi2WqABN0DMN3/sjedZSaisMIOR8HGZrH6Ee9b179dWOf8lRzt95LZgqqiEw1v5S7S5sSxYwKaDZFseaVYBK8zaUEL1hdl8EocNHlHgn4UDXeSU8W7b3j/wZmxnRgY1Kd5hov4pjgA1S7brvnvMgLUBDy9t5GA3VrbzjQujeF6k4grXlljGMPnn/4Cvmbhb42fbuzUz6w0uqJaNduo4+MfiUmTMZ8F/VPx0HYGI0EbWVUZkhLmQDnwhl8SUGzgrrl0NAzNfDurhOYdMuwbCnKux+8vnngVdI= 24 | file: crossbuild/* 25 | on: 26 | tags: true 27 | repo: arschles/envtpl 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------