-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (112 loc) · 3.03 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)
var config *Config
var routes map[string]Module
type Config struct {
Outputdirectory string `yaml:"outputdirectory"`
DefaultBranch string `yaml:"defaultbranch"`
Vanitydomain string `yaml:"vanitydomain"`
Modules []Module `yaml:"modules"`
}
type Module struct {
Name string `yaml:"name"`
Repository string `yaml:"repository"`
Version string `yaml:"version"`
Branch string `yaml:"branch"`
Homepage string `yaml:"homepage"`
Subpackages []string `yaml:"subpackages"`
// Vanitydomain string
Vanitymodulename string
Versions map[string]string
}
func (m Module) GetRoutes() (routes []string) {
routes = append(routes, fmt.Sprintf("/%s", m.Name))
for _, subpackage := range m.Subpackages {
routes = append(routes, fmt.Sprintf("/%s/%s", m.Name, subpackage))
}
return
}
type TemplateData struct {
DefaultBranch string
Vanitydomain string
Module Module
}
func loadConfig() *Config {
var c Config
data, err := os.ReadFile("packages.yaml")
if err != nil {
log.Fatal().Err(err).Msg("error reading packages.yaml")
}
err = yaml.Unmarshal(data, &c)
if err != nil {
log.Fatal().Err(err).Msg("error unmarshalling packages.yaml")
}
return &c
}
func loadRoutes() (routes map[string]Module) {
routes = map[string]Module{}
for _, module := range config.Modules {
for _, route := range module.GetRoutes() {
routes[route] = module
}
}
return
}
func main() {
config = loadConfig()
routes = loadRoutes()
t, err := template.ParseFiles("templates/package.tmpl")
if err != nil {
log.Err(err).Msg("error parsing package template")
return
}
for route, module := range routes {
log.Info().Msgf("generating %s", route)
pkgDir := filepath.Join(config.Outputdirectory, route)
if err := os.MkdirAll(pkgDir, 0755); err != nil {
log.Err(err).Msg("error creating route")
return
}
module.Repository = strings.TrimSuffix(module.Repository, ".git")
module.Vanitymodulename = fmt.Sprintf("%s/%s", config.Vanitydomain, module.Name)
var page bytes.Buffer
data := TemplateData{
DefaultBranch: config.DefaultBranch,
Vanitydomain: config.Vanitydomain,
Module: module,
}
if err := t.Execute(&page, data); err != nil {
log.Err(err).Msg("error executing template")
return
}
pkgFile := filepath.Join(pkgDir, "index.html")
if err := os.WriteFile(pkgFile, page.Bytes(), 0755); err != nil {
log.Err(err).Msg("error writing package file")
return
}
}
t, err = template.ParseFiles("templates/index.tmpl")
if err != nil {
log.Err(err).Msg("error parsing index template")
return
}
var page bytes.Buffer
if err := t.Execute(&page, config); err != nil {
log.Err(err).Msg("error executing template")
return
}
pkgFile := filepath.Join(config.Outputdirectory, "index.html")
if err := os.WriteFile(pkgFile, page.Bytes(), 0755); err != nil {
log.Err(err).Msg("error writing index file")
return
}
}