[config] move to godotenv
This commit is contained in:
24
plugins/config/src/subcommands/bundle/bundle.go
Normal file
24
plugins/config/src/subcommands/bundle/bundle.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := flag.NewFlagSet("config:get", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
merged := args.Bool("merged", false, "--merged: merge app environment and global environment")
|
||||
args.Parse(os.Args[2:])
|
||||
|
||||
appName, trailingArgs := config.GetCommonArgs(*global, args.Args())
|
||||
if len(trailingArgs) > 0 {
|
||||
common.LogFail(fmt.Sprintf("Trailing argument(s): %v", trailingArgs))
|
||||
}
|
||||
config := config.GetConfig(appName, *merged)
|
||||
config.ExportBundle(os.Stdout)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
|
||||
common "github.com/dokku/dokku/plugins/common"
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
"github.com/dokku/dokku/plugins/config/src/configenv"
|
||||
)
|
||||
|
||||
@@ -16,66 +17,26 @@ 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")
|
||||
keys := args.Bool("keys", false, "--keys: export keys only")
|
||||
bundle := args.Bool("bundle", false, "--bundle: export as tar bundle")
|
||||
envfile := args.Bool("envfile", false, "--envfile: export as envfile rather than bash exports (--prefix='')")
|
||||
prefix := args.String("prefix", defaultPrefix, "--prefix: prefix")
|
||||
separator := args.String("separator", defaultSeparator, "--separator: separator")
|
||||
escapeNewlines := args.Bool("escape-newlines", false, "--escape-newlines: replace literal newlines with $'\n'")
|
||||
format := args.String("format", "exports", "--format: [ exports | envfile | docker-args ] which format to export as)")
|
||||
args.Parse(os.Args[2:])
|
||||
|
||||
appName := args.Arg(0)
|
||||
if appName == "" && !*global {
|
||||
common.LogFail("Please specify an app or --global")
|
||||
appName, trailingArgs := config.GetCommonArgs(*global, args.Args())
|
||||
if len(trailingArgs) > 0 {
|
||||
common.LogFail(fmt.Sprintf("Trailing argument(s): %v", trailingArgs))
|
||||
}
|
||||
|
||||
if *global {
|
||||
if appName != "" {
|
||||
common.LogFail("Trailing argument: " + appName)
|
||||
}
|
||||
if *merged == true {
|
||||
common.LogFail("Only app environments can be merged")
|
||||
}
|
||||
appName = "--global"
|
||||
env := config.GetConfig(appName, *merged)
|
||||
exportType := configenv.Exports
|
||||
switch *format {
|
||||
case "exports":
|
||||
exportType = configenv.Exports
|
||||
case "envfile":
|
||||
exportType = configenv.Envfile
|
||||
case "docker-args":
|
||||
exportType = configenv.DockerArgs
|
||||
default:
|
||||
common.LogFail(fmt.Sprintf("Unknown export format: %v", *format))
|
||||
}
|
||||
|
||||
env, err := configenv.NewFromTarget(appName)
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
|
||||
if *merged {
|
||||
global, err := configenv.LoadGlobal()
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
global.Merge(env)
|
||||
env = global
|
||||
}
|
||||
|
||||
if *keys {
|
||||
for _, k := range env.Keys() {
|
||||
fmt.Println(k)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if *escapeNewlines {
|
||||
env.EscapeNewlines = true
|
||||
}
|
||||
|
||||
if *bundle {
|
||||
if *prefix != defaultPrefix || *separator != defaultSeparator || *envfile {
|
||||
common.LogFail("--bundle cannot be given with --envfile, --prefix, or --separator")
|
||||
}
|
||||
env.ExportBundle(os.Stdout)
|
||||
}
|
||||
if *envfile {
|
||||
if *prefix != defaultPrefix {
|
||||
common.LogFail("--prefix and --envfile cannot both be given")
|
||||
return
|
||||
}
|
||||
*prefix = ""
|
||||
}
|
||||
fmt.Println(env.StringWithPrefixAndSeparator(*prefix, *separator))
|
||||
exported := env.Export(exportType)
|
||||
fmt.Println(exported)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
|
||||
common "github.com/dokku/dokku/plugins/common"
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
"github.com/dokku/dokku/plugins/config/src/configenv"
|
||||
)
|
||||
|
||||
@@ -15,28 +16,13 @@ func main() {
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
quoted := args.Bool("quoted", false, "--quoted: get the value quoted")
|
||||
args.Parse(os.Args[2:])
|
||||
appName := args.Arg(0)
|
||||
nextArg := 0
|
||||
if appName == "" && !*global {
|
||||
common.LogFail("Please specify an app or --global")
|
||||
}
|
||||
|
||||
if *global {
|
||||
appName = "--global"
|
||||
} else {
|
||||
nextArg = 1
|
||||
appName, keys := config.GetCommonArgs(*global, args.Args())
|
||||
if len(keys) > 1 {
|
||||
common.LogFail(fmt.Sprintf("Unexpected argument(s): %v", keys[1:]))
|
||||
}
|
||||
value := config.GetWithDefault(appName, keys[0], "")
|
||||
|
||||
env, err := configenv.NewFromTarget(appName)
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
|
||||
if args.NArg() > nextArg+1 {
|
||||
common.LogFail(fmt.Sprintf("Unexpected argument(s): %v", args.Args()[nextArg+1:]))
|
||||
}
|
||||
key := args.Arg(nextArg)
|
||||
value := env.GetDefault(key, "")
|
||||
if *quoted {
|
||||
fmt.Printf("'%s'", configenv.SingleQuoteEscape(value))
|
||||
} else {
|
||||
|
||||
26
plugins/config/src/subcommands/keys/keys.go
Normal file
26
plugins/config/src/subcommands/keys/keys.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := flag.NewFlagSet("config:get", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
merged := args.Bool("merged", false, "--merged: merge app environment and global environment")
|
||||
args.Parse(os.Args[2:])
|
||||
|
||||
appName, trailingArgs := config.GetCommonArgs(*global, args.Args())
|
||||
if len(trailingArgs) > 0 {
|
||||
common.LogFail(fmt.Sprintf("Trailing argument(s): %v", trailingArgs))
|
||||
}
|
||||
config := config.GetConfig(appName, *merged)
|
||||
for _, k := range config.Keys() {
|
||||
fmt.Println(k)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
common "github.com/dokku/dokku/plugins/common"
|
||||
config "github.com/dokku/dokku/plugins/config"
|
||||
"github.com/dokku/dokku/plugins/config/src/configenv"
|
||||
)
|
||||
|
||||
// set the given entries from the specified environment
|
||||
@@ -19,28 +18,10 @@ func main() {
|
||||
encoded := args.Bool("encoded", false, "--encoded: interpret VALUEs as base64")
|
||||
noRestart := args.Bool("no-restart", false, "--no-restart: no restart")
|
||||
args.Parse(os.Args[2:])
|
||||
shouldRestart := !*global && !*noRestart
|
||||
var nextArg = 0
|
||||
appName := args.Arg(0)
|
||||
if appName == "" && !*global {
|
||||
common.LogFail("Please specify an app or --global")
|
||||
}
|
||||
appName, pairs := config.GetCommonArgs(*global, args.Args())
|
||||
|
||||
if *global {
|
||||
appName = "--global"
|
||||
} else {
|
||||
nextArg = 1
|
||||
}
|
||||
|
||||
env, err := configenv.NewFromTarget(appName)
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
|
||||
entries := args.Args()[nextArg:]
|
||||
updated := make(map[string]string)
|
||||
for _, e := range entries {
|
||||
//log
|
||||
for _, e := range pairs {
|
||||
parts := strings.SplitN(e, "=", 2)
|
||||
if len(parts) == 1 {
|
||||
common.LogFail("Invalid env pair: " + e)
|
||||
@@ -53,21 +34,7 @@ func main() {
|
||||
}
|
||||
value = string(decoded)
|
||||
}
|
||||
env.Set(key, value)
|
||||
updated[key] = value
|
||||
}
|
||||
|
||||
if len(updated) != 0 {
|
||||
common.LogInfo1("Setting config vars")
|
||||
fmt.Println(config.PrettyPrintLogEntries(" ", updated))
|
||||
env.Write()
|
||||
args := append([]string{appName, "set"}, entries...)
|
||||
common.PlugnTrigger("post-config-update", args...)
|
||||
}
|
||||
|
||||
if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
|
||||
common.LogInfo1(fmt.Sprintf("Restarting app %s", appName))
|
||||
cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName)
|
||||
cmd.Execute()
|
||||
}
|
||||
config.SetMany(appName, updated, !*noRestart)
|
||||
}
|
||||
|
||||
@@ -4,10 +4,7 @@ import (
|
||||
"flag"
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
|
||||
common "github.com/dokku/dokku/plugins/common"
|
||||
"github.com/dokku/dokku/plugins/config/src/configenv"
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
)
|
||||
|
||||
//unset the given entries from the given environment
|
||||
@@ -15,42 +12,8 @@ func main() {
|
||||
args := flag.NewFlagSet("config:unset", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
noRestart := args.Bool("no-restart", false, "--no-restart: no restart")
|
||||
|
||||
args.Parse(os.Args[2:])
|
||||
shouldRestart := !*global && !*noRestart
|
||||
var nextArg = 0
|
||||
appName := args.Arg(0)
|
||||
if appName == "" && !*global {
|
||||
common.LogFail("Please specify an app or --global")
|
||||
}
|
||||
|
||||
if *global {
|
||||
appName = "--global"
|
||||
} else {
|
||||
nextArg = 1
|
||||
}
|
||||
|
||||
env, err := configenv.NewFromTarget(appName)
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
|
||||
var changed = false
|
||||
keys := args.Args()[nextArg:]
|
||||
for _, k := range keys {
|
||||
common.LogInfo1(fmt.Sprintf("Unsetting %s", k))
|
||||
env.Unset(k)
|
||||
changed = true
|
||||
}
|
||||
|
||||
if changed {
|
||||
env.Write()
|
||||
args := append([]string{appName, "unset"}, keys...)
|
||||
common.PlugnTrigger("post-config-update", args...)
|
||||
}
|
||||
|
||||
if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
|
||||
common.LogInfo1(fmt.Sprintf("Restarting app %s", appName))
|
||||
cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName)
|
||||
cmd.Execute()
|
||||
}
|
||||
appName, keys := config.GetCommonArgs(*global, args.Args())
|
||||
config.Unset(appName, keys, !*noRestart)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user