Skip to content

Commit

Permalink
Merge pull request #481 from hairyhenderson/envfile-support-479
Browse files Browse the repository at this point in the history
Adding support for .env files
  • Loading branch information
hairyhenderson authored Feb 5, 2019
2 parents 3373261 + f850baa commit e5375ba
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"strings"

"github.com/joho/godotenv"

"github.com/Shopify/ejson"
ejsonJson "github.com/Shopify/ejson/json"
"github.com/hairyhenderson/gomplate/env"
Expand Down Expand Up @@ -100,6 +102,11 @@ func TOML(in string) (interface{}, error) {
return unmarshalObj(obj, in, toml.Unmarshal)
}

// dotEnv - Unmarshal a dotenv file
func dotEnv(in string) (interface{}, error) {
return godotenv.Unmarshal(in)
}

func parseCSV(args ...string) ([][]string, []string, error) {
in, delim, hdr := csvParseArgs(args...)
c := csv.NewReader(strings.NewReader(in))
Expand Down
22 changes: 22 additions & 0 deletions data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,25 @@ func TestDecryptEJSON(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, expected, actual)
}

func TestDotEnv(t *testing.T) {
in := `FOO=a regular unquoted value
export BAR=another value, exports are ignored
# comments are totally ignored, as are blank lines
FOO.BAR = "values can be double-quoted, and shell\nescapes are supported"
BAZ = "variable expansion: ${FOO}"
QUX='single quotes ignore $variables'
`
expected := map[string]string{
"FOO": "a regular unquoted value",
"BAR": "another value, exports are ignored",
"FOO.BAR": "values can be double-quoted, and shell\nescapes are supported",
"BAZ": "variable expansion: a regular unquoted value",
"QUX": "single quotes ignore $variables",
}
out, err := dotEnv(in)
assert.NoError(t, err)
assert.EqualValues(t, expected, out)
}
3 changes: 3 additions & 0 deletions data/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
regExtension(".yaml", yamlMimetype)
regExtension(".csv", csvMimetype)
regExtension(".toml", tomlMimetype)
regExtension(".env", envMimetype)
}

// registerReaders registers the source-reader functions
Expand Down Expand Up @@ -338,6 +339,8 @@ func parseData(mimeType, s string) (out interface{}, err error) {
out, err = CSV(s)
case tomlMimetype:
out, err = TOML(s)
case envMimetype:
out, err = dotEnv(s)
case textMimetype:
out = s
default:
Expand Down
1 change: 1 addition & 0 deletions data/mimetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const (
jsonArrayMimetype = "application/array+json"
tomlMimetype = "application/toml"
yamlMimetype = "application/yaml"
envMimetype = "application/x-env"
)
31 changes: 27 additions & 4 deletions docs/content/datasources.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ These are the supported types:

| Format | MIME Type | Extension(s) | Notes |
|--------|-----------|-------|------|
| CSV | `text/csv` | | Uses the [`data.CSV`][] function to present the file as a 2-dimensional row-first string array |
| JSON | `application/json` | | [JSON][] _objects_ are assumed, and arrays or other values are not parsed with this type. Uses the [`data.JSON`][] function for parsing. [EJSON][] (encrypted JSON) is supported and will be decrypted. |
| CSV | `text/csv` | `.csv` | Uses the [`data.CSV`][] function to present the file as a 2-dimensional row-first string array |
| JSON | `application/json` | `.json` | [JSON][] _objects_ are assumed, and arrays or other values are not parsed with this type. Uses the [`data.JSON`][] function for parsing. [EJSON][] (encrypted JSON) is supported and will be decrypted. |
| JSON Array | `application/array+json` | | A special type for parsing datasources containing just JSON arrays. Uses the [`data.JSONArray`][] function for parsing |
| Plain Text | `text/plain` | | Unstructured, and as such only intended for use with the [`include`][] function |
| TOML | `application/toml` | | Parses [TOML][] with the [`data.TOML`][] function |
| YAML | `application/yaml` | | Parses [YAML][] with the [`data.YAML`][] function |
| TOML | `application/toml` | `.toml` | Parses [TOML][] with the [`data.TOML`][] function |
| YAML | `application/yaml` | `.yml`, `.yaml` | Parses [YAML][] with the [`data.YAML`][] function |
| [.env](#the-env-file-format) | `application/x-env` | `.env` | Basically just a file of `key=value` pairs separated by newlines, usually intended for sourcing into a shell. Common in [Docker Compose](https://docs.docker.com/compose/env-file/), [Ruby](https://github.com/bkeepers/dotenv), and [Node.js](https://github.com/motdotla/dotenv) applications. See [below](#the-env-file-format) for more information. |

### Overriding MIME Types

Expand All @@ -119,6 +120,28 @@ $ gomplate -d data=file:///tmp/data.txt?type=application/json -i '{{ (ds "data")
bar
```

### The `.env` file format

Many applications and frameworks support the use of a ".env" file for providing environment variables. It can also be considerd a simple key/value file format, and as such can be used as a datasource in gomplate.

To [override](#overriding-mime-types), use the unregistered `application/x-env` MIME type.

Here's a sample explaining the syntax:

```bash
FOO=a regular unquoted value
export BAR=another value, exports are ignored

# comments are totally ignored, as are blank lines
FOO.BAR = "values can be double-quoted, and\tshell\nescapes are supported"

BAZ="variable expansion: ${FOO}"
QUX='single quotes ignore $variables and newlines'
```

The [`github.com/joho/godotenv`](https://github.com/joho/godotenv) package is used for parsing - see the full details there.


## Using `aws+smp` datasources

The `aws+smp://` scheme can be used to retrieve data from the [AWS Systems Manager](https://aws.amazon.com/systems-manager/) (née AWS EC2 Simple Systems Manager) [Parameter Store](https://aws.amazon.com/systems-manager/features/#Parameter_Store). This hierarchically organized key/value store allows you to store text, lists or encrypted secrets for easy retrieval by AWS resources. See [the AWS Systems Manager documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-su-create.html#sysman-paramstore-su-create-about) for details on creating these parameters.
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/datasources_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ func (s *FileDatasourcesSuite) SetUpSuite(c *C) {
A1,B1
A2,"foo""
bar"
`,
"test.env": `FOO=a regular unquoted value
export BAR=another value, exports are ignored
# comments are totally ignored, as are blank lines
FOO.BAR = "values can be double-quoted, and shell\nescapes are supported"
BAZ = "variable expansion: ${FOO}"
QUX='single quotes ignore $variables'
`,
}),
fs.WithDir("sortorder", fs.WithFiles(map[string]string{
Expand Down Expand Up @@ -144,4 +153,15 @@ bar`})
zonef = "false"
}
`})
result = icmd.RunCommand(GomplateBin,
"-d", "envfile="+s.tmpDir.Join("test.env"),
"-i", `{{ (ds "envfile") | data.ToJSONPretty " " }}`,
)
result.Assert(c, icmd.Expected{ExitCode: 0, Out: `{
"BAR": "another value, exports are ignored",
"BAZ": "variable expansion: a regular unquoted value",
"FOO": "a regular unquoted value",
"FOO.BAR": "values can be double-quoted, and shell\nescapes are supported",
"QUX": "single quotes ignore $variables"
}`})
}
23 changes: 23 additions & 0 deletions vendor/github.com/joho/godotenv/LICENCE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e5375ba

Please sign in to comment.