~handlerug/handlebot

ref: 7a899986d3a7d250418ed425c03a9e5391820979 handlebot/config.go -rw-r--r-- 1.2 KiB
7a899986Umar Getagazov Set user agent a month ago
                                                                                
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
package main

import (
	"encoding/json"
	"log"
	"os"
)

type Config struct {
	IRC          IRCConfig `json:"kwargs"`
	Channels     []string  `json:"channels"`
	Channel      string    `json:"channel"`
	HandlebotCfg `json:"handlebot"`
}

type IRCConfig struct {
	Server   string `json:"host"`
	TLS      bool   `json:"tls"`
	Port     int    `json:"port"`
	User     string `json:"user"`
	Name     string `json:"realname"`
	Nick     string `json:"nick"`
	Password string `json:"password"`
}

type HandlebotCfg struct {
	CommandPrefix string  `json:"command_prefix"`
	Database      string  `json:"database"`
	APIKeys       APIKeys `json:"keys"`
}

type APIKeys struct {
	YouTube        string `json:"youtube"`
	WolframAlpha   string `json:"wolframalpha"`
	MapQuest       string `json:"mapquest"`
	OpenWeatherMap string `json:"openweathermap"`
}

func parseConfig(path string) *Config {
	var cfg *Config

	file, err := os.Open(path)
	if err != nil {
		log.Fatal("couldn't open the config file: ", err)
	}
	defer file.Close()

	if err := json.NewDecoder(file).Decode(&cfg); err != nil {
		log.Fatal("couldn't parse the config file: ", err)
	}

	if cfg.CommandPrefix == "" {
		cfg.CommandPrefix = "."
	}
	return cfg
}