-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
67 lines (57 loc) · 1.43 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
// 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
func main() {
var (
searchPath = "./"
outputFile = os.Stdout
err error
flags flag.FlagSet
)
outputPath := flags.String("out", "", "write to file instead of stdout")
verbose = flags.Bool("v", false, "verbose logging")
flags.Usage = help
flags.Parse(os.Args[1:])
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()
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(" -v verbose logging\n")
os.Exit(1)
}