-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (68 loc) · 2.12 KB
/
main.go
File metadata and controls
85 lines (68 loc) · 2.12 KB
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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/spf13/cobra"
)
var Version string
func main() {
var (
debug bool
direction Direction
maxTraces uint
useFixedStrings bool
timeout time.Duration
showVersion bool
)
rootCmd := &cobra.Command{
Use: "tracemod",
Short: "Trace a module dependency from a Go project",
RunE: func(cmd *cobra.Command, args []string) error {
if showVersion {
fmt.Println(Version)
return nil
}
rootModule, err := detectRootModule()
if err != nil {
return err
}
goModGraph, err := parseGoModGraph()
if err != nil {
return err
}
moduleFilter := args[0]
isModuleMatching, err := buildModuleMatchingFunc(moduleFilter, useFixedStrings)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
chains, err := computeDependencyChains(ctx, goModGraph, rootModule, isModuleMatching)
if err != nil {
return err
}
diagram := generateMermaidDiagram(cancel, rootModule, isModuleMatching, direction, chains, maxTraces)
if debug {
fmt.Println(diagram)
return nil
}
filepath, err := generateHTML(diagram)
if err != nil {
return err
}
return open(filepath)
},
}
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "Show the Mermaid diagram instead of generating and opening the HTML rendering")
rootCmd.PersistentFlags().UintVarP(&maxTraces, "max-traces", "m", 0, "Limit the number of maximum traces to detect")
rootCmd.PersistentFlags().VarP(&direction, "direction", "d", `Direction of the dependency tree, defaults to "LR"`)
rootCmd.PersistentFlags().BoolVarP(&useFixedStrings, "fixed-strings", "F", false, "Treat all patterns as literals instead of as regular expressions.")
rootCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "t", 30*time.Second, "Timeout duration")
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "Show program version")
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to execute: %v", err)
os.Exit(1)
}
}