-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
81 lines (71 loc) · 1.74 KB
/
config.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package nettrigger
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
// Config holds configuration values.
type Config struct {
Params ArgMap
Rules []RuleSpec
Timeout time.Duration
GoogleProject string
DigitalOceanToken string
Concurrent bool
}
// DefaultConfig holds the default configuration values.
var DefaultConfig = Config{}
// ParseEnv will parse environment variables and apply them to the
// configuration.
func (c *Config) ParseEnv() error {
var (
googleProject, hasGoogleProject = os.LookupEnv("GOOGLE_PROJECT")
digitalOceanToken, hasDigitalOceanToken = os.LookupEnv("DIGITAL_OCEAN_TOKEN")
timeout, hasTimeout = os.LookupEnv("TIMEOUT")
concurrent, hasConcurrent = os.LookupEnv("CONCURRENT")
)
if hasGoogleProject {
c.GoogleProject = googleProject
}
if hasDigitalOceanToken {
c.DigitalOceanToken = digitalOceanToken
}
if hasTimeout {
t, err := time.ParseDuration(timeout)
if err != nil {
return fmt.Errorf("invalid timeout \"%s\": %v", timeout, err)
}
c.Timeout = t
}
if hasConcurrent {
b, err := strconv.ParseBool(concurrent)
if err != nil {
return fmt.Errorf("invalid concurrent value \"%s\": %v", concurrent, err)
}
c.Concurrent = b
}
for i := 1; i < 1000; i++ {
def, exists := os.LookupEnv(fmt.Sprintf("ARG%d", i))
if !exists {
break
}
if c.Params == nil {
c.Params = make(ArgMap)
}
c.Params[strings.ToLower(def)] = i - 1
}
for i := 1; i < 1000; i++ {
def, exists := os.LookupEnv(fmt.Sprintf("RULE%d", i))
if !exists {
break
}
rule, err := ParseRule(def)
if err != nil {
return fmt.Errorf("failed to parse rule %d: %v", i, err)
}
c.Rules = append(c.Rules, rule)
}
return nil
}