Refactor environment mapping into separate function

This commit is contained in:
Enrico Stahn 2018-02-25 22:14:25 +11:00
parent e3385e0a31
commit 4596f8c89b
No known key found for this signature in database
GPG key ID: 5263621C269A50DE
2 changed files with 13 additions and 10 deletions

View file

@ -101,3 +101,15 @@ func initLogger() {
log.SetLevel(lvl) 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)
}
}
}
}

View file

@ -15,7 +15,6 @@ package cmd
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -134,13 +133,5 @@ func init() {
"PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count", "PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count",
} }
for env, flag := range envs { mapEnvVars(envs, serverCmd)
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)
}
}
}
} }