[config] add config:export --format shell

This commit is contained in:
Alex Quick
2017-07-16 19:50:50 -04:00
parent e7a81a8f8b
commit 4f5c747c99
3 changed files with 22 additions and 2 deletions

View File

@@ -16,12 +16,18 @@ import (
common "github.com/dokku/dokku/plugins/common"
)
//ExportFormat types of possible exports
type ExportFormat int
const (
//Exports format: Sourceable exports
Exports ExportFormat = iota
//Envfile format: dotenv file
Envfile
//DockerArgs format: --env args for docker
DockerArgs
//Shell format: env arguments for shell
Shell
)
//Env is a representation for global or app environment
@@ -35,6 +41,7 @@ func (e *Env) String() string {
return e.EnvfileString()
}
//Export the Env in the given format
func (e *Env) Export(format ExportFormat) string {
switch format {
case Exports:
@@ -43,6 +50,8 @@ func (e *Env) Export(format ExportFormat) string {
return e.EnvfileString()
case DockerArgs:
return e.DockerArgsString()
case Shell:
return e.ShellString()
default:
common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
return ""
@@ -65,6 +74,12 @@ func (e *Env) DockerArgsString() string {
return e.stringWithPrefixAndSeparator("--env=", " ", true)
}
//ShellString gets the contents of this Env in the form "KEY='value' KEY2='value'"
// for passing the environment in the shell
func (e *Env) ShellString() string {
return e.stringWithPrefixAndSeparator("", " ", true)
}
//StringWithPrefixAndSeparator makes a string of the environment
// with the given prefix and separator for each entry
func (e *Env) stringWithPrefixAndSeparator(prefix string, separator string, allowNewlines bool) string {

View File

@@ -50,6 +50,7 @@ func TestArrayExport(t *testing.T) {
e, _ := NewFromString("BAR='BAZ'\nFOO='b'ar '")
Expect(e.EnvfileString()).To(Equal("BAR=\"BAZ\"\nFOO=\"b'ar \""))
Expect(e.DockerArgsString()).To(Equal("--env=BAR='BAZ' --env=FOO='b'\\''ar '"))
Expect(e.ShellString()).To(Equal("BAR='BAZ' FOO='b'\\''ar '"))
}
func pairs(vars ...string) map[string]string {

View File

@@ -17,7 +17,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 ] which format to export as)")
format := args.String("format", "exports", "--format: [ exports | envfile | docker-args | shell ] which format to export as)")
args.Parse(os.Args[2:])
appName, trailingArgs := config.GetCommonArgs(*global, args.Args())
@@ -27,6 +27,7 @@ func main() {
env := config.GetConfig(appName, *merged)
exportType := configenv.Exports
suffix := "\n"
switch *format {
case "exports":
exportType = configenv.Exports
@@ -34,9 +35,12 @@ func main() {
exportType = configenv.Envfile
case "docker-args":
exportType = configenv.DockerArgs
case "shell":
exportType = configenv.Shell
suffix = " "
default:
common.LogFail(fmt.Sprintf("Unknown export format: %v", *format))
}
exported := env.Export(exportType)
fmt.Println(exported)
fmt.Print(exported + suffix)
}