From 4596f8c89b31bc65cf851e7fab461542e2745e09 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 22:14:25 +1100 Subject: [PATCH] Refactor environment mapping into separate function --- cmd/root.go | 12 ++++++++++++ cmd/server.go | 11 +---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d19e5c4..9899ccc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -101,3 +101,15 @@ func initLogger() { log.SetLevel(lvl) } + +func mapEnvVars(envs map[string]string, cmd *cobra.Command) { + for env, flag := range envs { + flag := cmd.Flags().Lookup(flag) + flag.Usage = fmt.Sprintf("%v [env %v]", flag.Usage, env) + if value := os.Getenv(env); value != "" { + if err := flag.Value.Set(value); err != nil { + log.Error(err) + } + } + } +} diff --git a/cmd/server.go b/cmd/server.go index 4aa851e..4cb2beb 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -15,7 +15,6 @@ package cmd import ( "context" - "fmt" "net/http" "os" "os/signal" @@ -134,13 +133,5 @@ func init() { "PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count", } - for env, flag := range envs { - flag := serverCmd.Flags().Lookup(flag) - flag.Usage = fmt.Sprintf("%v [env %v]", flag.Usage, env) - if value := os.Getenv(env); value != "" { - if err := flag.Value.Set(value); err != nil { - log.Error(err) - } - } - } + mapEnvVars(envs, serverCmd) }