-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
98 lines (80 loc) · 2.16 KB
/
config.go
File metadata and controls
98 lines (80 loc) · 2.16 KB
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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
type IrcConfig struct {
host string
port uint16
nick string
pass string
channels []string
}
type CmdConfig struct {
triggers []string
}
type Config struct {
irc IrcConfig
cmd CmdConfig
dsn string
}
type LoadError struct {
variable string
what string
}
func (e *LoadError) Error() string {
return fmt.Sprintf("Environment variable %s %s",
e.variable, e.what)
}
// LoadConfig assembles the configuration used by the pump19 irc go-lem.
// It retrieves that information from environment variables.
// It returns a fully assembled Config or an error indicated a missing setting.
func LoadConfig() (*Config, error) {
cfg := Config{}
// IrcConfig {{{
if hostStr, exists := os.LookupEnv("PUMP19_IRC_HOSTNAME"); !exists {
return nil, &LoadError{"PUMP19_IRC_HOSTNAME", "is not set"}
} else {
cfg.irc.host = hostStr
}
if portStr, exists := os.LookupEnv("PUMP19_IRC_PORT"); !exists {
return nil, &LoadError{"PUMP19_IRC_PORT", "is not set"}
} else if port, err := strconv.ParseUint(portStr, 10, 16); err != nil {
return nil, &LoadError{"PUMP19_IRC_PORT", "cannot be parsed"}
} else {
cfg.irc.port = uint16(port)
}
if nickStr, exists := os.LookupEnv("PUMP19_IRC_NICKNAME"); !exists {
return nil, &LoadError{"PUMP19_IRC_NICKNAME", "is not set"}
} else {
cfg.irc.nick = nickStr
}
if passStr, exists := os.LookupEnv("PUMP19_IRC_PASSWORD"); !exists {
return nil, &LoadError{"PUMP19_IRC_PASSWORD", "is not set"}
} else {
cfg.irc.pass = passStr
}
if chanStr, exists := os.LookupEnv("PUMP19_IRC_CHANNELS"); !exists {
return nil, &LoadError{"PUMP19_IRC_CHANNELS", "is not set"}
} else {
cfg.irc.channels = strings.Split(chanStr, ",")
}
// IrcConfig }}}
// CmdConfig {{{
if triggerStr, exists := os.LookupEnv("PUMP19_CMD_TRIGGER"); !exists {
return nil, &LoadError{"PUMP19_CMD_TRIGGER", "is not set"}
} else {
cfg.cmd.triggers = strings.Split(triggerStr, "")
}
// CmdConfig }}}
// Database DSN {{{
if dsnStr, exists := os.LookupEnv("PUMP19_DSN"); !exists {
return nil, &LoadError{"PUMP19_DSN", "is not set"}
} else {
cfg.dsn = dsnStr
}
// Database DSN }}}
return &cfg, nil
}