all repos — paprika @ master

go rewrite of taigabot

config/config.go (view raw)

 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
package config

import (
	"io"
	"log"
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	Nick string
	Pass string
	Host string
	Sasl string
	Tls bool
	Channels []string
	DbPath string `yaml:"db-path"`
	ApiKeys map[string]string `yaml:"api-keys"`
}

var C Config

func init() {
	configPath := ""
	inC := false
	initMode := false
	for _, v := range os.Args {
		switch v {
		case "-c", "--config":
			inC = true
		case "-h", "--help":
			usage()
		default:
			if inC {
				configPath = v
			} else if v == "init" {
				initMode = true
			} else if v == "help" {
				usage()
			}
		}
	}

	if initMode {
		createConfig(configPath)
	}

	var file *os.File
	var err error
	if configPath == "" {
		file = findConfig()
	} else {
		file, err = os.Open(configPath)
		if err != nil {
			log.Printf("Error: Could not open config path.")
			log.Fatalf("- %s", err)
		}
	}
	defer file.Close()

	data, err := io.ReadAll(file)
	err = yaml.Unmarshal(data, &C)
	if err != nil {
		log.Fatal(err)
	}

	if C.DbPath == "" {
		C.DbPath = getDbPath()
	}
}