-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (48 loc) · 1.02 KB
/
main.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
package main
import (
"io/ioutil"
"log"
"os"
"os/user"
"path"
"github.com/FryDay/zorkbot/config"
"github.com/FryDay/zorkbot/zorkbot"
"github.com/spf13/viper"
)
const defaultConfig = `[bot]
nick = "mybot"
password = ""
server = "irc.freenode.net"
port = 7000
channel = "#foo"`
func main() {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
confpath := path.Join(usr.HomeDir, ".config")
if _, err := os.Stat(confpath); os.IsNotExist(err) {
os.Mkdir(confpath, 0700)
}
v := viper.New()
v.SetConfigName("zorkbot")
v.SetConfigType("toml")
v.AddConfigPath(".")
v.AddConfigPath(confpath)
if _, ok := v.ReadInConfig().(viper.ConfigFileNotFoundError); ok {
// Create config file
newConf := []byte(defaultConfig)
if err := ioutil.WriteFile(path.Join(confpath, "zorkbot.toml"), newConf, 0600); err != nil {
log.Fatal(err)
}
}
var conf config.Config
if err = v.Unmarshal(&conf); err != nil {
log.Fatal(err)
}
zorkBot, err := zorkbot.NewBot(&conf)
if err != nil {
log.Fatal(err)
}
zorkBot.Run()
}