Skip to content

Commit

Permalink
devbox: fix deduping packages and remove lo (#37)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gcurtis committed Aug 30, 2022
1 parent 0619cee commit fa25b9d
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
5 changes: 2 additions & 3 deletions devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"

"github.com/pkg/errors"
"github.com/samber/lo"
"go.jetpack.io/devbox/cuecfg"
"go.jetpack.io/devbox/docker"
"go.jetpack.io/devbox/nix"
Expand Down Expand Up @@ -61,15 +60,15 @@ func (d *Devbox) Add(pkgs ...string) error {
}
// Merge and remove duplicates:
merged := append(d.cfg.Packages, pkgs...)
d.cfg.Packages = lo.FindUniques(merged)
d.cfg.Packages = unique(merged)
return d.saveCfg()
}

// Remove removes Nix packages from the config so that it no longer exists in
// the devbox environment.
func (d *Devbox) Remove(pkgs ...string) error {
// Remove packages from config.
d.cfg.Packages = lo.Without(d.cfg.Packages, pkgs...)
d.cfg.Packages = exclude(d.cfg.Packages, pkgs)
return d.saveCfg()
}

Expand Down
87 changes: 87 additions & 0 deletions devbox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package devbox

import (
"fmt"
"strings"
"testing"

"golang.org/x/exp/slices"
)

func TestUnique(t *testing.T) {
cases := []struct{ in, out []string }{
{
in: []string{"a", "b", "b", "c"},
out: []string{"a", "b", "c"},
},
{
in: []string{},
out: []string{},
},
{
in: []string{"a", "b", "c"},
out: []string{"a", "b", "c"},
},
{
in: []string{"a", "a"},
out: []string{"a"},
},
}

for _, tc := range cases {
t.Run(fmt.Sprintf("{%s}", strings.Join(tc.in, ",")), func(t *testing.T) {
got := unique(tc.in)
if !slices.Equal(got, tc.out) {
t.Errorf("Got slice %v, want %v.", got, tc.out)
}
})
}
}

func TestExclude(t *testing.T) {
cases := []struct{ in, exclude, out []string }{
{
in: []string{},
exclude: []string{},
out: []string{},
},
{
in: []string{},
exclude: []string{"a"},
out: []string{},
},
{
in: []string{"a"},
exclude: []string{"a"},
out: []string{},
},
{
in: []string{"a", "b", "c"},
exclude: []string{"b"},
out: []string{"a", "c"},
},
{
in: []string{"a", "b", "c"},
exclude: []string{"a", "b"},
out: []string{"c"},
},
{
in: []string{"a", "b", "c"},
exclude: []string{"a", "d"},
out: []string{"b", "c"},
},
}

for _, tc := range cases {
name := fmt.Sprintf("{%s}-{%s}",
strings.Join(tc.in, ","),
strings.Join(tc.exclude, ","))

t.Run(name, func(t *testing.T) {
got := exclude(tc.in, tc.exclude)
if !slices.Equal(got, tc.out) {
t.Errorf("Got slice %v, want %v.", got, tc.out)
}
})
}
}
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ require (
github.com/denisbrodbeck/machineid v1.0.1
github.com/imdario/mergo v0.3.13
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.27.0
github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -20,13 +20,15 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/backo-go v1.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I
github.com/cockroachdb/apd/v2 v2.0.1 h1:y1Rh3tEU89D+7Tgbw+lp52T6p/GJLpDmNvr10UWqLTE=
github.com/cockroachdb/apd/v2 v2.0.1/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -21,11 +22,15 @@ github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de h1:D5x39vF5KCwKQaw+OC9ZPiLVHXz3UFw2+psEX+gYcto=
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuHZgRjZUWWuH1DTxCtxbHDOIJsudS8jzY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -34,8 +39,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc h1:gSVONBi2HWMFXCa9jFdYvYk7IwW/mTLxWOF7rXS4LO0=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.27.0 h1:GOyDWxsblvqYobqsmUuMddPa2/mMzkKyojlXol4+LaQ=
github.com/samber/lo v1.27.0/go.mod h1:it33p9UtPMS7z72fP4gw/EIfQB2eI8ke7GR2wc6+Rhg=
github.com/segmentio/analytics-go v3.1.0+incompatible h1:IyiOfUgQFVHvsykKKbdI7ZsH374uv3/DfZUo9+G0Z80=
github.com/segmentio/analytics-go v3.1.0+incompatible/go.mod h1:C7CYBtQWk4vRk2RyLu0qOcbHJ18E3F1HV2C/8JvKN48=
github.com/segmentio/backo-go v1.0.1 h1:68RQccglxZeyURy93ASB/2kc9QudzgIDexJ927N++y4=
Expand All @@ -49,7 +52,6 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -66,6 +68,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
28 changes: 28 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package devbox

func unique(s []string) []string {
deduped := make([]string, 0, len(s))
seen := make(map[string]bool, len(s))
for _, str := range s {
if !seen[str] {
deduped = append(deduped, str)
}
seen[str] = true
}
return deduped
}

func exclude(s []string, elems []string) []string {
excluded := make(map[string]bool, len(elems))
for _, ex := range elems {
excluded[ex] = true
}

filtered := make([]string, 0, len(s))
for _, str := range s {
if !excluded[str] {
filtered = append(filtered, str)
}
}
return filtered
}

0 comments on commit fa25b9d

Please sign in to comment.