Merge pull request #3511 from dokku/config-json

Add json output to config:export
This commit is contained in:
Jose Diaz-Gonzalez authored and GitHub committed 2019-04-21 21:14:02 -04:00
commit f9a59180f2
5 files changed
+75 -9

No files matched your search

+40 -4
View File
@@ -1,17 +1,16 @@
package config package config
import ( import (
"archive/tar"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"archive/tar"
"os"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/ryanuber/columnize" "github.com/ryanuber/columnize"
@@ -31,6 +30,10 @@ const (
ExportFormatShell ExportFormatShell
//ExportFormatPretty format: pretty-printed in columns //ExportFormatPretty format: pretty-printed in columns
ExportFormatPretty ExportFormatPretty
//ExportFormatJSON format: json key/value output
ExportFormatJSON
//ExportFormatJSONList format: json output as a list of objects
ExportFormatJSONList
) )
//Env is a representation for global or app environment //Env is a representation for global or app environment
@@ -168,6 +171,10 @@ func (e *Env) Export(format ExportFormat) string {
return e.ShellString() return e.ShellString()
case ExportFormatPretty: case ExportFormatPretty:
return prettyPrintEnvEntries("", e.Map()) return prettyPrintEnvEntries("", e.Map())
case ExportFormatJSON:
return e.JSONString()
case ExportFormatJSONList:
return e.JSONListString()
default: default:
common.LogFail(fmt.Sprintf("Unknown export format: %v", format)) common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
return "" return ""
@@ -190,6 +197,35 @@ func (e *Env) DockerArgsString() string {
return e.stringWithPrefixAndSeparator("--env=", " ") return e.stringWithPrefixAndSeparator("--env=", " ")
} }
//JSONString returns the contents of this Env as a key/value json object
func (e *Env) JSONString() string {
data, err := json.Marshal(e.Map())
if err != nil {
return "{}"
}
return string(data)
}
//JSONListString returns the contents of this Env as a json list of objects containing the name and the value of the env var
func (e *Env) JSONListString() string {
var list []map[string]string
for _, key := range e.Keys() {
value, _ := e.Get(key)
list = append(list, map[string]string{
"name": key,
"value": value,
})
}
data, err := json.Marshal(list)
if err != nil {
return "[]"
}
return string(data)
}
//ShellString gets the contents of this Env in the form "KEY='value' KEY2='value'" //ShellString gets the contents of this Env in the form "KEY='value' KEY2='value'"
// for passing the environment in the shell // for passing the environment in the shell
func (e *Env) ShellString() string { func (e *Env) ShellString() string {
+3 -3
View File
@@ -21,12 +21,12 @@ Additional commands:`
helpContent = ` helpContent = `
config (<app>|--global), Pretty-print an app or global environment config (<app>|--global), Pretty-print an app or global environment
config:bundle (<app>|--global) [--merged], Bundle environment into tarfile
config:export (<app>|--global) [--envfile], Export a global or app environment
config:get (<app>|--global) KEY, Display a global or app-specific config value config:get (<app>|--global) KEY, Display a global or app-specific config value
config:keys (<app>|--global) [--merged], Show keys set in environment
config:set [--encoded] [--no-restart] (<app>|--global) KEY1=VALUE1 [KEY2=VALUE2 ...], Set one or more config vars config:set [--encoded] [--no-restart] (<app>|--global) KEY1=VALUE1 [KEY2=VALUE2 ...], Set one or more config vars
config:unset [--no-restart] (<app>|--global) KEY1 [KEY2 ...], Unset one or more config vars config:unset [--no-restart] (<app>|--global) KEY1 [KEY2 ...], Unset one or more config vars
config:export (<app>|--global) [--envfile], Export a global or app environment
config:keys (<app>|--global) [--merged], Show keys set in environment
config:bundle (<app>|--global) [--merged], Bundle environment into tarfile
` `
) )
@@ -14,7 +14,7 @@ func main() {
args := flag.NewFlagSet("config:export", flag.ExitOnError) args := flag.NewFlagSet("config:export", flag.ExitOnError)
global := args.Bool("global", false, "--global: use the global environment") global := args.Bool("global", false, "--global: use the global environment")
merged := args.Bool("merged", false, "--merged: merge app environment and global environment") merged := args.Bool("merged", false, "--merged: merge app environment and global environment")
format := args.String("format", "exports", "--format: [ exports | envfile | docker-args | shell ] which format to export as)") format := args.String("format", "exports", "--format: [ exports | envfile | docker-args | shell | pretty | json | json-list ] which format to export as)")
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
config.CommandExport(args.Args(), *global, *merged, *format) config.CommandExport(args.Args(), *global, *merged, *format)
} }
+4
View File
@@ -117,6 +117,10 @@ func CommandExport(args []string, global bool, merged bool, format string) {
suffix = " " suffix = " "
case "pretty": case "pretty":
exportType = ExportFormatPretty exportType = ExportFormatPretty
case "json":
exportType = ExportFormatJSON
case "json-list":
exportType = ExportFormatJSONList
default: default:
common.LogFail(fmt.Sprintf("Unknown export format: %v", format)) common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
} }
+27 -1
View File
@@ -155,6 +155,32 @@ teardown() {
run /bin/bash -c "dokku --app $TEST_APP config:show" run /bin/bash -c "dokku --app $TEST_APP config:show"
echo "output: $output" echo "output: $output"
echo "status: "$stat echo "status: "$stat
assert_output "=====> $TEST_APP env vars"$'\nBKEY: true\naKey: true\nbKey: true\nzKey: true' assert_output "=====> $TEST_APP env vars"$'\nBKEY: true\naKey: true\nbKey: true\nzKey: true'
} }
@test "(config) config:export" {
run /bin/bash -c "dokku --app $TEST_APP config:set zKey=true bKey=true BKEY=true aKey=true"
echo "output: $output"
echo "status: $status"
assert_success
run /bin/bash -c "dokku config:export --format docker-args $TEST_APP"
echo "output: $output"
echo "status: "$stat
assert_output "--env=BKEY='true' --env=aKey='true' --env=bKey='true' --env=zKey='true'"
run /bin/bash -c "dokku config:export --format shell $TEST_APP"
echo "output: $output"
echo "status: "$stat
assert_output "BKEY='true' aKey='true' bKey='true' zKey='true' "
run /bin/bash -c "dokku config:export --format json $TEST_APP"
echo "output: $output"
echo "status: "$stat
assert_output '{"BKEY":"true","aKey":"true","bKey":"true","zKey":"true"}'
run /bin/bash -c "dokku config:export --format json-list $TEST_APP"
echo "output: $output"
echo "status: "$stat
assert_output '[{"name":"BKEY","value":"true"},{"name":"aKey","value":"true"},{"name":"bKey","value":"true"},{"name":"zKey","value":"true"}]'
}