/
config.go
78 lines (63 loc) · 2.05 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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
irc "github.com/fluffle/goirc/client"
)
type Config struct {
// TODO: Support multiple networks
Network ConfigNetworks `json:"network"`
ModuleConfig map[string]json.RawMessage `json:"module_config,omitempty"`
}
type ChannelConfig struct {
Modules []string `json:"modules,omitempty"`
}
type ConfigNetworks struct {
Nick string `json:"nick"`
Server string `json:"host"`
Pass string `json:"pass"`
SSL bool `json:"ssl,omitempty"`
Channels map[string]ChannelConfig `json:"channels"`
OnConnectCmds []string `json:"on_connect_commands"`
}
func (cn *ConfigNetworks) GoIrcConfig() *irc.Config {
cfg := irc.NewConfig(cn.Nick)
cfg.SSL = cn.SSL
cfg.SSLConfig = &tls.Config{ServerName: cn.Server}
cfg.Server = cn.Server
cfg.Pass = cn.Pass
cfg.Me.Ident = "mipples"
cfg.Me.Name = "Mipples bot (http://github.com/nemith/mipples)"
cfg.QuitMessage = "I love dem mipples!"
return cfg
}
func loadConfig() *Config {
var config *Config
file, err := ioutil.ReadFile("mipples.json")
if err != nil {
panic(err)
}
if err := json.Unmarshal(file, &config); err != nil {
syntaxErr, ok := err.(*json.SyntaxError)
if !ok {
log.Fatalf("Cannot read config: %s", err)
}
// We have a syntax error. Extract out the line number and friends.
// https://groups.google.com/forum/#!topic/golang-nuts/fizimmXtVfc
newline := []byte{'\x0a'}
// Calculate the start/end position of the line where the error is
start := bytes.LastIndex(file[:syntaxErr.Offset], newline) + 1
end := len(file)
if idx := bytes.Index(file[start:], newline); idx >= 0 {
end = start + idx
}
// Count the line number we're on plus the offset in the line
line := bytes.Count(file[:start], newline) + 1
pos := int(syntaxErr.Offset) - start - 1
log.Fatalf("Cannot read config. Error in line %d, char %d: %s\n%s",
line, pos, syntaxErr, file[start:end])
}
return config
}