all repos — paprika @ 812bfbf89dc1a08aaf6a7174442126c9ff88f451

go rewrite of taigabot

config/config.go (view raw)

 1package config
 2
 3import (
 4	"io"
 5	"log"
 6	"os"
 7
 8	"gopkg.in/yaml.v3"
 9)
10
11type Config struct {
12	Nick string
13	Pass string
14	Host string
15	Sasl string
16	Tls bool
17	Channels []string
18	DbPath string `yaml:"db-path"`
19	ApiKeys map[string]string `yaml:"api-keys"`
20}
21
22var C Config
23
24func init() {
25	configPath := ""
26	inC := false
27	initMode := false
28	for _, v := range os.Args {
29		switch v {
30		case "-c", "--config":
31			inC = true
32		case "-h", "--help":
33			usage()
34		default:
35			if inC {
36				configPath = v
37			} else if v == "init" {
38				initMode = true
39			} else if v == "help" {
40				usage()
41			}
42		}
43	}
44
45	if initMode {
46		createConfig(configPath)
47	}
48
49	var file *os.File
50	var err error
51	if configPath == "" {
52		file = findConfig()
53	} else {
54		file, err = os.Open(configPath)
55		if err != nil {
56			log.Printf("Error: Could not open config path.")
57			log.Fatalf("- %s", err)
58		}
59	}
60	defer file.Close()
61
62	data, err := io.ReadAll(file)
63	err = yaml.Unmarshal(data, &C)
64	if err != nil {
65		log.Fatal(err)
66	}
67
68	if C.DbPath == "" {
69		C.DbPath = getDbPath()
70	}
71}
72