From be5e81b24673dbc834a412354331a04e6ced2ae5 Mon Sep 17 00:00:00 2001 From: Alex Quick Date: Tue, 18 Jul 2017 00:06:49 -0400 Subject: [PATCH] [config] config:get exit with status 1 if key is not set in env --- plugins/config/config.go | 14 ++++++++++++++ plugins/config/src/subcommands/get/get.go | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/plugins/config/config.go b/plugins/config/config.go index a39cf1c56..17785b337 100644 --- a/plugins/config/config.go +++ b/plugins/config/config.go @@ -17,6 +17,20 @@ func GetWithDefault(appName string, key string, defaultValue string) string { return env.GetDefault(key, defaultValue) } +//HasKey determines if the config given by appName has a value for the given key +func HasKey(appName string, key string) bool { + env, err := loadConfig(appName) + if err != nil { + return false + } + for _, v := range env.Keys() { + if v == key { + return true + } + } + return false +} + //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) { global := appName == "" diff --git a/plugins/config/src/subcommands/get/get.go b/plugins/config/src/subcommands/get/get.go index f9d4fa162..6419bb8c7 100644 --- a/plugins/config/src/subcommands/get/get.go +++ b/plugins/config/src/subcommands/get/get.go @@ -24,8 +24,11 @@ func main() { if len(keys) == 0 { common.LogFail("Expected: key") } - value := config.GetWithDefault(appName, keys[0], "") + if !config.HasKey(appName, keys[0]) { + os.Exit(1) + } + value := config.GetWithDefault(appName, keys[0], "") if *quoted { fmt.Printf("'%s'", configenv.SingleQuoteEscape(value)) } else {