[config] first cut of config rewritten in golang

still to do:
* Rework disk format to be okay with quotes
* Fix Dockerfile deployments
* Dedup code in the subcommands
* Add --encode
* Add deploytests for the new stuff
* Restore some log messages (should I make some helpers in common?)
* Restore some functions that may be used by 3d-party plugins
This commit is contained in:
Alex Quick
2017-01-16 23:13:32 -05:00
parent 5e3184b61d
commit 294d4b1f29
23 changed files with 1187 additions and 312 deletions

View File

@@ -0,0 +1,40 @@
package main
import (
"flag"
"fmt"
"os"
common "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config/src/configenv"
)
// print the environment to stdout
func main() {
args := flag.NewFlagSet("config:export", flag.ExitOnError)
global := args.Bool("global", false, "--global: use the global environment")
envfile := args.Bool("envfile", false, "--envfile: export as envfile rather than bash exports")
args.Parse(os.Args[2:])
appName := args.Arg(0)
if appName == "" && !*global {
common.LogFail("Please specify an app or --global")
}
if *global {
if appName != "" {
common.LogFail("Trailing argument: " + appName)
}
appName = "--global"
}
env, err := configenv.NewFromTarget(appName)
if err != nil {
common.LogFail(err.Error())
}
if *envfile {
fmt.Println(env.EnvfileString())
} else {
fmt.Println(env.ExportfileString())
}
}

View File

@@ -0,0 +1,39 @@
package main
import (
"flag"
"fmt"
"os"
common "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config/src/configenv"
)
// get the given entries from the specified environment
func main() {
args := flag.NewFlagSet("config:get", flag.ExitOnError)
global := args.Bool("global", false, "--global: use the global environment")
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
}
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)
fmt.Printf("%s", env.GetDefault(key, ""))
}

View File

@@ -0,0 +1,61 @@
package main
import (
"flag"
"strings"
"os"
common "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config/src/configenv"
)
// set the given entries from the specified environment
func main() {
args := flag.NewFlagSet("config:set", 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
entries := args.Args()[nextArg:]
for _, e := range entries {
//log
parts := strings.SplitN(e, "=", 2)
if len(parts) == 1 {
common.LogFail("Invalid env pair: " + e)
}
env.Set(parts[0], parts[1])
changed = true
}
if changed {
//log
env.Write()
args := append([]string{appName, "set"}, entries...)
common.PlugnTrigger("post-config-update", args...)
}
if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName)
cmd.Execute()
}
}

View File

@@ -0,0 +1,54 @@
package main
import (
"flag"
"os"
common "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config/src/configenv"
)
//unset the given entries from the given environment
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 {
//log
env.Unset(k)
changed = true
}
if changed {
//log
env.Write()
args := append([]string{appName, "unset"}, keys...)
common.PlugnTrigger("post-config-update", args...)
}
if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName)
cmd.Execute()
}
}