Skip to content

Commit afeec38

Browse files
authored
[info] Modernize via search api (#1376)
## Summary The `devbox info` command is quite old and outdated. 1. It returns results inconsistent with the `devbox search` results. It also doesn't print much useful information except a poorly formatted package-name and version. 2. Some users have asked in the past for understanding what a particular package in the search results is for. This PR leverages the search service's API to get the package information, and also prints a summary of the package. ## How was it tested? BEFORE: ``` > devbox info go go-1.20.2 > devbox info [email protected] Ensuring nixpkgs registry is downloaded. Downloaded 'github:NixOS/nixpkgs/1c6eb4876f71e8903ae9f73e6adf45fdbebc0292' to '/nix/store/y3kz7n62fwvp331na0k17xdw4dyaa66d-source' (hash 'sha256-hI7+s1UVDsJNqNn9UGV6xTBGqMC4dqOyVpeDf+su7JU='). Ensuring nixpkgs registry is downloaded: Success go-1.19.2 > devbox info php php-with-extensions-8.1.17 php NOTES: PHP is compiled with default extensions. If you would like to use non-default extensions you can add them with devbox add php81Extensions.{extension} . For example, for the memcache extension you can do `devbox add php81Extensions.memcached`. Error: open /Users/savil/code/jetpack/devbox/.devbox/virtenv/php/process-compose.yaml: no such file or directory ``` AFTER: ``` > devbox info go go 1.20.6 The Go Programming language > devbox info [email protected] go 1.19.2 The Go Programming language > devbox info php php 8.2.8 An HTML-embedded scripting language php NOTES: PHP is compiled with default extensions. If you would like to use non-default extensions you can add them with devbox add php81Extensions.{extension} . For example, for the memcache extension you can do `devbox add php81Extensions.memcached`. Error: open /Users/savil/code/jetpack/devbox/.devbox/virtenv/php/process-compose.yaml: no such file or directory ``` NOTE: the error with `devbox info php` still needs fixing...
1 parent 1c4b8a9 commit afeec38

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

internal/impl/devbox.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/samber/lo"
2222
"go.jetpack.io/devbox/internal/devpkg"
2323
"go.jetpack.io/devbox/internal/impl/generate"
24+
"go.jetpack.io/devbox/internal/searcher"
2425
"go.jetpack.io/devbox/internal/shellgen"
2526
"go.jetpack.io/devbox/internal/telemetry"
2627
"golang.org/x/exp/slices"
@@ -345,24 +346,35 @@ func (d *Devbox) Info(ctx context.Context, pkg string, markdown bool) error {
345346
ctx, task := trace.NewTask(ctx, "devboxInfo")
346347
defer task.End()
347348

348-
locked, err := d.lockfile.Resolve(pkg)
349+
name, version, isVersioned := searcher.ParseVersionedPackage(pkg)
350+
if !isVersioned {
351+
name = pkg
352+
version = "latest"
353+
}
354+
355+
packageVersion, err := searcher.Client().Resolve(name, version)
349356
if err != nil {
350-
return err
357+
if !errors.Is(err, searcher.ErrNotFound) {
358+
return usererr.WithUserMessage(err, "Package %q not found\n", pkg)
359+
}
360+
361+
packageVersion = nil
362+
// fallthrough to below
351363
}
352364

353-
results, _ := nix.Search(locked.Resolved)
354-
if len(results) == 0 {
355-
_, err := fmt.Fprintf(d.writer, "Package %s not found\n", pkg)
365+
if packageVersion == nil {
366+
_, err := fmt.Fprintf(d.writer, "Package %q not found\n", pkg)
356367
return errors.WithStack(err)
357368
}
358369

359370
// we should only have one result
360-
info := lo.Values(results)[0]
361371
if _, err := fmt.Fprintf(
362372
d.writer,
363-
"%s%s\n",
373+
"%s%s %s\n%s\n",
364374
lo.Ternary(markdown, "## ", ""),
365-
info,
375+
packageVersion.Name,
376+
packageVersion.Version,
377+
packageVersion.Summary,
366378
); err != nil {
367379
return errors.WithStack(err)
368380
}

internal/nix/search.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Info struct {
1919
// if we know exactly which version we are using
2020
AttributeKey string
2121
PName string
22+
Summary string
2223
Version string
2324
}
2425

internal/searcher/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616

1717
const searchAPIEndpoint = "https://search.devbox.sh"
1818

19+
var ErrNotFound = errors.New("Not found")
20+
1921
type client struct {
2022
host string
2123
}
@@ -68,6 +70,9 @@ func execGet[T any](url string) (*T, error) {
6870
if err != nil {
6971
return nil, err
7072
}
73+
if response.StatusCode == http.StatusNotFound {
74+
return nil, ErrNotFound
75+
}
7176
var result T
7277
return &result, json.Unmarshal(data, &result)
7378
}

internal/searcher/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ type PackageInfo struct {
3333
MetaVersion []string `json:"meta_version"`
3434
AttrPaths []string `json:"attr_paths"`
3535
Version string `json:"version"`
36+
Summary string `json:"summary"`
3637
}

testscripts/info/info.test.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
exec devbox init
22
exec devbox info hello
3-
stdout hello-.
3+
stdout 'hello '
44

55
exec devbox init
66
exec devbox info hello@latest
7-
stdout hello-.
7+
stdout 'hello '
88

99
exec devbox init
1010
exec devbox info notapackage
11-
stdout 'Package notapackage not found'
11+
stdout 'Package "notapackage" not found'

0 commit comments

Comments
 (0)