legit/config/config.go

69 lines
1.5 KiB
Go
Raw Permalink Normal View History

2022-12-11 05:52:47 +00:00
package config
import (
"fmt"
2023-02-04 16:34:21 +00:00
"html/template"
2024-01-30 04:08:08 +00:00
"os"
2023-02-04 16:34:21 +00:00
"strings"
2022-12-11 05:52:47 +00:00
"gopkg.in/yaml.v3"
)
type Config struct {
2022-12-13 04:28:40 +00:00
Repo struct {
ScanPath string `yaml:"scanPath"`
Readme []string `yaml:"readme"`
MainBranch []string `yaml:"mainBranch"`
2022-12-22 15:43:49 +00:00
Ignore []string `yaml:"ignore,omitempty"`
2022-12-13 04:28:40 +00:00
} `yaml:"repo"`
Dirs struct {
Templates string `yaml:"templates"`
Static string `yaml:"static"`
} `yaml:"dirs"`
2022-12-11 05:52:47 +00:00
Meta struct {
2024-01-30 04:08:08 +00:00
Title string `yaml:"title"`
Description string `yaml:"description"`
Footer string `yaml:"footer,omitempty"`
MaintainerEmail string `yaml:"maintainerEmail,omitempty"`
CompiledFooter template.HTML `yaml:"thisIsNotSupposedToBeHere,omitempty"`
2022-12-11 05:52:47 +00:00
} `yaml:"meta"`
2022-12-12 17:28:23 +00:00
Server struct {
2022-12-19 04:27:25 +00:00
Name string `yaml:"name,omitempty"`
2022-12-12 17:28:23 +00:00
Host string `yaml:"host"`
2022-12-13 04:28:40 +00:00
Port int `yaml:"port"`
2022-12-12 17:28:23 +00:00
} `yaml:"server"`
2024-01-30 04:08:08 +00:00
Mailmap string `yaml:"mailmap,omitempty"`
2022-12-11 05:52:47 +00:00
}
2023-02-04 16:34:21 +00:00
func compileFooter(c *Config, version string) {
2024-01-30 04:08:08 +00:00
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)))
}
2023-02-04 16:34:21 +00:00
}
func Read(f, v string) (*Config, error) {
2022-12-11 05:52:47 +00:00
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)
}
2024-01-30 04:08:08 +00:00
2023-02-04 16:34:21 +00:00
compileFooter(&c, v)
2022-12-11 05:52:47 +00:00
return &c, nil
}