move config parsing into a separate module, fix project name

This commit is contained in:
Von Random 2025-02-09 16:06:39 +02:00
parent 0ab510adef
commit d6d8b1a9a6
4 changed files with 46 additions and 25 deletions

18
config/config.go Normal file
View file

@ -0,0 +1,18 @@
package config
import (
"log"
"os"
"gopkg.in/yaml.v3"
)
func Parse(path string, spec interface{}) error {
configData, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
return err
}
err = yaml.Unmarshal(configData, spec)
return nil
}

15
main.go
View file

@ -2,18 +2,25 @@ package main
import ( import (
"log" "log"
"os"
"time" "time"
"mechanus.net/pgobot/matcher" "mechanus.net/pgbot/config"
"mechanus.net/pgobot/responder" "mechanus.net/pgbot/matcher"
"mechanus.net/pgbot/responder"
"gopkg.in/telebot.v4" "gopkg.in/telebot.v4"
) )
type ConfigSpec struct {
Token string `yaml:"token"`
}
func main() { func main() {
var conf ConfigSpec
config.Parse("config.yml", &conf)
pref := telebot.Settings{ pref := telebot.Settings{
Token: os.Getenv("TOKEN"), Token: conf.Token,
Poller: &telebot.LongPoller{Timeout: 10 * time.Second}, Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
} }

View file

@ -1,12 +1,16 @@
package matcher package matcher
import ( import (
"os"
"regexp" "regexp"
"gopkg.in/yaml.v3" "mechanus.net/pgbot/config"
) )
type tokenSpec struct {
Token string `yaml:"token"`
Regex []string `yaml:"regex"`
}
type Matcher struct { type Matcher struct {
tokens []TokenCompiled tokens []TokenCompiled
} }
@ -16,11 +20,11 @@ type TokenCompiled struct {
regex []*regexp.Regexp regex []*regexp.Regexp
} }
func createToken(token string, regexList []string) TokenCompiled { func createToken(spec tokenSpec) TokenCompiled {
result := new(TokenCompiled) result := new(TokenCompiled)
result.token = token result.token = spec.Token
result.regex = make([]*regexp.Regexp, len(regexList)) result.regex = make([]*regexp.Regexp, len(spec.Regex))
for i, regex := range regexList { for i, regex := range spec.Regex {
result.regex[i] = regexp.MustCompile(regex) result.regex[i] = regexp.MustCompile(regex)
} }
return *result return *result
@ -46,22 +50,14 @@ func (m *Matcher) Tokenize(input string) []string {
return result return result
} }
func InitMatcher(config string) *Matcher { func InitMatcher(tokenPath string) *Matcher {
configData, err := os.ReadFile(config) var list []tokenSpec
if err != nil { _ = config.Parse(tokenPath, &list)
panic(err)
}
var configMap map[string][]string
err = yaml.Unmarshal(configData, &configMap) tokens := make([]TokenCompiled, 0, len(list))
if err != nil {
panic(err)
}
tokens := make([]TokenCompiled, 0, len(configMap)) for _, spec := range list {
tokensCompiled := createToken(spec)
for k, v := range configMap {
tokensCompiled := createToken(k, v)
tokens = append(tokens, tokensCompiled) tokens = append(tokens, tokensCompiled)
} }
result := new(Matcher) result := new(Matcher)

View file

@ -3,7 +3,7 @@ package responder
import ( import (
"strings" "strings"
"mechanus.net/pgobot/matcher" "mechanus.net/pgbot/matcher"
"gopkg.in/telebot.v4" "gopkg.in/telebot.v4"
) )