[config] refactor package structure
This commit is contained in:
@@ -4,32 +4,33 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
configenv "github.com/dokku/dokku/plugins/config/src/configenv"
|
||||
)
|
||||
|
||||
//GetWithDefault gets a value from a config. If appName is empty the global config is used.
|
||||
func GetWithDefault(appName string, key string, defaultValue string) (value string) {
|
||||
env, err := loadConfig(appName)
|
||||
//Get retreives a value from a config. If appName is empty the global config is used.
|
||||
func Get(appName string, key string) (value string, ok bool) {
|
||||
env, err := loadAppOrGlobalEnv(appName)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
return "", false
|
||||
}
|
||||
return env.GetDefault(key, defaultValue)
|
||||
return env.Get(key)
|
||||
}
|
||||
|
||||
//HasKey determines if the config given by appName has a value for the given key
|
||||
func HasKey(appName string, key string) (ok bool) {
|
||||
env, err := loadConfig(appName)
|
||||
if err != nil {
|
||||
return false
|
||||
//GetWithDefault gets a value from a config. If appName is empty the global config is used. If the appName or key do not exist defaultValue is returned.
|
||||
func GetWithDefault(appName string, key string, defaultValue string) (value string) {
|
||||
value, ok := Get(appName, key)
|
||||
if !ok {
|
||||
return defaultValue
|
||||
}
|
||||
_, ok = env.Get(key)
|
||||
return ok
|
||||
return value
|
||||
}
|
||||
|
||||
//SetMany variables in the environment. If appName is empty the global config is used. If restart is true the app is restarted.
|
||||
func SetMany(appName string, entries map[string]string, restart bool) {
|
||||
func SetMany(appName string, entries map[string]string, restart bool) (err error) {
|
||||
global := appName == ""
|
||||
env := GetConfig(appName, false)
|
||||
env, err := loadAppOrGlobalEnv(appName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
keys := make([]string, 0, len(entries))
|
||||
for k, v := range entries {
|
||||
env.Set(k, v)
|
||||
@@ -37,20 +38,24 @@ func SetMany(appName string, entries map[string]string, restart bool) {
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
common.LogInfo1("Setting config vars")
|
||||
fmt.Println(configenv.PrettyPrintEnvEntries(" ", entries))
|
||||
fmt.Println(prettyPrintEnvEntries(" ", entries))
|
||||
env.Write()
|
||||
args := append([]string{appName, "set"}, keys...)
|
||||
common.PlugnTrigger("post-config-update", args...)
|
||||
}
|
||||
if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
|
||||
Restart(appName)
|
||||
triggerRestart(appName)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//UnsetMany a value in a config. If appName is empty the global config is used. If restart is true the app is restarted.
|
||||
func UnsetMany(appName string, keys []string, restart bool) {
|
||||
func UnsetMany(appName string, keys []string, restart bool) (err error) {
|
||||
global := appName == ""
|
||||
env := GetConfig(appName, false)
|
||||
env, err := loadAppOrGlobalEnv(appName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var changed = false
|
||||
for _, k := range keys {
|
||||
common.LogInfo1(fmt.Sprintf("Unsetting %s", k))
|
||||
@@ -63,53 +68,19 @@ func UnsetMany(appName string, keys []string, restart bool) {
|
||||
common.PlugnTrigger("post-config-update", args...)
|
||||
}
|
||||
if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
|
||||
Restart(appName)
|
||||
triggerRestart(appName)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//GetCommonArgs extracts common positional args (appName and keys)
|
||||
func GetCommonArgs(global bool, args []string) (appName string, keys []string) {
|
||||
nextArg := 0
|
||||
if !global {
|
||||
if len(args) > 0 {
|
||||
appName = args[0]
|
||||
}
|
||||
if appName == "" {
|
||||
common.LogFail("Please specify an app or --global")
|
||||
} else {
|
||||
nextArg++
|
||||
}
|
||||
}
|
||||
keys = args[nextArg:]
|
||||
return appName, keys
|
||||
}
|
||||
|
||||
//GetConfig for the given app (global config if appName is empty). Merge with global config if merged is true.
|
||||
func GetConfig(appName string, merged bool) (env *configenv.Env) {
|
||||
env, err := loadConfig(appName)
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
if appName != "" && merged {
|
||||
global, err := configenv.LoadGlobal()
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
global.Merge(env)
|
||||
return global
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
//Restart trigger restart on app
|
||||
func Restart(appName string) {
|
||||
func triggerRestart(appName string) {
|
||||
common.LogInfo1(fmt.Sprintf("Restarting app %s", appName))
|
||||
common.PlugnTrigger("app-restart", appName)
|
||||
}
|
||||
|
||||
func loadConfig(appName string) (env *configenv.Env, err error) {
|
||||
func loadAppOrGlobalEnv(appName string) (env *Env, err error) {
|
||||
if appName == "" || appName == "--global" {
|
||||
return configenv.LoadGlobal()
|
||||
return LoadGlobalEnv()
|
||||
}
|
||||
return configenv.LoadApp(appName)
|
||||
return LoadAppEnv(appName)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user