forked from leighmcculloch/vangen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (115 loc) · 3.12 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
var version = "<not set>"
func main() {
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func run() error {
printHelp := flag.Bool("help", false, "print this help list")
printVersion := flag.Bool("version", false, "print program version")
verbose := flag.Bool("verbose", false, "print verbose output when run")
filename := flag.String("config", "vangen.json", "vangen json configuration `filename`")
outputDir := flag.String("out", "vangen/", "output `directory` that static files will be written to")
noOverwrite := flag.Bool("no-overwrite", false, "If an output file already exists, stops with a non-zero return code")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Vangen is a tool for generating static HTML for hosting Go repositories at a vanity import path.\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n\n")
fmt.Fprintf(os.Stderr, " vangen [-config=vangen.json] [-out=vangen/]\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n\n")
flag.PrintDefaults()
}
flag.Parse()
if *printHelp {
flag.Usage()
return nil
}
if *printVersion {
fmt.Fprintln(os.Stderr, version)
return nil
}
cf, err := os.Open(*filename)
if err != nil {
return err
}
c, err := parseConfig(cf)
if err != nil {
return err
}
err = os.MkdirAll(*outputDir, os.ModePerm)
if err != nil {
return fmt.Errorf("making dir %s: %w", *outputDir, err)
}
if c.Index {
pathOut := filepath.Join(*outputDir, "index.html")
f, err := os.Create(pathOut)
if err != nil {
return fmt.Errorf("writing file %s: %w", pathOut, err)
}
defer func() {
err := f.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "closing file %s: %v", pathOut, err)
}
}()
if *verbose {
fmt.Fprintf(os.Stderr, "Writing %s\n", pathOut)
}
err = generate_index(f, c.Domain, c.Repositories)
if err != nil {
return fmt.Errorf("generating index: %w", err)
}
err = f.Sync()
if err != nil {
return fmt.Errorf("flushing file %s: %w", pathOut, err)
}
}
for _, r := range c.Repositories {
for _, p := range r.Packages() {
dirOut := filepath.Join(*outputDir, p)
err = os.MkdirAll(dirOut, os.ModePerm)
if err != nil {
return fmt.Errorf("making dir %s: %v", dirOut, err)
}
pathOut := filepath.Join(dirOut, "index.html")
if *noOverwrite {
if _, err := os.Stat(pathOut); !os.IsNotExist(err) {
if err == nil {
return fmt.Errorf("cannot overwrite output file %s: %w", pathOut, err)
}
return fmt.Errorf("checking file %s: %w", pathOut, err)
}
}
f, err := os.Create(pathOut)
if err != nil {
return fmt.Errorf("writing file %s: %v", pathOut, err)
}
defer func() {
err := f.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "closing file %s: %v", pathOut, err)
}
}()
if *verbose {
fmt.Fprintf(os.Stderr, "Writing %s\n", pathOut)
}
err = generate_package(f, c.Domain, c.DocsDomain, p, r)
if err != nil {
return fmt.Errorf("generating package %s: %w", p, err)
}
err = f.Sync()
if err != nil {
return fmt.Errorf("flushing file %s: %w", pathOut, err)
}
}
}
return nil
}