Skip to content

Commit

Permalink
Merge pull request #7 from systemli/fix-rootcmd-args
Browse files Browse the repository at this point in the history
fix environment handling
  • Loading branch information
0x46616c6b authored May 25, 2021
2 parents 7a82c80 + 1f935e2 commit 96c9918
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
28 changes: 12 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&etherpadUrl, "etherpad.url", "http://localhost:9001", "URL to access Etherpad (Env: ETHERPAD_URL)")
cmd.PersistentFlags().StringVar(&etherpadApiKey, "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" {
Expand Down
30 changes: 30 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"

Expand All @@ -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")
}

0 comments on commit 96c9918

Please sign in to comment.