-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
110 lines (95 loc) · 2.46 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
package main // import "github.com/teamwork/kommentaar"
import (
"flag"
"fmt"
"io"
"os"
"runtime"
"runtime/pprof"
"github.com/teamwork/kommentaar/docparse"
"github.com/teamwork/kommentaar/kconfig"
"github.com/teamwork/kommentaar/openapi2"
)
func main() {
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "usage: kommentaar [pkg pkg...]\n\n")
flag.PrintDefaults()
os.Exit(2)
}
showUsage, err := start()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "kommentaar: %s\n", err)
if showUsage {
flag.Usage()
}
os.Exit(1)
}
}
var stdout io.Writer = os.Stdout
func start() (bool, error) {
config := flag.String("config", "", "configuration file")
debug := flag.Bool("debug", false, "print debug output to stderr")
addr := flag.String("serve", "", "serve HTML output on this address, instead of writing to\n"+
"stdout; every page load will rescan the source tree")
output := flag.String("output", "", `output function, valid values are:
openapi2-yaml OpenAPI/Swagger 2.0 as YAML
openapi2-json OpenAPI/Swagger 2.0 as JSON
openapi2-jsonindent OpenAPI/Swagger 2.0 as JSON indented
html HTML documentation
`)
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
return false, err
}
err = pprof.StartCPUProfile(f)
if err != nil {
return false, err
}
defer pprof.StopCPUProfile()
}
prog := docparse.NewProgram(*debug)
if *config != "" {
err := kconfig.Load(prog, *config)
if err != nil {
return false, err
}
}
if prog.Config.Output == nil {
prog.Config.Output = openapi2.WriteYAML
}
if *output != "" {
var err error
prog.Config.Output, err = kconfig.Output(*output, *addr)
if err != nil {
return true, fmt.Errorf("-output: %s", err)
}
}
pkgs := flag.Args()
if len(pkgs) > 0 {
prog.Config.Packages = pkgs
}
if len(prog.Config.Packages) == 0 {
prog.Config.Packages = []string{"."}
}
err := docparse.FindComments(stdout, prog)
if err != nil {
return false, err
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
return false, fmt.Errorf("could not create memory profile: %v", err)
}
runtime.GC()
err = pprof.WriteHeapProfile(f)
if err != nil {
return false, fmt.Errorf("could not write memory profile: %v", err)
}
_ = f.Close()
}
return false, nil
}