Skip to content

Commit fa25b9d

Browse files
authored
devbox: fix deduping packages and remove lo (#37)
devbox: fix deduping packages and remove lo Fix Devbox.Add to deduplicate packages instead of removing the ones with duplicates. Extract the two functions that we were using in lo to remove it as a dependency.
1 parent 0619cee commit fa25b9d

File tree

5 files changed

+127
-8
lines changed

5 files changed

+127
-8
lines changed

devbox.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"path/filepath"
99

1010
"github.com/pkg/errors"
11-
"github.com/samber/lo"
1211
"go.jetpack.io/devbox/cuecfg"
1312
"go.jetpack.io/devbox/docker"
1413
"go.jetpack.io/devbox/nix"
@@ -61,15 +60,15 @@ func (d *Devbox) Add(pkgs ...string) error {
6160
}
6261
// Merge and remove duplicates:
6362
merged := append(d.cfg.Packages, pkgs...)
64-
d.cfg.Packages = lo.FindUniques(merged)
63+
d.cfg.Packages = unique(merged)
6564
return d.saveCfg()
6665
}
6766

6867
// Remove removes Nix packages from the config so that it no longer exists in
6968
// the devbox environment.
7069
func (d *Devbox) Remove(pkgs ...string) error {
7170
// Remove packages from config.
72-
d.cfg.Packages = lo.Without(d.cfg.Packages, pkgs...)
71+
d.cfg.Packages = exclude(d.cfg.Packages, pkgs)
7372
return d.saveCfg()
7473
}
7574

devbox_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package devbox
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"golang.org/x/exp/slices"
9+
)
10+
11+
func TestUnique(t *testing.T) {
12+
cases := []struct{ in, out []string }{
13+
{
14+
in: []string{"a", "b", "b", "c"},
15+
out: []string{"a", "b", "c"},
16+
},
17+
{
18+
in: []string{},
19+
out: []string{},
20+
},
21+
{
22+
in: []string{"a", "b", "c"},
23+
out: []string{"a", "b", "c"},
24+
},
25+
{
26+
in: []string{"a", "a"},
27+
out: []string{"a"},
28+
},
29+
}
30+
31+
for _, tc := range cases {
32+
t.Run(fmt.Sprintf("{%s}", strings.Join(tc.in, ",")), func(t *testing.T) {
33+
got := unique(tc.in)
34+
if !slices.Equal(got, tc.out) {
35+
t.Errorf("Got slice %v, want %v.", got, tc.out)
36+
}
37+
})
38+
}
39+
}
40+
41+
func TestExclude(t *testing.T) {
42+
cases := []struct{ in, exclude, out []string }{
43+
{
44+
in: []string{},
45+
exclude: []string{},
46+
out: []string{},
47+
},
48+
{
49+
in: []string{},
50+
exclude: []string{"a"},
51+
out: []string{},
52+
},
53+
{
54+
in: []string{"a"},
55+
exclude: []string{"a"},
56+
out: []string{},
57+
},
58+
{
59+
in: []string{"a", "b", "c"},
60+
exclude: []string{"b"},
61+
out: []string{"a", "c"},
62+
},
63+
{
64+
in: []string{"a", "b", "c"},
65+
exclude: []string{"a", "b"},
66+
out: []string{"c"},
67+
},
68+
{
69+
in: []string{"a", "b", "c"},
70+
exclude: []string{"a", "d"},
71+
out: []string{"b", "c"},
72+
},
73+
}
74+
75+
for _, tc := range cases {
76+
name := fmt.Sprintf("{%s}-{%s}",
77+
strings.Join(tc.in, ","),
78+
strings.Join(tc.exclude, ","))
79+
80+
t.Run(name, func(t *testing.T) {
81+
got := exclude(tc.in, tc.exclude)
82+
if !slices.Equal(got, tc.out) {
83+
t.Errorf("Got slice %v, want %v.", got, tc.out)
84+
}
85+
})
86+
}
87+
}

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ require (
77
github.com/denisbrodbeck/machineid v1.0.1
88
github.com/imdario/mergo v0.3.13
99
github.com/pkg/errors v0.9.1
10-
github.com/samber/lo v1.27.0
1110
github.com/segmentio/analytics-go v3.1.0+incompatible
1211
github.com/spf13/cobra v1.5.0
1312
github.com/stretchr/testify v1.8.0
13+
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17
1414
gopkg.in/yaml.v3 v3.0.1
1515
)
1616

@@ -20,13 +20,15 @@ require (
2020
github.com/davecgh/go-spew v1.1.1 // indirect
2121
github.com/google/uuid v1.2.0 // indirect
2222
github.com/inconshreveable/mousetrap v1.0.0 // indirect
23+
github.com/kr/text v0.2.0 // indirect
2324
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
25+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
2426
github.com/pmezard/go-difflib v1.0.0 // indirect
2527
github.com/segmentio/backo-go v1.0.1 // indirect
2628
github.com/spf13/pflag v1.0.5 // indirect
2729
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
28-
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
2930
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
3031
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
3132
golang.org/x/text v0.3.7 // indirect
33+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
3234
)

go.sum

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I
66
github.com/cockroachdb/apd/v2 v2.0.1 h1:y1Rh3tEU89D+7Tgbw+lp52T6p/GJLpDmNvr10UWqLTE=
77
github.com/cockroachdb/apd/v2 v2.0.1/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw=
88
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
9+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
910
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1011
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1112
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -21,11 +22,15 @@ github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK
2122
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
2223
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
2324
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
25+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
26+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2427
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
28+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2529
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
2630
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de h1:D5x39vF5KCwKQaw+OC9ZPiLVHXz3UFw2+psEX+gYcto=
2731
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuHZgRjZUWWuH1DTxCtxbHDOIJsudS8jzY=
2832
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
33+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
2934
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3035
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3136
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -34,8 +39,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
3439
github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc h1:gSVONBi2HWMFXCa9jFdYvYk7IwW/mTLxWOF7rXS4LO0=
3540
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
3641
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
37-
github.com/samber/lo v1.27.0 h1:GOyDWxsblvqYobqsmUuMddPa2/mMzkKyojlXol4+LaQ=
38-
github.com/samber/lo v1.27.0/go.mod h1:it33p9UtPMS7z72fP4gw/EIfQB2eI8ke7GR2wc6+Rhg=
3942
github.com/segmentio/analytics-go v3.1.0+incompatible h1:IyiOfUgQFVHvsykKKbdI7ZsH374uv3/DfZUo9+G0Z80=
4043
github.com/segmentio/analytics-go v3.1.0+incompatible/go.mod h1:C7CYBtQWk4vRk2RyLu0qOcbHJ18E3F1HV2C/8JvKN48=
4144
github.com/segmentio/backo-go v1.0.1 h1:68RQccglxZeyURy93ASB/2kc9QudzgIDexJ927N++y4=
@@ -49,7 +52,6 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
4952
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5053
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
5154
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
52-
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
5355
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
5456
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
5557
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -66,6 +68,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
6668
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
6769
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6870
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
71+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6972
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7073
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7174
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

slice.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package devbox
2+
3+
func unique(s []string) []string {
4+
deduped := make([]string, 0, len(s))
5+
seen := make(map[string]bool, len(s))
6+
for _, str := range s {
7+
if !seen[str] {
8+
deduped = append(deduped, str)
9+
}
10+
seen[str] = true
11+
}
12+
return deduped
13+
}
14+
15+
func exclude(s []string, elems []string) []string {
16+
excluded := make(map[string]bool, len(elems))
17+
for _, ex := range elems {
18+
excluded[ex] = true
19+
}
20+
21+
filtered := make([]string, 0, len(s))
22+
for _, str := range s {
23+
if !excluded[str] {
24+
filtered = append(filtered, str)
25+
}
26+
}
27+
return filtered
28+
}

0 commit comments

Comments
 (0)