Skip to content

Commit d868a24

Browse files
authored
add --overlap-left and --overlap-right flags (#115)
* add -ol and -or flags * change to -l and -r short flags * Fix overlap order * lint
1 parent 6e5e98e commit d868a24

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

README.md

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

147147
### Explain
148148

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

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

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

174192
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 (-l) or --overlap-right (-r) 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+
toFiles = utils.RemoveOverlap(toFiles, fromFiles)
43+
}
44+
45+
if overlapRight {
46+
fromFiles = utils.RemoveOverlap(fromFiles, toFiles)
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", "l", 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", "r", 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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)