config.go

 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
73
74
75
76
77
78
package common

import (
	"encoding/json"
	"fmt"
	"html/template"
	"os"
	"path/filepath"
	"slices"
)

type LimitsConfig struct {
	MaxPatchSizeMB   int
	MaxCommentLength int
}

type Config struct {
	LimitsConfig

	BaseDir           string // the directory where static html/css files are stored
	RepoDir           string // the directory where static repo html files will be stored
	RequestsDir       string // the directory where saved change requests will be stored
	ClientID          string
	CallbackURI       string
	Templates         *template.Template
	CommitDepth       int // the number of commits per repository to statically compile into pages (0 = all)
	MaintainerDomains []string

	RepoBases []string

	RepoPathMap map[string]string
}

func (c *Config) IsMaintainer(domain string) bool {
	return slices.Contains(c.MaintainerDomains, domain)
}

func LoadConfig(path string) (*Config, error) {

	configBytes, err := os.ReadFile("senary.json")
	if err != nil {
		return nil, fmt.Errorf("could not parse config file: %v", err)
	}

	var config Config
	err = json.Unmarshal(configBytes, &config)
	if err != nil {
		return nil, fmt.Errorf("could not parse config file: %v", err)
	}

	templates, err := template.ParseGlob("./templates/*.tpl.html")
	if err != nil {
		return nil, fmt.Errorf("could not parse template files: %v", err)
	}

	if config.MaxPatchSizeMB == 0 {
		config.MaxPatchSizeMB = 20
	}

	if config.MaxCommentLength == 0 {
		config.MaxCommentLength = 8192
	}

	config.Templates = templates
	config.RepoPathMap = make(map[string]string)
	for _, base := range config.RepoBases {

		dirEntries, _ := os.ReadDir(base)

		for _, e := range dirEntries {
			if e.IsDir() {
				config.RepoPathMap[e.Name()] = filepath.Join(base, e.Name())
			}
		}
	}

	return &config, nil
}