-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgo-code-visualizer.go
69 lines (53 loc) · 1.3 KB
/
go-code-visualizer.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
package main
import (
"bufio"
"github.com/codehipster/go-code-visualizer/formatter"
"github.com/maelkum/go-code-visualizer/parser"
"log"
"os"
"path/filepath"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
var dir string
if len(os.Args) > 1 {
dir = os.Args[1]
} else {
// get directory where binary is located if no args
// CWD makes more sense but let's keep existing behaviour as-is
exe_dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
dir = exe_dir
}
parsedGoCodeFiles := make([]formatter.ParsedCode, 0)
//walk the filesystem.
walkFunc := func(path string, info os.FileInfo, err error) error {
//Skip .git directory.
if info.IsDir() && info.Name() == ".git" {
return filepath.SkipDir
}
//Parse if file is .go file.
extension := filepath.Ext(path)
if strings.ToLower(extension) == ".go" {
parsedGoCode := parser.ParseFile(path)
parsedGoCodeFiles = append(parsedGoCodeFiles, parsedGoCode)
}
return nil
}
filepath.Walk(dir, walkFunc)
dotGraph := formatter.GenerateDotGraph(parsedGoCodeFiles)
//Create/overwrite a file
cvFile, err := os.Create(dir + "/dot-visual.gv")
check(err)
defer cvFile.Close()
writer := bufio.NewWriter(cvFile)
writer.WriteString(dotGraph)
writer.Flush()
}