-
Notifications
You must be signed in to change notification settings - Fork 16
/
configuration.go
115 lines (102 loc) · 2.22 KB
/
configuration.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package honeybadger
import (
"log"
"os"
"strconv"
"time"
)
// The Logger interface is implemented by the standard log package and requires
// a limited subset of the interface implemented by log.Logger.
type Logger interface {
Printf(format string, v ...interface{})
}
// Configuration manages the configuration for the client.
type Configuration struct {
APIKey string
Root string
Env string
Hostname string
Endpoint string
Sync bool
Timeout time.Duration
Logger Logger
Backend Backend
}
func (c1 *Configuration) update(c2 *Configuration) *Configuration {
if c2.APIKey != "" {
c1.APIKey = c2.APIKey
}
if c2.Root != "" {
c1.Root = c2.Root
}
if c2.Env != "" {
c1.Env = c2.Env
}
if c2.Hostname != "" {
c1.Hostname = c2.Hostname
}
if c2.Endpoint != "" {
c1.Endpoint = c2.Endpoint
}
if c2.Timeout > 0 {
c1.Timeout = c2.Timeout
}
if c2.Logger != nil {
c1.Logger = c2.Logger
}
if c2.Backend != nil {
c1.Backend = c2.Backend
}
c1.Sync = c2.Sync
return c1
}
func newConfig(c Configuration) *Configuration {
config := &Configuration{
APIKey: getEnv("HONEYBADGER_API_KEY"),
Root: getPWD(),
Env: getEnv("HONEYBADGER_ENV"),
Hostname: getHostname(),
Endpoint: getEnv("HONEYBADGER_ENDPOINT", "https://api.honeybadger.io"),
Timeout: getTimeout(),
Logger: log.New(os.Stderr, "[honeybadger] ", log.Flags()),
Sync: getSync(),
}
config.update(&c)
if config.Backend == nil {
config.Backend = newServerBackend(config)
}
return config
}
func getTimeout() time.Duration {
if env := getEnv("HONEYBADGER_TIMEOUT"); env != "" {
if ns, err := strconv.ParseInt(env, 10, 64); err == nil {
return time.Duration(ns)
}
}
return 3 * time.Second
}
func getEnv(key string, fallback ...string) (val string) {
val = os.Getenv(key)
if val == "" && len(fallback) > 0 {
return fallback[0]
}
return
}
func getHostname() (hostname string) {
if val, err := os.Hostname(); err == nil {
hostname = val
}
return getEnv("HONEYBADGER_HOSTNAME", hostname)
}
func getPWD() (pwd string) {
if val, err := os.Getwd(); err == nil {
pwd = val
}
return getEnv("HONEYBADGER_ROOT", pwd)
}
func getSync() bool {
if getEnv("HONEYBADGER_SYNC") != "" {
return true
}
return false
}