all repos — legit @ 4f5d35ffd22e8727c25ba0b028c0baade25cb32f

web frontend for git, written in go

config/config.go (view raw)

 1package config
 2
 3import (
 4	"fmt"
 5	"os"
 6	"path/filepath"
 7
 8	"gopkg.in/yaml.v3"
 9)
10
11type Config struct {
12	Repo struct {
13		ScanPath   string   `yaml:"scanPath"`
14		Readme     []string `yaml:"readme"`
15		MainBranch []string `yaml:"mainBranch"`
16		Ignore     []string `yaml:"ignore,omitempty"`
17	} `yaml:"repo"`
18	Dirs struct {
19		Templates string `yaml:"templates"`
20		Static    string `yaml:"static"`
21	} `yaml:"dirs"`
22	Meta struct {
23		Title       string `yaml:"title"`
24		Description string `yaml:"description"`
25	} `yaml:"meta"`
26	Server struct {
27		Name string `yaml:"name,omitempty"`
28		Host string `yaml:"host"`
29		Port int    `yaml:"port"`
30	} `yaml:"server"`
31}
32
33func Read(f string) (*Config, error) {
34	b, err := os.ReadFile(f)
35	if err != nil {
36		return nil, fmt.Errorf("reading config: %w", err)
37	}
38
39	c := Config{}
40	if err := yaml.Unmarshal(b, &c); err != nil {
41		return nil, fmt.Errorf("parsing config: %w", err)
42	}
43
44	if c.Repo.ScanPath, err = filepath.Abs(c.Repo.ScanPath); err != nil {
45		return nil, err
46	}
47	if c.Dirs.Templates, err = filepath.Abs(c.Dirs.Templates); err != nil {
48		return nil, err
49	}
50	if c.Dirs.Static, err = filepath.Abs(c.Dirs.Static); err != nil {
51		return nil, err
52	}
53
54	return &c, nil
55}