Skip to content

Commit 7d7beaa

Browse files
authored
Merge pull request #42 from alenkacz/av/refactor
Wrap main graph properties into a struct
2 parents 4fe547a + 238bf4c commit 7d7beaa

File tree

6 files changed

+56
-26
lines changed

6 files changed

+56
-26
lines changed

cmd/cycles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ var cyclesCmd = &cobra.Command{
3131
Short: "Prints cycles in dependency chains.",
3232
Long: `Will show all the cycles in the dependencies of the project.`,
3333
RunE: func(cmd *cobra.Command, args []string) error {
34-
depGraph, _, mainModule := getDepInfo()
34+
overview := getDepInfo()
3535
var cycleChains []Chain
3636
var temp Chain
37-
getCycleChains(mainModule, depGraph, temp, &cycleChains)
37+
getCycleChains(overview.MainModuleName, overview.Graph, temp, &cycleChains)
3838
cycles := getCycles(cycleChains)
3939

4040
if !jsonOutputCycles {

cmd/graph.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var graphCmd = &cobra.Command{
3333
For example to generate a svg image use:
3434
twopi -Tsvg -o dag.svg graph.dot`,
3535
RunE: func(cmd *cobra.Command, args []string) error {
36-
depGraph, deps, mainModule := getDepInfo()
36+
overview := getDepInfo()
3737
// strict ensures that there is only one edge between two vertices
3838
// overlap = false ensures the vertices don't overlap
3939
fileContents := "strict digraph {\ngraph [overlap=false];\n"
@@ -42,10 +42,10 @@ var graphCmd = &cobra.Command{
4242
if dep != "" {
4343
var chains []Chain
4444
var temp Chain
45-
getAllChains(mainModule, depGraph, temp, &chains)
45+
getAllChains(overview.MainModuleName, overview.Graph, temp, &chains)
4646
fileContents += getFileContentsForSingleDep(chains, dep)
4747
} else {
48-
fileContents += getFileContentsForAllDeps(deps, depGraph, mainModule)
48+
fileContents += getFileContentsForAllDeps(overview)
4949
}
5050
fileContents += "}"
5151
fileContentsByte := []byte(fileContents)
@@ -102,18 +102,18 @@ func getFileContentsForSingleDep(chains []Chain, dep string) string {
102102

103103
// get the contents of the .dot file for the graph
104104
// of all dependencies (when -d is not set)
105-
func getFileContentsForAllDeps(deps []string, depGraph map[string][]string, mainModule string) string {
105+
func getFileContentsForAllDeps(overview *DependencyOverview) string {
106106

107107
// color the main module as yellow
108-
data := colorMainNode(mainModule)
109-
for _, dep := range deps {
110-
_, ok := depGraph[dep]
108+
data := colorMainNode(overview.MainModuleName)
109+
for _, dep := range overview.DepList {
110+
_, ok := overview.Graph[dep]
111111
if !ok {
112112
continue
113113
}
114114
// main module can never be a neighbour
115-
for _, neighbour := range depGraph[dep] {
116-
if dep == mainModule {
115+
for _, neighbour := range overview.Graph[dep] {
116+
if dep == overview.MainModuleName {
117117
// for the main module use a colored node
118118
data += fmt.Sprintf("\"MainNode\" -> \"%s\"\n", neighbour)
119119
} else {

cmd/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ var listCmd = &cobra.Command{
2929
Long: `Gives a list of all the dependencies of the project.
3030
These include both direct as well as transitive dependencies.`,
3131
RunE: func(cmd *cobra.Command, args []string) error {
32-
_, deps, _ := getDepInfo()
32+
depGraph := getDepInfo()
3333
fmt.Println("List of all dependencies:")
34-
printDeps(deps)
34+
printDeps(depGraph.DepList)
3535
return nil
3636
},
3737
}

cmd/stats.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ var statsCmd = &cobra.Command{
3737
2. Max Depth of Dependencies: Number of dependencies in the longest dependency chain
3838
3. Transitive Dependencies: Total number of transitive dependencies (dependencies which are not direct dependencies of the project)`,
3939
RunE: func(cmd *cobra.Command, args []string) error {
40-
depGraph, deps, mainModule := getDepInfo()
40+
depGraph := getDepInfo()
4141

4242
// get the longest chain
4343
var longestChain Chain
4444
var temp Chain
45-
getLongestChain(mainModule, depGraph, temp, &longestChain)
45+
getLongestChain(depGraph.MainModuleName, depGraph.Graph, temp, &longestChain)
4646

4747
// get values
48-
totalDeps := len(deps)
48+
totalDeps := len(depGraph.DepList)
4949
maxDepth := len(longestChain)
50-
directDeps := len(depGraph[mainModule])
50+
directDeps := len(depGraph.Graph[depGraph.MainModuleName])
5151
transitiveDeps := totalDeps - directDeps
5252

5353
if !jsonOutput {
@@ -58,7 +58,7 @@ var statsCmd = &cobra.Command{
5858

5959
if verbose {
6060
fmt.Println("All dependencies:")
61-
printDeps(deps)
61+
printDeps(depGraph.DepList)
6262
}
6363

6464
// print the longest chain

cmd/utils.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ func printChain(slice []string) {
3030
fmt.Println(strings.Join(slice, " -> "))
3131
}
3232

33-
func getDepInfo() (map[string][]string, []string, string) {
33+
// DependencyOverview holds dependency module informations
34+
type DependencyOverview struct {
35+
// Dependency graph edges modelled as node plus adjacency nodes
36+
Graph map[string][]string
37+
// List of all (including transitive) dependencies
38+
DepList []string
39+
// Name of the module from which the dependencies are computed
40+
MainModuleName string
41+
}
3442

43+
func getDepInfo() *DependencyOverview {
3544
// get output of "go mod graph" in a string
3645
goModGraph := exec.Command("go", "mod", "graph")
3746
goModGraphOutput, err := goModGraph.Output()
@@ -60,21 +69,26 @@ func getDepInfo() (map[string][]string, []string, string) {
6069
depGraph[words[0]] = append(depGraph[words[0]], words[1])
6170
}
6271

72+
isMainModule := false
6373
if mainModule == "notset" {
6474
mainModule = words[0]
65-
// we don't want to add mainModule to deps list
66-
continue
75+
isMainModule = true
6776
}
6877

69-
if !contains(deps, words[0]) {
78+
if !contains(deps, words[0]) && !isMainModule {
79+
// we don't want to add mainModule to deps list
7080
deps = append(deps, words[0])
7181
}
7282
if !contains(deps, words[1]) {
7383
deps = append(deps, words[1])
7484
}
7585

7686
}
77-
return depGraph, deps, mainModule
87+
return &DependencyOverview{
88+
Graph: depGraph,
89+
DepList: deps,
90+
MainModuleName: mainModule,
91+
}
7892
}
7993

8094
func printDeps(deps []string) {

cmd/utils_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func Test_getChains_simple(t *testing.T) {
4444
graph["F"] = []string{"H"}
4545

4646
deps := []string{"A", "B", "C", "D", "E", "F", "H"}
47+
overview := &DependencyOverview{
48+
Graph: graph,
49+
DepList: deps,
50+
MainModuleName: "A",
51+
}
4752

4853
var cycleChains []Chain
4954
var longestChain Chain
@@ -72,7 +77,7 @@ func Test_getChains_simple(t *testing.T) {
7277
"F" -> "H"
7378
`
7479

75-
if correctFileContentsForAllDeps != getFileContentsForAllDeps(deps, graph, "A") {
80+
if correctFileContentsForAllDeps != getFileContentsForAllDeps(overview) {
7681
t.Errorf("File contents for graph of all dependencies are wrong")
7782
}
7883

@@ -131,6 +136,11 @@ func Test_getChains_cycle(t *testing.T) {
131136
graph["H"] = []string{"D"}
132137

133138
deps := []string{"A", "B", "C", "D", "E", "F", "G", "H"}
139+
overview := &DependencyOverview{
140+
Graph: graph,
141+
DepList: deps,
142+
MainModuleName: "A",
143+
}
134144

135145
var cycleChains []Chain
136146
var longestChain Chain
@@ -152,7 +162,7 @@ func Test_getChains_cycle(t *testing.T) {
152162
"G" -> "H"
153163
"H" -> "D"
154164
`
155-
if correctFileContentsForAllDeps != getFileContentsForAllDeps(deps, graph, "A") {
165+
if correctFileContentsForAllDeps != getFileContentsForAllDeps(overview) {
156166
t.Errorf("File contents for graph of all dependencies are wrong")
157167
}
158168

@@ -218,6 +228,12 @@ func Test_getChains_cycle_2(t *testing.T) {
218228

219229
deps := []string{"A", "B", "C", "D", "E", "F"}
220230

231+
overview := &DependencyOverview{
232+
Graph: graph,
233+
DepList: deps,
234+
MainModuleName: "A",
235+
}
236+
221237
var cycleChains []Chain
222238
var longestChain Chain
223239
var chains []Chain
@@ -245,7 +261,7 @@ func Test_getChains_cycle_2(t *testing.T) {
245261
"E" -> "F"
246262
"F" -> "D"
247263
`
248-
if correctFileContentsForAllDeps != getFileContentsForAllDeps(deps, graph, "A") {
264+
if correctFileContentsForAllDeps != getFileContentsForAllDeps(overview) {
249265
t.Errorf("File contents for graph of all dependencies are wrong")
250266
}
251267

0 commit comments

Comments
 (0)