-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[search] Update search endpoints (#1249)
## Summary This updates search endpoints to use new `v1` endpoints. It's a fairly small change, but required modernizing the format we use to unmarshal results. I took advantage to make a few low risk refactoring changes: * Removed lock dependency from search. This allows lock to have an actual searcher instance and not an interface. * Removed `devpkgutil` package and incorporated to searcher. * Improved search command to truncate results/versions to improve ui/ux. Added flag to show all commands. * Improved search command by allowing version to be specified to get a preview of what it will resolve to. This can be improved by doing server side filtering and using search instead of resolve. * Fixed `devbox update` bug where updating was causing lock files entries to get replaced even if version had not changed. (This causes nixpkgs to change which slows stuff down) ## How was it tested? * Ran `devbox update` * Tested `devbox add [email protected]` * Tested `devbox search` with many values, with and without the `--show-all` flag <img width="738" alt="image" src="https://github.com/jetpack-io/devbox/assets/544948/033f0720-ede2-45ad-b36e-eac4d9e2895c">
- Loading branch information
1 parent
8b6be0b
commit 0a280d1
Showing
15 changed files
with
243 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,9 @@ | |
"version": "1.6.23" | ||
}, | ||
"[email protected]": { | ||
"last_modified": "2023-05-01T16:53:22Z", | ||
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go", | ||
"version": "1.20.3" | ||
"last_modified": "2023-05-25T03:54:59Z", | ||
"resolved": "github:NixOS/nixpkgs/8d4d822bc0efa9de6eddc79cb0d82897a9baa750#go", | ||
"version": "1.20.4" | ||
}, | ||
"[email protected]": { | ||
"last_modified": "2023-05-01T16:53:22Z", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package lock | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"go.jetpack.io/devbox/internal/boxcli/featureflag" | ||
"go.jetpack.io/devbox/internal/boxcli/usererr" | ||
"go.jetpack.io/devbox/internal/nix" | ||
"go.jetpack.io/devbox/internal/searcher" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
// FetchResolvedPackage fetches a resolution but does not write it to the lock | ||
// struct. This allows testing new versions of packages without writing to the | ||
// lock. This is useful to avoid changing nixpkgs commit hashes when version has | ||
// not changed. This can happen when doing `devbox update` and search has | ||
// a newer hash than the lock file but same version. In that case we don't want | ||
// to update because it would be slow and wasteful. | ||
func (l *File) FetchResolvedPackage(pkg string) (*Package, error) { | ||
name, version, _ := searcher.ParseVersionedPackage(pkg) | ||
if version == "" { | ||
return nil, usererr.New("No version specified for %q.", name) | ||
} | ||
|
||
packageVersion, err := searcher.Client().Resolve(name, version) | ||
if err != nil { | ||
return nil, errors.Wrapf(nix.ErrPackageNotFound, "%s@%s", name, version) | ||
} | ||
|
||
sysInfos := map[string]*SystemInfo{} | ||
if featureflag.RemoveNixpkgs.Enabled() { | ||
sysInfos = buildLockSystemInfos(packageVersion) | ||
} | ||
packageInfo, err := selectForSystem(packageVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(packageInfo.AttrPaths) == 0 { | ||
return nil, fmt.Errorf("no attr paths found for package %q", name) | ||
} | ||
|
||
return &Package{ | ||
LastModified: time.Unix(int64(packageInfo.LastUpdated), 0).UTC(). | ||
Format(time.RFC3339), | ||
Resolved: fmt.Sprintf( | ||
"github:NixOS/nixpkgs/%s#%s", | ||
packageInfo.CommitHash, | ||
packageInfo.AttrPaths[0], | ||
), | ||
Version: packageInfo.Version, | ||
Systems: sysInfos, | ||
}, nil | ||
} | ||
|
||
func selectForSystem(pkg *searcher.PackageVersion) (searcher.PackageInfo, error) { | ||
currentSystem, err := nix.System() | ||
if err != nil { | ||
return searcher.PackageInfo{}, err | ||
} | ||
if pi, ok := pkg.Systems[currentSystem]; ok { | ||
return pi, nil | ||
} | ||
if pi, ok := pkg.Systems["x86_64-linux"]; ok { | ||
return pi, nil | ||
} | ||
if len(pkg.Systems) == 0 { | ||
return searcher.PackageInfo{}, | ||
fmt.Errorf("no systems found for package %q", pkg.Name) | ||
} | ||
return maps.Values(pkg.Systems)[0], nil | ||
} | ||
|
||
func buildLockSystemInfos(pkg *searcher.PackageVersion) map[string]*SystemInfo { | ||
sysInfos := map[string]*SystemInfo{} | ||
for sysName, sysInfo := range pkg.Systems { | ||
sysInfos[sysName] = &SystemInfo{ | ||
System: sysName, | ||
FromHash: sysInfo.StoreHash, | ||
StoreName: sysInfo.StoreName, | ||
StoreVersion: sysInfo.StoreVersion, | ||
} | ||
} | ||
return sysInfos | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.