feat: add ability to export env vars as a --env=key ... string
This commit is contained in:
@@ -24,8 +24,10 @@ const (
|
||||
ExportFormatExports ExportFormat = iota
|
||||
//ExportFormatEnvfile format: dotenv file
|
||||
ExportFormatEnvfile
|
||||
//ExportFormatDockerArgs format: --env args for docker
|
||||
//ExportFormatDockerArgs format: --env KEY=VALUE args for docker
|
||||
ExportFormatDockerArgs
|
||||
//ExportFormatDockerArgsKeys format: --env KEY args for docker
|
||||
ExportFormatDockerArgsKeys
|
||||
//ExportFormatShell format: env arguments for shell
|
||||
ExportFormatShell
|
||||
//ExportFormatPretty format: pretty-printed in columns
|
||||
@@ -171,6 +173,8 @@ func (e *Env) Export(format ExportFormat) string {
|
||||
return e.EnvfileString()
|
||||
case ExportFormatDockerArgs:
|
||||
return e.DockerArgsString()
|
||||
case ExportFormatDockerArgsKeys:
|
||||
return e.DockerArgsKeysString()
|
||||
case ExportFormatShell:
|
||||
return e.ShellString()
|
||||
case ExportFormatPretty:
|
||||
@@ -201,6 +205,16 @@ func (e *Env) DockerArgsString() string {
|
||||
return e.stringWithPrefixAndSeparator("--env=", " ")
|
||||
}
|
||||
|
||||
//DockerArgsStringKeys gets the contents of this Env in the form -env=KEY --env...
|
||||
func (e *Env) DockerArgsKeysString() string {
|
||||
keys := e.Keys()
|
||||
entries := make([]string, len(keys))
|
||||
for i, k := range keys {
|
||||
entries[i] = fmt.Sprintf("%s%s", "--env=", k)
|
||||
}
|
||||
return strings.Join(entries, " ")
|
||||
}
|
||||
|
||||
//JSONString returns the contents of this Env as a key/value json object
|
||||
func (e *Env) JSONString() string {
|
||||
data, err := json.Marshal(e.Map())
|
||||
|
||||
@@ -54,6 +54,7 @@ func TestExport(t *testing.T) {
|
||||
e, _ := newEnvFromString("BAR='BAZ'\nFOO='b'ar '\nBAZ='a\\nb'")
|
||||
Expect(e.Export(ExportFormatEnvfile)).To(Equal("BAR=\"BAZ\"\nBAZ=\"a\\nb\"\nFOO=\"b'ar \""))
|
||||
Expect(e.Export(ExportFormatDockerArgs)).To(Equal("--env=BAR='BAZ' --env=BAZ='a\nb' --env=FOO='b'\\''ar '"))
|
||||
Expect(e.Export(ExportFormatDockerArgsKeys)).To(Equal("--env=BAR --env=BAZ --env=FOO"))
|
||||
Expect(e.Export(ExportFormatShell)).To(Equal("BAR='BAZ' BAZ='a\nb' FOO='b'\\''ar '"))
|
||||
Expect(e.Export(ExportFormatExports)).To(Equal("export BAR='BAZ'\nexport BAZ='a\nb'\nexport FOO='b'\\''ar '"))
|
||||
Expect(e.Export(ExportFormatPretty)).To(Equal("BAR: BAZ\nBAZ: a\nb\nFOO: b'ar"))
|
||||
|
||||
@@ -49,7 +49,7 @@ func main() {
|
||||
args := flag.NewFlagSet("config:export", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the 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 | pretty | json | json-list ] which format to export as)")
|
||||
format := args.String("format", "exports", "--format: [ exports | envfile | docker-args | docker-args-keys | shell | pretty | json | json-list ] which format to export as)")
|
||||
args.Parse(os.Args[2:])
|
||||
if !*global {
|
||||
appName = args.Arg(0)
|
||||
|
||||
@@ -43,13 +43,14 @@ func CommandExport(appName string, global bool, merged bool, format string) erro
|
||||
suffix := "\n"
|
||||
|
||||
exportTypes := map[string]ExportFormat{
|
||||
"exports": ExportFormatExports,
|
||||
"envfile": ExportFormatEnvfile,
|
||||
"docker-args": ExportFormatDockerArgs,
|
||||
"shell": ExportFormatShell,
|
||||
"pretty": ExportFormatPretty,
|
||||
"json": ExportFormatJSON,
|
||||
"json-list": ExportFormatJSONList,
|
||||
"exports": ExportFormatExports,
|
||||
"envfile": ExportFormatEnvfile,
|
||||
"docker-args": ExportFormatDockerArgs,
|
||||
"docker-args-keys": ExportFormatDockerArgsKeys,
|
||||
"shell": ExportFormatShell,
|
||||
"pretty": ExportFormatPretty,
|
||||
"json": ExportFormatJSON,
|
||||
"json-list": ExportFormatJSONList,
|
||||
}
|
||||
|
||||
exportType, ok := exportTypes[format]
|
||||
|
||||
Reference in New Issue
Block a user