legit/config/config.go

67 lines
1.5 KiB
Go

package config
import (
"fmt"
"os"
"html/template"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
Repo struct {
ScanPath string `yaml:"scanPath"`
Readme []string `yaml:"readme"`
MainBranch []string `yaml:"mainBranch"`
Ignore []string `yaml:"ignore,omitempty"`
} `yaml:"repo"`
Dirs struct {
Templates string `yaml:"templates"`
Static string `yaml:"static"`
} `yaml:"dirs"`
Meta struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
Footer string `yaml:"footer,omitempty"`
MaintainerEmail string `yaml:"maintainerEmail,omitempty"`
CompiledFooter template.HTML `yaml:"thisIsNotSupposedToBeHere,omitempty"`
} `yaml:"meta"`
Server struct {
Name string `yaml:"name,omitempty"`
Host string `yaml:"host"`
Port int `yaml:"port"`
} `yaml:"server"`
}
func compileFooter(c *Config, version string) {
if c.Meta.Footer != "" {
c.Meta.CompiledFooter = template.HTML(
strings.ReplaceAll(
strings.ReplaceAll(
c.Meta.Footer,
"VERSION",
version),
"MAINTAINER",
fmt.Sprintf(
"<a href='mailto:%s'>%s</a>",
c.Meta.MaintainerEmail,
c.Meta.MaintainerEmail)))
}
}
func Read(f, v string) (*Config, error) {
b, err := os.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("reading config: %w", err)
}
c := Config{}
if err := yaml.Unmarshal(b, &c); err != nil {
return nil, fmt.Errorf("parsing config: %w", err)
}
compileFooter(&c, v)
return &c, nil
}