diff --git a/README.md b/README.md index f3d51ef..ae22e0e 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,12 @@ Available Commands: purge Removes old Pads entirely from Etherpad Flags: - --etherpad.apikey string API Key for Etherpad - --etherpad.url string URL to access Etherpad (default "http://localhost:9001") + --etherpad.apikey string API Key for Etherpad (Env: ETHERPAD_APIKEY) + --etherpad.url string URL to access Etherpad (Env: ETHERPAD_URL) (default "http://localhost:9001") -h, --help help for etherpad-toolkit - --log.format string Format for log output (default "text") - --log.level string Log level (default "info") + --log.format string Format for log output (Env: LOG_FORMAT) (default "text") + --log.level string Log level (Env: LOG_LEVEL) (default "info") + Use "etherpad-toolkit [command] --help" for more information about a command. ``` diff --git a/cmd/root.go b/cmd/root.go index 5234cc6..e5746a9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,8 @@ package cmd import ( + "os" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -25,10 +27,30 @@ func NewRootCmd() *cobra.Command { Long: "Etherpad Toolkit is a collection for most common Etherpad maintenance tasks.", } - cmd.PersistentFlags().StringVar(ðerpadUrl, "etherpad.url", "http://localhost:9001", "URL to access Etherpad") - cmd.PersistentFlags().StringVar(ðerpadApiKey, "etherpad.apikey", "", "API Key for Etherpad") - cmd.PersistentFlags().StringVar(&logLevel, "log.level", "info", "Log level") - cmd.PersistentFlags().StringVar(&logFormat, "log.format", "text", "Format for log output") + 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)") + + etherpadUrl = os.Getenv("ETHERPAD_URL") + if etherpadUrl == "" { + etherpadUrl = cmd.Flag("etherpad.url").Value.String() + } + + etherpadApiKey = os.Getenv("ETHERPAD_APIKEY") + if etherpadApiKey == "" { + etherpadApiKey = cmd.Flag("etherpad.apikey").Value.String() + } + + logLevel = os.Getenv("LOG_LEVEL") + if logLevel == "" { + logLevel = cmd.Flag("log.level").Value.String() + } + + logFormat = os.Getenv("LOG_FORMAT") + if logFormat == "" { + logFormat = cmd.Flag("log.format").Value.String() + } if logFormat == "json" { log.SetFormatter(&log.JSONFormatter{}) diff --git a/go.mod b/go.mod index a7f7c86..d28efa2 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,6 @@ require ( github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.1.3 github.com/stretchr/testify v1.7.0 + golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 6c15d15..69617cb 100644 --- a/go.sum +++ b/go.sum @@ -423,8 +423,9 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=