From 1f935e2103313c9c90acc60a448b7f06af1023fb Mon Sep 17 00:00:00 2001 From: louis Date: Tue, 25 May 2021 22:48:19 +0200 Subject: [PATCH] fix environment handling --- cmd/root.go | 28 ++++++++++++---------------- cmd/root_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index e5746a9..0ed9ed5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,29 +27,25 @@ func NewRootCmd() *cobra.Command { Long: "Etherpad Toolkit is a collection for most common Etherpad maintenance tasks.", } - cmd.PersistentFlags().String("etherpad.url", "http://localhost:9001", "URL to access Etherpad (Env: ETHERPAD_URL)") - cmd.PersistentFlags().String("etherpad.apikey", "", "API Key for Etherpad (Env: ETHERPAD_APIKEY)") - cmd.PersistentFlags().String("log.level", "info", "Log level (Env: LOG_LEVEL)") - cmd.PersistentFlags().String("log.format", "text", "Format for log output (Env: LOG_FORMAT)") + cmd.PersistentFlags().StringVar(ðerpadUrl, "etherpad.url", "http://localhost:9001", "URL to access Etherpad (Env: ETHERPAD_URL)") + cmd.PersistentFlags().StringVar(ðerpadApiKey, "etherpad.apikey", "", "API Key for Etherpad (Env: ETHERPAD_APIKEY)") + cmd.PersistentFlags().StringVar(&logLevel, "log.level", "info", "Log level (Env: LOG_LEVEL)") + cmd.PersistentFlags().StringVar(&logFormat, "log.format", "text", "Format for log output (Env: LOG_FORMAT)") - etherpadUrl = os.Getenv("ETHERPAD_URL") - if etherpadUrl == "" { - etherpadUrl = cmd.Flag("etherpad.url").Value.String() + if os.Getenv("ETHERPAD_URL") != "" { + etherpadUrl = os.Getenv("ETHERPAD_URL") } - etherpadApiKey = os.Getenv("ETHERPAD_APIKEY") - if etherpadApiKey == "" { - etherpadApiKey = cmd.Flag("etherpad.apikey").Value.String() + if os.Getenv("ETHERPAD_APIKEY") != "" { + etherpadApiKey = os.Getenv("ETHERPAD_APIKEY") } - logLevel = os.Getenv("LOG_LEVEL") - if logLevel == "" { - logLevel = cmd.Flag("log.level").Value.String() + if os.Getenv("LOG_LEVEL") != "" { + logLevel = os.Getenv("LOG_LEVEL") } - logFormat = os.Getenv("LOG_FORMAT") - if logFormat == "" { - logFormat = cmd.Flag("log.format").Value.String() + if os.Getenv("LOG_FORMAT") != "" { + logFormat = os.Getenv("LOG_FORMAT") } if logFormat == "json" { diff --git a/cmd/root_test.go b/cmd/root_test.go index a079da3..b73eff9 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "io/ioutil" + "os" "strings" "testing" @@ -24,5 +25,34 @@ func TestNewRootCmd(t *testing.T) { } assert.NotEmpty(t, string(out)) + assert.Equal(t, etherpadApiKey, "") + assert.Equal(t, etherpadUrl, "http://localhost:9001") assert.Equal(t, cmd.Long, strings.TrimRight(string(out), "\n")) } + +func TestNewRootCmdArgs(t *testing.T) { + cmd := NewRootCmd() + cmd.SetArgs([]string{"--etherpad.apikey", "1"}) + err := cmd.Execute() + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, etherpadApiKey, "1") + assert.Equal(t, etherpadUrl, "http://localhost:9001") +} + +func TestNewRootCmdEnv(t *testing.T) { + _ = os.Setenv("ETHERPAD_APIKEY", "1") + + cmd := NewRootCmd() + err := cmd.Execute() + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, etherpadApiKey, "1") + assert.Equal(t, etherpadUrl, "http://localhost:9001") + + _ = os.Unsetenv("ETHERPAD_APIKEY") +}