summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/matterbridge/gozulipbot/flag.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-08-21 00:14:33 +0200
committerGitHub <noreply@github.com>2020-08-21 00:14:33 +0200
commitb451285af70df0651989b43149eb748648ac8c1b (patch)
tree9a3d9f9093cdceb7325bcddfda36983862b5b80b /vendor/github.com/matterbridge/gozulipbot/flag.go
parent63a1847cdc895a3e968c567c20d20359f0785ce2 (diff)
downloadmatterbridge-msglm-b451285af70df0651989b43149eb748648ac8c1b.tar.gz
matterbridge-msglm-b451285af70df0651989b43149eb748648ac8c1b.tar.bz2
matterbridge-msglm-b451285af70df0651989b43149eb748648ac8c1b.zip
Sync with upstream gozulipbot fixes (#1202)
Diffstat (limited to 'vendor/github.com/matterbridge/gozulipbot/flag.go')
-rw-r--r--vendor/github.com/matterbridge/gozulipbot/flag.go53
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
}