feat: add ability to export env vars as a --env=key ... string
This commit is contained in:
1 parent
33d5dd44d9
commit
5921692f83
4 files changed
+25
-9
No files matched your search
@@ -24,8 +24,10 @@ const (
|
|||||||
ExportFormatExports ExportFormat = iota
|
ExportFormatExports ExportFormat = iota
|
||||||
//ExportFormatEnvfile format: dotenv file
|
//ExportFormatEnvfile format: dotenv file
|
||||||
ExportFormatEnvfile
|
ExportFormatEnvfile
|
||||||
//ExportFormatDockerArgs format: --env args for docker
|
//ExportFormatDockerArgs format: --env KEY=VALUE args for docker
|
||||||
ExportFormatDockerArgs
|
ExportFormatDockerArgs
|
||||||
|
//ExportFormatDockerArgsKeys format: --env KEY args for docker
|
||||||
|
ExportFormatDockerArgsKeys
|
||||||
//ExportFormatShell format: env arguments for shell
|
//ExportFormatShell format: env arguments for shell
|
||||||
ExportFormatShell
|
ExportFormatShell
|
||||||
//ExportFormatPretty format: pretty-printed in columns
|
//ExportFormatPretty format: pretty-printed in columns
|
||||||
@@ -171,6 +173,8 @@ func (e *Env) Export(format ExportFormat) string {
|
|||||||
return e.EnvfileString()
|
return e.EnvfileString()
|
||||||
case ExportFormatDockerArgs:
|
case ExportFormatDockerArgs:
|
||||||
return e.DockerArgsString()
|
return e.DockerArgsString()
|
||||||
|
case ExportFormatDockerArgsKeys:
|
||||||
|
return e.DockerArgsKeysString()
|
||||||
case ExportFormatShell:
|
case ExportFormatShell:
|
||||||
return e.ShellString()
|
return e.ShellString()
|
||||||
case ExportFormatPretty:
|
case ExportFormatPretty:
|
||||||
@@ -201,6 +205,16 @@ func (e *Env) DockerArgsString() string {
|
|||||||
return e.stringWithPrefixAndSeparator("--env=", " ")
|
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
|
//JSONString returns the contents of this Env as a key/value json object
|
||||||
func (e *Env) JSONString() string {
|
func (e *Env) JSONString() string {
|
||||||
data, err := json.Marshal(e.Map())
|
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'")
|
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(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(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(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(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"))
|
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)
|
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 | 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:])
|
args.Parse(os.Args[2:])
|
||||||
if !*global {
|
if !*global {
|
||||||
appName = args.Arg(0)
|
appName = args.Arg(0)
|
||||||
|
|||||||
@@ -43,13 +43,14 @@ func CommandExport(appName string, global bool, merged bool, format string) erro
|
|||||||
suffix := "\n"
|
suffix := "\n"
|
||||||
|
|
||||||
exportTypes := map[string]ExportFormat{
|
exportTypes := map[string]ExportFormat{
|
||||||
"exports": ExportFormatExports,
|
"exports": ExportFormatExports,
|
||||||
"envfile": ExportFormatEnvfile,
|
"envfile": ExportFormatEnvfile,
|
||||||
"docker-args": ExportFormatDockerArgs,
|
"docker-args": ExportFormatDockerArgs,
|
||||||
"shell": ExportFormatShell,
|
"docker-args-keys": ExportFormatDockerArgsKeys,
|
||||||
"pretty": ExportFormatPretty,
|
"shell": ExportFormatShell,
|
||||||
"json": ExportFormatJSON,
|
"pretty": ExportFormatPretty,
|
||||||
"json-list": ExportFormatJSONList,
|
"json": ExportFormatJSON,
|
||||||
|
"json-list": ExportFormatJSONList,
|
||||||
}
|
}
|
||||||
|
|
||||||
exportType, ok := exportTypes[format]
|
exportType, ok := exportTypes[format]
|
||||||
|
|||||||
Reference in new issue
Block a user