Skip to content

Commit 3656bb4

Browse files
committed
add -ol and -or flags
1 parent 34050c2 commit 3656bb4

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ clustering and no clear separation between them:
145145

146146
### Explain
147147

148-
Given two pieces of code, displays what are the dependencies between them, for example:
148+
Given two pieces of code, displays what are the dependencies between them. These pieces
149+
of code are specified using a glob patterns, for example:
149150

150151
```shell
151152
dep-tree explain 'src/products/**/*.go' 'src/orders/**/*.go'
@@ -168,6 +169,23 @@ src/products/price.go -> src/orders/order_manager.go
168169
src/products/storage.go -> src/orders/order_manager.go
169170
```
170171

172+
Additionally, the `--overlap-left` (`-ol`) or `--overlap-right` (`-or`) arguments can be passed:
173+
- `--overlap-left`: when the left and right glob patterns have some files in common, keep only the
174+
common files at the left, and discard them from the right. This flag is useful for retrieving any
175+
external dependencies of a specific folder:
176+
```shell
177+
# Retrieves dependencies from files in src/products to any other file that is not inside src/products
178+
dep-tree explain 'src/products/**/*.go' '**/*.go' --overlap-left
179+
```
180+
181+
- `--overlap-right`: when the left and right glob patterns have some files in common, keep only the
182+
common files at the right, and discard them from the left. This flag is useful for retrieving
183+
any file outside a specific folder that depends on that folder.
184+
```shell
185+
# Retrieves dependencies from any folder but src/products that point to files inside src/products
186+
dep-tree explain '**/*.go' 'src/products/**/*.go' --overlap-right
187+
```
188+
171189
### Tree
172190

173191
Choose the file that will act as the root of the dependency graph (for example `my-file.py`), and run:

cmd/explain.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"errors"
45
"os"
56
"slices"
67
"strings"
@@ -9,16 +10,24 @@ import (
910
"github.com/gabotechs/dep-tree/internal/explain"
1011
"github.com/gabotechs/dep-tree/internal/graph"
1112
"github.com/gabotechs/dep-tree/internal/language"
13+
"github.com/gabotechs/dep-tree/internal/utils"
1214
"github.com/spf13/cobra"
1315
)
1416

1517
func ExplainCmd(cfgF func() (*config.Config, error)) *cobra.Command {
18+
var overlapLeft bool
19+
var overlapRight bool
20+
1621
cmd := &cobra.Command{
1722
Use: "explain",
1823
Short: "Shows all the dependencies between two parts of the code",
1924
GroupID: explainGroupId,
2025
Args: cobra.ExactArgs(2),
2126
RunE: func(cmd *cobra.Command, args []string) error {
27+
if overlapLeft && overlapRight {
28+
return errors.New("only one of --overlap-left (-ol) or --overlap-right (-or) can be used at a time")
29+
}
30+
2231
fromFiles, err := filesFromArgs([]string{args[0]})
2332
if err != nil {
2433
return err
@@ -29,6 +38,14 @@ func ExplainCmd(cfgF func() (*config.Config, error)) *cobra.Command {
2938
return err
3039
}
3140

41+
if overlapLeft {
42+
fromFiles = utils.RemoveOverlap(fromFiles, toFiles)
43+
}
44+
45+
if overlapRight {
46+
toFiles = utils.RemoveOverlap(toFiles, fromFiles)
47+
}
48+
3249
cfg, err := cfgF()
3350
if err != nil {
3451
return err
@@ -85,6 +102,9 @@ func ExplainCmd(cfgF func() (*config.Config, error)) *cobra.Command {
85102
},
86103
}
87104

105+
cmd.Flags().BoolVarP(&overlapLeft, "overlap-left", "ol", false, "When there's an overlap between the files at the left and the right, keep the ones at the left")
106+
cmd.Flags().BoolVarP(&overlapRight, "overlap-right", "or", false, "When there's an overlap between the files at the left and the right, keep the ones at the right")
107+
88108
return cmd
89109
}
90110

internal/utils/remove_overlap.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package utils
2+
3+
// RemoveOverlap removes elements from a that are also on b.
4+
func RemoveOverlap[T comparable](a, b []T) []T {
5+
res := make([]T, 0, len(a))
6+
bSet := SetFromSlice(b)
7+
for _, el := range a {
8+
if _, ok := bSet[el]; !ok {
9+
res = append(res, el)
10+
}
11+
}
12+
13+
return res
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestRemoveOverlap(t *testing.T) {
10+
tests := []struct {
11+
Name string
12+
A []string
13+
B []string
14+
Expected []string
15+
}{
16+
{
17+
Name: "No overlap",
18+
A: []string{"a", "b", "c"},
19+
B: []string{"d", "e", "f"},
20+
Expected: []string{"a", "b", "c"},
21+
},
22+
{
23+
Name: "Partial overlap",
24+
A: []string{"a", "b", "c"},
25+
B: []string{"b", "c", "d"},
26+
Expected: []string{"a"},
27+
},
28+
{
29+
Name: "Full overlap",
30+
A: []string{"a", "b", "c"},
31+
B: []string{"a", "b", "c"},
32+
Expected: []string{},
33+
},
34+
}
35+
36+
for _, tt := range tests {
37+
t.Run(tt.Name, func(t *testing.T) {
38+
assert.Equal(t, tt.Expected, RemoveOverlap(tt.A, tt.B))
39+
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)