diff options
Diffstat (limited to 'vendor/github.com/matterbridge/gozulipbot/flag.go')
-rw-r--r-- | vendor/github.com/matterbridge/gozulipbot/flag.go | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/vendor/github.com/matterbridge/gozulipbot/flag.go b/vendor/github.com/matterbridge/gozulipbot/flag.go index 6305aac1..0a263388 100644 --- a/vendor/github.com/matterbridge/gozulipbot/flag.go +++ b/vendor/github.com/matterbridge/gozulipbot/flag.go @@ -3,30 +3,61 @@ package gozulipbot import ( "flag" "fmt" + "os" "time" ) func (b *Bot) GetConfigFromFlags() error { var ( - apiKey = flag.String("apikey", "", "bot api key") - apiURL = flag.String("apiurl", "", "url of zulip server") - email = flag.String("email", "", "bot email address") - backoff = flag.Duration("backoff", 1*time.Second, "backoff base duration") + apiKey = flag.String("apikey", "ZULIP_APIKEY", "bot api key or env var") + apiURL = flag.String("apiurl", "ZULIP_APIURL", "url of zulip server or env var") + backoff = flag.Duration("backoff", 1*time.Second, "backoff base duration or env var") + email = flag.String("email", "ZULIP_EMAIL", "bot email address or env var") + env = flag.Bool("env", false, "get string values from environment variables") ) flag.Parse() - if *apiKey == "" { + b.APIKey = *apiKey + b.APIURL = *apiURL + b.Email = *email + b.Backoff = *backoff + if *env { + b.GetConfigFromEnvironment() + } + return b.checkConfig() +} + +func (b *Bot) GetConfigFromEnvironment() error { + if apiKey, exists := os.LookupEnv(b.APIKey); !exists { + return fmt.Errorf("--env was set but env var %s did not exist", b.APIKey) + } else { + b.APIKey = apiKey + } + if apiURL, exists := os.LookupEnv(b.APIURL); !exists { + return fmt.Errorf("--env was set but env var %s did not exist", b.APIURL) + } else { + b.APIURL = apiURL + } + if email, exists := os.LookupEnv(b.Email); !exists { + return fmt.Errorf("--env was set but env var %s did not exist", b.Email) + } else { + b.Email = email + } + return nil +} + +func (b *Bot) checkConfig() error { + if b.APIKey == "" { return fmt.Errorf("--apikey is required") } - if *apiURL == "" { + if b.APIURL == "" { return fmt.Errorf("--apiurl is required") } - if *email == "" { + if b.Email == "" { return fmt.Errorf("--email is required") } - b.APIKey = *apiKey - b.APIURL = *apiURL - b.Email = *email - b.Backoff = *backoff + if b.Backoff <= 0 { + return fmt.Errorf("--backoff must be greater than zero") + } return nil } |