forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
133 lines (103 loc) · 3.15 KB
/
trace.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cmd
import (
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/gonum/graph"
"github.com/gonum/graph/encoding/dot"
depgraph "github.com/openshift/origin/tools/depcheck/pkg/graph"
)
var traceImportsExample = `# create a dependency graph
%[1]s trace --root=github.com/openshift/origin --entry="./cmd/..."
# create a dependency graph using OpenShift-specific settings
%[1]s trace --root=github.com/openshift/origin --entry="./cmd/..." --openshift
# create a dependency graph and output in "dot" format
%[1]s trace --root=github.com/openshift/origin --entry=pkg/foo/... --output=dot --openshift
`
type TraceOptions struct {
GraphOptions *depgraph.GraphOptions
outputGraphName string
OutputFormat string
Out io.Writer
ErrOut io.Writer
}
type TraceFlags struct {
GraphFlags *depgraph.GraphFlags
OutputFormat string
}
func NewCmdTraceImports(parent string, out, errout io.Writer) *cobra.Command {
traceFlags := &TraceFlags{
GraphFlags: &depgraph.GraphFlags{},
OutputFormat: "dot",
}
cmd := &cobra.Command{
Use: "trace --root=github.com/openshift/origin --entry=pkg/foo/...",
Short: "Creates a dependency graph for a given repository",
Long: "Creates a dependency graph for a given repository, for every Go package reachable from a set of --entry points into the codebase",
Example: fmt.Sprintf(traceImportsExample, parent),
RunE: func(c *cobra.Command, args []string) error {
opts, err := traceFlags.ToOptions(out, errout)
if err != nil {
return err
}
if err := opts.Complete(); err != nil {
return err
}
if err := opts.Validate(); err != nil {
return err
}
if err := opts.Run(); err != nil {
return err
}
return nil
},
}
traceFlags.GraphFlags.AddFlags(cmd)
cmd.Flags().StringVarP(&traceFlags.OutputFormat, "output", "o", traceFlags.OutputFormat, "output generated dependency graph in specified format. One of: dot.")
return cmd
}
func (o *TraceOptions) Complete() error {
return o.GraphOptions.Complete()
}
func (o *TraceOptions) Validate() error {
if err := o.GraphOptions.Validate(); err != nil {
return err
}
if len(o.OutputFormat) > 0 && o.OutputFormat != "dot" {
return fmt.Errorf("invalid output format provided: %s", o.OutputFormat)
}
return nil
}
func (o *TraceFlags) ToOptions(out, errout io.Writer) (*TraceOptions, error) {
graphOpts, err := o.GraphFlags.ToOptions(out, errout)
if err != nil {
return nil, err
}
return &TraceOptions{
GraphOptions: graphOpts,
OutputFormat: o.OutputFormat,
outputGraphName: o.GraphFlags.RepoImportPath,
Out: out,
ErrOut: errout,
}, nil
}
// Run creates a dependency graph using user-specified options.
// Outputs graph contents in the format specified.
func (o *TraceOptions) Run() error {
g, err := o.GraphOptions.BuildGraph()
if err != nil {
return err
}
return o.outputGraph(g)
}
func (o *TraceOptions) outputGraph(g graph.Directed) error {
if o.OutputFormat != "dot" {
return fmt.Errorf("invalid output format provided: %s", o.OutputFormat)
}
data, err := dot.Marshal(g, fmt.Sprintf("%q", o.outputGraphName), "", " ", false)
if err != nil {
return err
}
fmt.Fprintf(o.Out, "%v\n", string(data))
return nil
}