forked from caddyserver/nginx-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx-dirs.go
89 lines (81 loc) · 1.92 KB
/
nginx-dirs.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
//+build ignore
package main
import (
"encoding/xml"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
type Directive struct {
Name string `xml:"name,attr"`
Contexts []string `xml:"context"`
}
type Module struct {
Section []struct {
Directive []Directive `xml:"directive"`
} `xml:"section"`
}
const pathToDocs = "/xml/en/docs/http/ngx_*"
func main() {
if len(os.Args) < 2 {
log.Println("provide the path to the directory of nginx.org repo as argument")
os.Exit(1)
}
fullPath := filepath.Clean(filepath.Join(os.Args[1], pathToDocs))
matches, err := filepath.Glob(fullPath)
if err != nil {
log.Printf("error globing: %s", err)
os.Exit(1)
}
contextDirs := make(map[string][]string)
for _, v := range matches {
mods := []Module{}
func(m string) {
f, err := os.Open(m)
if err != nil {
log.Printf("error openning the file %s : %s", m, err)
return
}
defer f.Close()
dec := xml.NewDecoder(f)
dec.Strict = false // "Strict" implies it doesn't recognize   and &mdash
err = dec.Decode(&mods)
if err != nil {
// It complains about unexpected EOF in ngx_http_api_module_head.xml on line 261, which is actually the end of file.
// So ¯\_(ツ)_/¯
log.Printf("error unmarshalling the file %s : %s", m, err)
}
}(v)
for _, m := range mods {
if len(m.Section) == 0 {
continue
}
for _, s := range m.Section {
if len(s.Directive) == 0 {
continue
}
for _, d := range s.Directive {
for _, c := range d.Contexts {
contextDirs[c] = append(contextDirs[c], d.Name)
}
}
}
}
}
for ctx, dirs := range contextDirs {
func(ctx string, dirs []string) {
ctx = strings.ReplaceAll(ctx, " ", "_")
f, err := os.Create(fmt.Sprintf("%s.txt", ctx))
if err != nil {
log.Printf("Error creating out file: %s", err)
return
}
defer f.Close()
for _, d := range dirs {
f.WriteString(fmt.Sprintf("%s\n", d))
}
}(ctx, dirs)
}
}