-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
61 lines (53 loc) · 1.34 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
//go:build !coverage
package main
import (
"bufio"
"fmt"
"os"
"github.com/g4s8/envdoc/ast"
"github.com/g4s8/envdoc/debug"
"github.com/g4s8/envdoc/render"
)
func main() {
var cfg Config
if err := cfg.Load(); err != nil {
fatal("Failed to load config: %v", err)
}
if cfg.Debug {
debug.Config.Enabled = true
cfg.fprint(os.Stdout)
}
parser := ast.NewParser(cfg.FileGlob, cfg.TypeGlob,
ast.WithDebug(cfg.Debug),
ast.WithExecConfig(cfg.ExecFile, cfg.ExecLine))
converter := NewConverter(ConverterOpts{
EnvPrefix: cfg.EnvPrefix,
TagName: cfg.TagName,
TagDefault: cfg.TagDefault,
RequiredIfNoDef: cfg.RequiredIfNoDef,
UseFieldNames: cfg.FieldNames,
})
renderer := render.NewRenderer(cfg.OutFormat, cfg.NoStyles)
gen := NewGenerator(parser, converter, renderer)
out, err := os.Create(cfg.OutFile)
if err != nil {
fatal("Failed to open output file: %v", err)
}
buf := bufio.NewWriter(out)
defer func() {
if err := out.Close(); err != nil {
fatal("Failed to close output file: %v", err)
}
}()
if err := gen.Generate(cfg.Dir, buf); err != nil {
fatal("Failed to generate: %v", err)
}
if err := buf.Flush(); err != nil {
fatal("Failed to flush output: %v", err)
}
}
func fatal(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintln(os.Stderr)
os.Exit(1)
}