-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
153 lines (122 loc) · 3.98 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
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path"
"sort"
"strings"
"github.com/Icikowski/vanitygen/config"
"github.com/Icikowski/vanitygen/formatters"
"github.com/caarlos0/env/v9"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
const (
indexFile string = "index.html"
)
var args config.Arguments
func init() {
if err := env.ParseWithOptions(&args, env.Options{
Prefix: "VANITYGEN_",
}); err != nil {
log.Fatal(err)
}
flag.StringVar(&args.Output, "out", args.Output, "output directory")
flag.StringVar(&args.Config, "config", args.Config, "configuration file")
flag.StringVar(&args.Formatter, "format", args.Formatter, "output formatter [plain/bootstrap]")
flag.Parse()
}
func getDataLoader(filename string) (func([]byte, any) error, error) {
switch path.Ext(filename) {
case ".json":
return json.Unmarshal, nil
case ".yaml", ".yml":
return yaml.Unmarshal, nil
}
return nil, fmt.Errorf("unknown extension '%s'", path.Ext(filename))
}
func main() {
data, err := os.ReadFile(args.Config)
if err != nil {
log.Fatal(errors.Wrapf(err, "unable to read file '%s'", args.Config))
}
dataLoader, err := getDataLoader(args.Config)
if err != nil {
log.Fatal(errors.Wrap(err, "unable to determine data loader"))
}
var cfg config.Config
if err := dataLoader(data, &cfg); err != nil {
log.Fatal(errors.Wrap(err, "unable to unmarshal config"))
}
if err := cfg.Validate(); err != nil {
log.Fatal(errors.Wrap(err, "config is invalid"))
}
formatter, err := formatters.New(args.Formatter, cfg)
if err != nil {
log.Fatal(errors.Wrap(err, "unable to instantiate formatter"))
}
if err := os.MkdirAll(args.Output, 0755); err != nil && !os.IsExist(err) {
log.Fatal(errors.Wrap(err, "unable to create output directory"))
}
sort.SliceStable(cfg.Packages, func(i, j int) bool {
return strings.Compare(cfg.Packages[i].Name, cfg.Packages[j].Name) < 0
})
for n := range cfg.Packages {
if cfg.Packages[n].Subpackages != nil {
sort.SliceStable(cfg.Packages[n].Subpackages, func(i, j int) bool {
return strings.Compare(cfg.Packages[n].Subpackages[i], cfg.Packages[n].Subpackages[j]) < 0
})
}
}
for _, pkg := range cfg.Packages {
target := path.Join(args.Output, pkg.Name, indexFile)
log.Printf("building index for package '%s' in '%s'", pkg.Name, target)
if err := os.MkdirAll(path.Dir(target), 0755); err != nil && !os.IsExist(err) {
log.Fatal(errors.Wrap(err, "unable to create package output directory"))
}
out, subsOut, err := formatter.EncodePackage(pkg)
if err != nil {
log.Fatal(errors.Wrap(err, "unable to encode package"))
}
f, err := os.Create(target)
if err != nil {
log.Panic(errors.Wrapf(err, "unable to create package index '%s'", target))
}
defer f.Close()
if _, err := f.Write(out); err != nil {
log.Panic(errors.Wrapf(err, "unable to write package index '%s'", target))
}
for subName, subOut := range subsOut {
subTarget := path.Join(args.Output, pkg.Name, subName, indexFile)
log.Printf("building index for subpackage '%s' of '%s' in '%s'", subName, pkg.Name, subTarget)
if err := os.MkdirAll(path.Dir(subTarget), 0755); err != nil && !os.IsExist(err) {
log.Fatal(errors.Wrap(err, "unable to create package output directory"))
}
f, err := os.Create(subTarget)
if err != nil {
log.Panic(errors.Wrapf(err, "unable to create subpackage index '%s'", subTarget))
}
defer f.Close()
if _, err := f.Write(subOut); err != nil {
log.Panic(errors.Wrapf(err, "unable to write subpackage index '%s'", subTarget))
}
}
}
target := path.Join(args.Output, indexFile)
log.Printf("building index list in '%s'", target)
out, err := formatter.EncodeRoot(cfg.Packages)
if err != nil {
log.Fatal(errors.Wrap(err, "unable to encode index"))
}
f, err := os.Create(target)
if err != nil {
log.Panic(errors.Wrapf(err, "unable to create index '%s'", target))
}
defer f.Close()
if _, err := f.Write(out); err != nil {
log.Panic(errors.Wrapf(err, "unable to write index '%s'", target))
}
}