diff --git a/plugins/common/common.go b/plugins/common/common.go index 7d0f1106e..92c9076fc 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -150,3 +150,32 @@ func PlugnTrigger(triggerName string, args ...string) (string, error) { res, err := sh.Command("plugn", "trigger", triggerName, args).Output() 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)) +} diff --git a/plugins/config/config.go b/plugins/config/config.go new file mode 100644 index 000000000..29869cc09 --- /dev/null +++ b/plugins/config/config.go @@ -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) +} diff --git a/plugins/config/src/commands/commands.go b/plugins/config/src/commands/commands.go index edbbadc3c..02c909ace 100644 --- a/plugins/config/src/commands/commands.go +++ b/plugins/config/src/commands/commands.go @@ -8,6 +8,7 @@ import ( "strings" common "github.com/dokku/dokku/plugins/common" + "github.com/dokku/dokku/plugins/config" configenv "github.com/dokku/dokku/plugins/config/src/configenv" columnize "github.com/ryanuber/columnize" ) @@ -20,9 +21,10 @@ Display all global or app-specific config vars Additional commands:` helpContent = ` - config:get (|--global) KEY, Display a global or app-specific config value - config:set (|--global) [--encoded] [--no-restart] KEY1=VALUE1 [KEY2=VALUE2 ...], Set one or more config vars - config:unset (|--global) KEY1 [KEY2 ...], Unset one or more config vars + config (|--global), Pretty-print an app or global environment + config:get (|--global) KEY, Display a global or app-specific config value + config:set (|--global) [--encoded] [--no-restart] KEY1=VALUE1 [KEY2=VALUE2 ...], Set one or more config vars + config:unset (|--global) KEY1 [KEY2 ...], Unset one or more config vars config:export (|--global) [--envfile], Export a global or app environment ` ) @@ -33,13 +35,17 @@ func main() { cmd := flag.Arg(0) switch cmd { - case "config": + case "config", "config:show": target := flag.Arg(1) + if target == "" { + usage() + common.LogFail("Please specify an app or --global") + } env, err := configenv.NewFromTarget(target) if err != nil { common.LogFail(err.Error()) } else { - fmt.Println(env.String()) + fmt.Println(config.PrettyPrintLogEntries("", env.Map())) } case "config:help": usage() diff --git a/plugins/config/src/subcommands/set/set.go b/plugins/config/src/subcommands/set/set.go index b01be629f..f3504883b 100644 --- a/plugins/config/src/subcommands/set/set.go +++ b/plugins/config/src/subcommands/set/set.go @@ -8,6 +8,7 @@ import ( "strings" common "github.com/dokku/dokku/plugins/common" + config "github.com/dokku/dokku/plugins/config" "github.com/dokku/dokku/plugins/config/src/configenv" ) @@ -36,8 +37,8 @@ func main() { common.LogFail(err.Error()) } - var changed = false entries := args.Args()[nextArg:] + updated := make(map[string]string) for _, e := range entries { //log parts := strings.SplitN(e, "=", 2) @@ -53,17 +54,19 @@ func main() { value = string(decoded) } env.Set(key, value) - changed = true + updated[key] = value } - if changed { - //log + 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() } diff --git a/plugins/config/src/subcommands/unset/unset.go b/plugins/config/src/subcommands/unset/unset.go index ed72cf44b..7b37e1811 100644 --- a/plugins/config/src/subcommands/unset/unset.go +++ b/plugins/config/src/subcommands/unset/unset.go @@ -4,6 +4,8 @@ import ( "flag" "os" + "fmt" + common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config/src/configenv" ) @@ -35,19 +37,19 @@ func main() { var changed = false keys := args.Args()[nextArg:] for _, k := range keys { - //log + common.LogInfo1(fmt.Sprintf("Unsetting %s", k)) 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) { + common.LogInfo1(fmt.Sprintf("Restarting app %s", appName)) cmd := common.NewTokenizedShellCmd("dokku", "ps:restart", appName) cmd.Execute() }