[config] add config:export --format shell
This commit is contained in:
1 parent
e7a81a8f8b
commit
4f5c747c99
3 files changed
+22
-2
No files matched your search
@@ -16,12 +16,18 @@ import (
|
|||||||
common "github.com/dokku/dokku/plugins/common"
|
common "github.com/dokku/dokku/plugins/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//ExportFormat types of possible exports
|
||||||
type ExportFormat int
|
type ExportFormat int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
//Exports format: Sourceable exports
|
||||||
Exports ExportFormat = iota
|
Exports ExportFormat = iota
|
||||||
|
//Envfile format: dotenv file
|
||||||
Envfile
|
Envfile
|
||||||
|
//DockerArgs format: --env args for docker
|
||||||
DockerArgs
|
DockerArgs
|
||||||
|
//Shell format: env arguments for shell
|
||||||
|
Shell
|
||||||
)
|
)
|
||||||
|
|
||||||
//Env is a representation for global or app environment
|
//Env is a representation for global or app environment
|
||||||
@@ -35,6 +41,7 @@ func (e *Env) String() string {
|
|||||||
return e.EnvfileString()
|
return e.EnvfileString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Export the Env in the given format
|
||||||
func (e *Env) Export(format ExportFormat) string {
|
func (e *Env) Export(format ExportFormat) string {
|
||||||
switch format {
|
switch format {
|
||||||
case Exports:
|
case Exports:
|
||||||
@@ -43,6 +50,8 @@ func (e *Env) Export(format ExportFormat) string {
|
|||||||
return e.EnvfileString()
|
return e.EnvfileString()
|
||||||
case DockerArgs:
|
case DockerArgs:
|
||||||
return e.DockerArgsString()
|
return e.DockerArgsString()
|
||||||
|
case Shell:
|
||||||
|
return e.ShellString()
|
||||||
default:
|
default:
|
||||||
common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
|
common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
|
||||||
return ""
|
return ""
|
||||||
@@ -65,6 +74,12 @@ func (e *Env) DockerArgsString() string {
|
|||||||
return e.stringWithPrefixAndSeparator("--env=", " ", true)
|
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
|
//StringWithPrefixAndSeparator makes a string of the environment
|
||||||
// with the given prefix and separator for each entry
|
// with the given prefix and separator for each entry
|
||||||
func (e *Env) stringWithPrefixAndSeparator(prefix string, separator string, allowNewlines bool) string {
|
func (e *Env) stringWithPrefixAndSeparator(prefix string, separator string, allowNewlines bool) string {
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ func TestArrayExport(t *testing.T) {
|
|||||||
e, _ := NewFromString("BAR='BAZ'\nFOO='b'ar '")
|
e, _ := NewFromString("BAR='BAZ'\nFOO='b'ar '")
|
||||||
Expect(e.EnvfileString()).To(Equal("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.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 {
|
func pairs(vars ...string) map[string]string {
|
||||||
|
|||||||
@@ -17,7 +17,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 ] 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:])
|
args.Parse(os.Args[2:])
|
||||||
|
|
||||||
appName, trailingArgs := config.GetCommonArgs(*global, args.Args())
|
appName, trailingArgs := config.GetCommonArgs(*global, args.Args())
|
||||||
@@ -27,6 +27,7 @@ func main() {
|
|||||||
|
|
||||||
env := config.GetConfig(appName, *merged)
|
env := config.GetConfig(appName, *merged)
|
||||||
exportType := configenv.Exports
|
exportType := configenv.Exports
|
||||||
|
suffix := "\n"
|
||||||
switch *format {
|
switch *format {
|
||||||
case "exports":
|
case "exports":
|
||||||
exportType = configenv.Exports
|
exportType = configenv.Exports
|
||||||
@@ -34,9 +35,12 @@ func main() {
|
|||||||
exportType = configenv.Envfile
|
exportType = configenv.Envfile
|
||||||
case "docker-args":
|
case "docker-args":
|
||||||
exportType = configenv.DockerArgs
|
exportType = configenv.DockerArgs
|
||||||
|
case "shell":
|
||||||
|
exportType = configenv.Shell
|
||||||
|
suffix = " "
|
||||||
default:
|
default:
|
||||||
common.LogFail(fmt.Sprintf("Unknown export format: %v", *format))
|
common.LogFail(fmt.Sprintf("Unknown export format: %v", *format))
|
||||||
}
|
}
|
||||||
exported := env.Export(exportType)
|
exported := env.Export(exportType)
|
||||||
fmt.Println(exported)
|
fmt.Print(exported + suffix)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user