Skip to content

Commit dd6d815

Browse files
authored
devbox.findPackagesByName should be constructed from Config packages (#1360)
## Summary This PR refactors `devbox.findPackagesByName` to be constructed from `devconfig.Package`. **Motivation** `devbox.Update` calls `findPackageByName` and needs to call `devbox.Add` again with the Platform and ExcludedPlatforms. (see #1370) ## How was it tested? compiles
1 parent 89ef96b commit dd6d815

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

internal/impl/devbox.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -992,22 +992,22 @@ func (d *Devbox) HasDeprecatedPackages() bool {
992992
return false
993993
}
994994

995-
func (d *Devbox) findPackageByName(name string) (string, error) {
996-
results := map[string]bool{}
995+
func (d *Devbox) findPackageByName(name string) (*devpkg.Package, error) {
996+
results := map[*devpkg.Package]bool{}
997997
for _, pkg := range d.configPackages() {
998-
if pkg.String() == name || pkg.CanonicalName() == name {
999-
results[pkg.String()] = true
998+
if pkg.Raw == name || pkg.CanonicalName() == name {
999+
results[pkg] = true
10001000
}
10011001
}
10021002
if len(results) > 1 {
1003-
return "", usererr.New(
1003+
return nil, usererr.New(
10041004
"found multiple packages with name %s: %s. Please specify version",
10051005
name,
10061006
lo.Keys(results),
10071007
)
10081008
}
10091009
if len(results) == 0 {
1010-
return "", usererr.New("no package found with name %s", name)
1010+
return nil, usererr.New("no package found with name %s", name)
10111011
}
10121012
return lo.Keys(results)[0], nil
10131013
}

internal/impl/packages.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ func (d *Devbox) Add(ctx context.Context, pkgsNames ...string) error {
5757
// it. Ignore error (which is either missing or more than one). We search by
5858
// CanonicalName so any legacy or versioned packages will be removed if they
5959
// match.
60-
if name, _ := d.findPackageByName(pkg.CanonicalName()); name != "" {
61-
if err := d.Remove(ctx, name); err != nil {
60+
found, _ := d.findPackageByName(pkg.CanonicalName())
61+
if found != nil {
62+
if err := d.Remove(ctx, found.Raw); err != nil {
6263
return err
6364
}
6465
}
@@ -134,9 +135,9 @@ func (d *Devbox) Remove(ctx context.Context, pkgs ...string) error {
134135
missingPkgs := []string{}
135136
for _, pkg := range lo.Uniq(pkgs) {
136137
found, _ := d.findPackageByName(pkg)
137-
if found != "" {
138-
packagesToUninstall = append(packagesToUninstall, found)
139-
d.cfg.Packages.Remove(found)
138+
if found != nil {
139+
packagesToUninstall = append(packagesToUninstall, found.Raw)
140+
d.cfg.Packages.Remove(found.Raw)
140141
} else {
141142
missingPkgs = append(missingPkgs, pkg)
142143
}

internal/impl/update.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ func (d *Devbox) Update(ctx context.Context, pkgs ...string) error {
6666
}
6767

6868
func (d *Devbox) inputsToUpdate(pkgs ...string) ([]*devpkg.Package, error) {
69-
var pkgsToUpdate []string
69+
if len(pkgs) == 0 {
70+
return d.configPackages(), nil
71+
}
72+
73+
var pkgsToUpdate []*devpkg.Package
7074
for _, pkg := range pkgs {
7175
found, err := d.findPackageByName(pkg)
7276
if err != nil {
7377
return nil, err
7478
}
7579
pkgsToUpdate = append(pkgsToUpdate, found)
7680
}
77-
if len(pkgsToUpdate) == 0 {
78-
pkgsToUpdate = d.PackageNames()
79-
}
80-
81-
return devpkg.PackageFromStrings(pkgsToUpdate, d.lockfile), nil
81+
return pkgsToUpdate, nil
8282
}
8383

8484
func (d *Devbox) updateDevboxPackage(

0 commit comments

Comments
 (0)