gshr
git static host repo -- generates static html for repos
git clone https://git.vogt.world/gshr.git
Log | Files | README.md | LICENSE
← All files
name: config.go
-rw-r--r--
1831
 1package main
 2
 3import (
 4	"errors"
 5	"fmt"
 6	"os"
 7	"path"
 8	"regexp"
 9
10	"github.com/BurntSushi/toml"
11)
12
13type config struct {
14	Site  siteConfig   `toml:"site"`
15	Repos []repoConfig `toml:"repos"`
16}
17
18func (c *config) validate() {
19	names := map[string]bool{}
20	for _, r := range c.Repos {
21		_, duplicate := names[r.Name]
22		if duplicate {
23			checkErr(errors.New(fmt.Sprintf("duplicate repo name: '%s'", r.Name)))
24		}
25		r.validate()
26		names[r.Name] = true
27	}
28}
29
30type repoConfig struct {
31	Name        string `toml:"name"`
32	Description string `toml:"description"`
33	URL         string `toml:"url"`
34	AltLink     string `toml:"alt_link"`
35}
36
37func (r *repoConfig) validate() {
38	ok, err := regexp.MatchString(`[A-Za-z0-9_.-]`, r.Name)
39	checkErr(err)
40	if !ok {
41		checkErr(errors.New("repo names only allow [A-Za-z0-9_.-]"))
42	}
43	if r.Name == "git" {
44		checkErr(errors.New("repo in config cannot have the name 'git'"))
45	}
46}
47
48type siteConfig struct {
49	BaseURL string `toml:"base_url"`
50	Name    string `toml:"name"`
51}
52
53// cloneDir gets the directory that this repo was cloned into using the output directory
54// from the program arguments, and this repo's name.
55func (r *repoConfig) cloneDir() string {
56	return path.Join(args.OutputDir, r.Name, "git")
57}
58
59func (r *repoConfig) findFileInRoot(oneOfThese map[string]bool) string {
60	dir, err := os.ReadDir(r.cloneDir())
61	checkErr(err)
62	for _, e := range dir {
63		name := e.Name()
64		if _, ok := oneOfThese[name]; ok {
65			return name
66		}
67	}
68	return ""
69}
70
71func parseConfig(data string) config {
72	conf := config{}
73	_, err := toml.Decode(data, &conf)
74	checkErr(err)
75	return conf
76}
77
78type repoData struct {
79	repoConfig
80	AltLink         string
81	BaseURL         string
82	HeadData        HeadData
83	ReadMePath      string
84	LicenseFilePath string
85}
86
87type HeadData struct {
88	GenTime  string
89	BaseURL  string
90	SiteName string
91}