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