Skip to content

Commit

Permalink
JSON object export type
Browse files Browse the repository at this point in the history
  • Loading branch information
kendavis2 committed Oct 7, 2019
1 parent 9e3785c commit 3a77217
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
25 changes: 25 additions & 0 deletions cli/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,28 @@ type EnvFormat struct {
Name string `json:"name"`
Value string `json:"value"`
}

func toJsonObjectFormat(file []byte) (bytes.Buffer, error) {
reader := bytes.NewReader(file)
pairs := gotenv.Parse(reader)

var buff bytes.Buffer

env := map[string]string{}

for key, value := range pairs {
env[key] = value
}

b, err := json.MarshalIndent(env, "", " ")
if err != nil {
return buff, err
}

_, err = buff.Write(b)
if err != nil {
return buff, err
}

return buff, nil
}
6 changes: 6 additions & 0 deletions cli/cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ func Pull(catalogPath string, opt cfg.UserOptions, io models.IO) (int, int, erro
if err != nil {
logger.L.Print(err)
}
case "json-object":
msg = fmt.Sprintf(msg, "JSON object")
exportBuffer, err = toJsonObjectFormat(exportBuffer.Bytes())
if err != nil {
logger.L.Print(err)
}
case "terminal-export":
msg = fmt.Sprintf(msg, "terminal export commands")
exportBuffer, err = bufferExportScript(exportBuffer.Bytes())
Expand Down
2 changes: 1 addition & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| `-v` | <code>"v0.2.0-rc"</code> | Set version of file to pull or push. |
| `-a` | `{path}/{file}` | Set alternate location for the file to be restored. When used during a push, the alternate location will be saved, but when used during a pull, the alternate location will override any stored locations. |
| `-e` | | Send environment variables from store prefixed with export commands to `stdout` instead of writing file to disk. (default: `restore file`) |
| `-g` | `terminal-export/task-def-secrets/task-def-env` | Send environment variables from store using specified format to `stdout` instead of writing file to disk. |
| `-g` | `terminal-export/task-def-secrets/task-def-env/json-object` | Send environment variables from store using specified format to `stdout` instead of writing file to disk. |
| `-n` | | Skip pulling environment variables already exported in the current environment. (default: `all`) |
| `-d` | `true/false` | Set automatic deletion of local files after successful push. (default: `false`) |
| `-h` | | List command documentaion. |
Expand Down
36 changes: 34 additions & 2 deletions docs/LIBRARY.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## How to Load Configuration Using a Library ##

Run this code on app start to retrieve configuration.
Run `pull` on app start to retrieve configuration.

Need a Node library? Try [cstore-js](https://github.com/shivpatel/cstore-js) instead.
### Using Library (Go) ###

Requires: `go1.12.0`

Expand Down Expand Up @@ -30,3 +30,35 @@ for k, v := range config {
log.Printf("%s=%s\n", k, v)
}
```

### Using CLI (javascript) ###

This method should work with any language. Below is a javascript example.

1. Install cStore at build time
```bash
$ curl -L -o /usr/local/bin/cstore https://github.com/turnerlabs/cstore/releases/download/v3.4.0-alpha/cstore_linux_386 && chmod +x /usr/local/bin/cstore
```

2. Wrap CLI pull command
```javascript

'use strict';
const shell = require('shelljs');

function pull(cstorePath, tags) {
// silent:true prevents output from being logged compromising secrets
var result = shell.exec('./' + cstorePath + ' pull -l -g json-object -t "' + tags + '"', {silent:true})

if (result.code != 0) {
throw new Error(result.stderr)
}

return JSON.parse(result.stdout)
}

module.exports.pull = pull;
```

Need a Node library? Try [cstore-js](https://github.com/shivpatel/cstore-js) instead.

0 comments on commit 3a77217

Please sign in to comment.