forked from davidschlachter/embedded-struct-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
81 lines (71 loc) · 2.09 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
// The embedded-struct-visualizer command builds a Graphviz DOT file
// representing the tree of embedded structs in a Go project
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
)
type Struct struct {
Name string
Package string
FilePath string
Embeds map[string]bool
}
var structsList []Struct
var verbose *bool
var excludePkg []string
func main() {
var (
searchPath = "./"
outputFile = os.Stdout
err error
flags flag.FlagSet
)
outputPath := flags.String("out", "", "write to file instead of stdout")
rankdir := flags.String("rankdir", "LR", "graphs direction")
excludeConfigFile := flags.String("exclude-pkg", "", "exclude go pkg config file")
verbose = flags.Bool("v", false, "verbose logging")
flags.Usage = help
flags.Parse(os.Args[1:])
if *excludeConfigFile != "" {
if err := parseExcludeConfig(*excludeConfigFile); err != nil {
fmt.Printf("Error read exclude config file: %v", err)
return
}
}
if len(flags.Args()) == 1 {
searchPath = flags.Arg(0)
}
if *outputPath != "" {
outputFile, err = os.OpenFile(*outputPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
fmt.Printf("Error writing output file: %v", err)
return
}
}
_ = filepath.WalkDir(searchPath, findGoFiles)
graph := buildDOTFile(*rankdir)
writer := bufio.NewWriter(outputFile)
_, err = writer.WriteString(graph)
if err != nil {
fmt.Printf("Error writing output file: %v", err)
return
}
writer.Flush()
}
func help() {
fmt.Printf("Usage: %s [OPTIONS] DirToScan\n", os.Args[0])
fmt.Printf("If the directory to scan is not provided, it defaults to './'\n")
fmt.Printf("OPTIONS:\n")
fmt.Printf(" -out <file> path to output file (default: write to stdout)\n")
fmt.Printf(" -rankdir <direction> graphs direction (default: LR, enum: TB,LR,BT,RL)\n")
fmt.Printf(" -exclude-pkg path to exclude pkg config file, format(default: empty):\n")
fmt.Printf(" eg: exclude gopkg.in/guregu/null.v3 and models/MyStuct\n")
fmt.Printf(" prefix:null.\n")
fmt.Printf(" models.MyStuct\n")
fmt.Printf(" -v verbose logging\n")
os.Exit(1)
}