1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package gozulipbot
import (
"flag"
"fmt"
"os"
"time"
)
func (b *Bot) GetConfigFromFlags() error {
var (
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()
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 b.APIURL == "" {
return fmt.Errorf("--apiurl is required")
}
if b.Email == "" {
return fmt.Errorf("--email is required")
}
if b.Backoff <= 0 {
return fmt.Errorf("--backoff must be greater than zero")
}
return nil
}
|