-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
90 lines (76 loc) · 2.32 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/mazrean/isucrud/dbdoc"
"github.com/mazrean/isucrud/internal/ui"
)
var (
version = "Unknown"
revision = "Unknown"
versionFlag bool
dst string
ignores sliceString
ignorePrefixes sliceString
ignoreMain, ignoreInitialize bool
web bool
addr string
base string
)
func init() {
flag.BoolVar(&versionFlag, "version", false, "show version")
flag.StringVar(&dst, "dst", "./dbdoc.md", "destination file")
flag.Var(&ignores, "ignore", "ignore function")
flag.Var(&ignorePrefixes, "ignorePrefix", "ignore function")
flag.BoolVar(&ignoreMain, "ignoreMain", true, "ignore main function")
flag.BoolVar(&ignoreInitialize, "ignoreInitialize", true, "ignore functions with 'initialize' in the name")
flag.BoolVar(&web, "web", false, "run as web server")
flag.StringVar(&addr, "addr", "localhost:7070", "address to listen on")
flag.StringVar(&base, "base", "/", "base for serving the web server")
}
func main() {
flag.Parse()
if versionFlag {
fmt.Printf("iwrapper %s (revision: %s)\n", version, revision)
return
}
wd, err := os.Getwd()
if err != nil {
panic(fmt.Errorf("failed to get working directory: %w", err))
}
nodes, err := dbdoc.Run(dbdoc.Config{
WorkDir: wd,
BuildArgs: flag.Args(),
IgnoreFuncs: ignores,
IgnoreFuncPrefixes: ignorePrefixes,
IgnoreMain: ignoreMain,
IgnoreInitialize: ignoreInitialize,
DestinationFilePath: dst,
})
if err != nil {
panic(fmt.Errorf("failed to run dbdoc: %w", err))
}
if web {
mux := http.NewServeMux()
mux.Handle("GET /", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Content-Type", "text/html")
targetNodeID := r.URL.Query().Get("node")
err := ui.RenderHTML(w, nodes, targetNodeID, base)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}))
log.Printf("open http://%s/ in your browser\n", addr)
if err := http.ListenAndServe(addr, mux); err != nil {
panic(fmt.Errorf("server exit: %w", err))
}
} else {
err = ui.RenderMarkdown(dst, nodes)
if err != nil {
panic(fmt.Errorf("failed to render markdown: %w", err))
}
}
}