[config] improve logging and default command

This commit is contained in:
Alex Quick
2017-05-04 22:41:32 -04:00
parent 8b90d4ebf9
commit 08aa2b9fa4
5 changed files with 77 additions and 11 deletions

View File

@@ -150,3 +150,32 @@ func PlugnTrigger(triggerName string, args ...string) (string, error) {
res, err := sh.Command("plugn", "trigger", triggerName, args).Output() res, err := sh.Command("plugn", "trigger", triggerName, args).Output()
return string(res), err return string(res), err
} }
// LogInfo1 is the info1 header formatter
func LogInfo1(text string) {
fmt.Fprintln(os.Stdout, fmt.Sprintf("-----> %s", text))
}
// LogInfo1Quiet is the info1 header formatter (with quiet option)
func LogInfo1Quiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") != "" {
LogInfo1(text)
}
}
// LogInfo2 is the info2 header formatter
func LogInfo2(text string) {
fmt.Fprintln(os.Stdout, fmt.Sprintf("=====> %s", text))
}
// LogInfo2Quiet is the info2 header formatter (with quiet option)
func LogInfo2Quiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") != "" {
LogInfo2(text)
}
}
// LogWarn is the warning log formatter
func LogWarn(text string) {
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
}

26
plugins/config/config.go Normal file
View File

@@ -0,0 +1,26 @@
package config
import configenv "github.com/dokku/dokku/plugins/config/src/configenv"
import columnize "github.com/ryanuber/columnize"
import "fmt"
//GetWithDefault gets value from app config
func GetWithDefault(appName string, key string, defaultValue string) string {
env, err := configenv.NewFromTarget(appName)
if err != nil {
return defaultValue
}
return env.GetDefault(key, defaultValue)
}
//PrettyPrintLogEntries in columns
func PrettyPrintLogEntries(prefix string, entries map[string]string) string {
colConfig := columnize.DefaultConfig()
colConfig.Prefix = prefix
colConfig.Delim = "\x00"
lines := make([]string, 0, len(entries))
for k, v := range entries {
lines = append(lines, fmt.Sprintf("%s:\x00%s", k, v))
}
return columnize.Format(lines, colConfig)
}

View File

@@ -8,6 +8,7 @@ import (
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" common "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
configenv "github.com/dokku/dokku/plugins/config/src/configenv" configenv "github.com/dokku/dokku/plugins/config/src/configenv"
columnize "github.com/ryanuber/columnize" columnize "github.com/ryanuber/columnize"
) )
@@ -20,9 +21,10 @@ Display all global or app-specific config vars
Additional commands:` Additional commands:`
helpContent = ` helpContent = `
config:get (<app>|--global) KEY, Display a global or app-specific config value config (<app>|--global), Pretty-print an app or global environment
config:set (<app>|--global) [--encoded] [--no-restart] KEY1=VALUE1 [KEY2=VALUE2 ...], Set one or more config vars config:get (<app>|--global) KEY, Display a global or app-specific config value
config:unset (<app>|--global) KEY1 [KEY2 ...], Unset one or more config vars config:set (<app>|--global) [--encoded] [--no-restart] KEY1=VALUE1 [KEY2=VALUE2 ...], Set one or more config vars
config:unset (<app>|--global) KEY1 [KEY2 ...], Unset one or more config vars
config:export (<app>|--global) [--envfile], Export a global or app environment config:export (<app>|--global) [--envfile], Export a global or app environment
` `
) )
@@ -33,13 +35,17 @@ func main() {
cmd := flag.Arg(0) cmd := flag.Arg(0)
switch cmd { switch cmd {
case "config": case "config", "config:show":
target := flag.Arg(1) target := flag.Arg(1)
if target == "" {
usage()
common.LogFail("Please specify an app or --global")
}
env, err := configenv.NewFromTarget(target) env, err := configenv.NewFromTarget(target)
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFail(err.Error())
} else { } else {
fmt.Println(env.String()) fmt.Println(config.PrettyPrintLogEntries("", env.Map()))
} }
case "config:help": case "config:help":
usage() usage()

View File

@@ -8,6 +8,7 @@ import (
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" common "github.com/dokku/dokku/plugins/common"
config "github.com/dokku/dokku/plugins/config"
"github.com/dokku/dokku/plugins/config/src/configenv" "github.com/dokku/dokku/plugins/config/src/configenv"
) )
@@ -36,8 +37,8 @@ func main() {
common.LogFail(err.Error()) common.LogFail(err.Error())
} }
var changed = false
entries := args.Args()[nextArg:] entries := args.Args()[nextArg:]
updated := make(map[string]string)
for _, e := range entries { for _, e := range entries {
//log //log
parts := strings.SplitN(e, "=", 2) parts := strings.SplitN(e, "=", 2)
@@ -53,17 +54,19 @@ func main() {
value = string(decoded) value = string(decoded)
} }
env.Set(key, value) env.Set(key, value)
changed = true updated[key] = value
} }
if changed { if len(updated) != 0 {
//log common.LogInfo1("Setting config vars")
fmt.Println(config.PrettyPrintLogEntries(" ", updated))
env.Write() env.Write()
args := append([]string{appName, "set"}, entries...) args := append([]string{appName, "set"}, entries...)
common.PlugnTrigger("post-config-update", args...) common.PlugnTrigger("post-config-update", args...)
} }
if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) { if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
common.LogInfo1(fmt.Sprintf("Restarting app %s", appName))
cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName) cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName)
cmd.Execute() cmd.Execute()
} }

View File

@@ -4,6 +4,8 @@ import (
"flag" "flag"
"os" "os"
"fmt"
common "github.com/dokku/dokku/plugins/common" common "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config/src/configenv" "github.com/dokku/dokku/plugins/config/src/configenv"
) )
@@ -35,19 +37,19 @@ func main() {
var changed = false var changed = false
keys := args.Args()[nextArg:] keys := args.Args()[nextArg:]
for _, k := range keys { for _, k := range keys {
//log common.LogInfo1(fmt.Sprintf("Unsetting %s", k))
env.Unset(k) env.Unset(k)
changed = true changed = true
} }
if changed { if changed {
//log
env.Write() env.Write()
args := append([]string{appName, "unset"}, keys...) args := append([]string{appName, "unset"}, keys...)
common.PlugnTrigger("post-config-update", args...) common.PlugnTrigger("post-config-update", args...)
} }
if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) { if shouldRestart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
common.LogInfo1(fmt.Sprintf("Restarting app %s", appName))
cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName) cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName)
cmd.Execute() cmd.Execute()
} }