Skip to content

Commit

Permalink
make persistent flags also available as environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed May 15, 2021
1 parent 19a19b7 commit 7a82c80
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
Expand Down
30 changes: 26 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -25,10 +27,30 @@ func NewRootCmd() *cobra.Command {
Long: "Etherpad Toolkit is a collection for most common Etherpad maintenance tasks.",
}

cmd.PersistentFlags().StringVar(&etherpadUrl, "etherpad.url", "http://localhost:9001", "URL to access Etherpad")
cmd.PersistentFlags().StringVar(&etherpadApiKey, "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{})
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 7a82c80

Please sign in to comment.