Skip to content

[info] Modernize via search api #1376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/samber/lo"
"go.jetpack.io/devbox/internal/devpkg"
"go.jetpack.io/devbox/internal/impl/generate"
"go.jetpack.io/devbox/internal/searcher"
"go.jetpack.io/devbox/internal/shellgen"
"go.jetpack.io/devbox/internal/telemetry"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -340,24 +341,35 @@ func (d *Devbox) Info(ctx context.Context, pkg string, markdown bool) error {
ctx, task := trace.NewTask(ctx, "devboxInfo")
defer task.End()

locked, err := d.lockfile.Resolve(pkg)
name, version, isVersioned := searcher.ParseVersionedPackage(pkg)
if !isVersioned {
name = pkg
version = "latest"
}

packageVersion, err := searcher.Client().Resolve(name, version)
if err != nil {
return err
if !errors.Is(err, searcher.ErrNotFound) {
return usererr.WithUserMessage(err, "Package %q not found\n", pkg)
}

packageVersion = nil
// fallthrough to below
}

results, _ := nix.Search(locked.Resolved)
if len(results) == 0 {
_, err := fmt.Fprintf(d.writer, "Package %s not found\n", pkg)
if packageVersion == nil {
_, err := fmt.Fprintf(d.writer, "Package %q not found\n", pkg)
return errors.WithStack(err)
}

// we should only have one result
info := lo.Values(results)[0]
if _, err := fmt.Fprintf(
d.writer,
"%s%s\n",
"%s%s %s\n%s\n",
lo.Ternary(markdown, "## ", ""),
info,
packageVersion.Name,
packageVersion.Version,
packageVersion.Summary,
); err != nil {
return errors.WithStack(err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/nix/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Info struct {
// if we know exactly which version we are using
AttributeKey string
PName string
Summary string
Version string
}

Expand Down
5 changes: 5 additions & 0 deletions internal/searcher/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

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

var ErrNotFound = errors.New("Not found")

type client struct {
host string
}
Expand Down Expand Up @@ -68,6 +70,9 @@ func execGet[T any](url string) (*T, error) {
if err != nil {
return nil, err
}
if response.StatusCode == http.StatusNotFound {
return nil, ErrNotFound
}
var result T
return &result, json.Unmarshal(data, &result)
}
1 change: 1 addition & 0 deletions internal/searcher/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ type PackageInfo struct {
MetaVersion []string `json:"meta_version"`
AttrPaths []string `json:"attr_paths"`
Version string `json:"version"`
Summary string `json:"summary"`
}
6 changes: 3 additions & 3 deletions testscripts/info/info.test.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
exec devbox init
exec devbox info hello
stdout hello-.
stdout 'hello '

exec devbox init
exec devbox info hello@latest
stdout hello-.
stdout 'hello '

exec devbox init
exec devbox info notapackage
stdout 'Package notapackage not found'
stdout 'Package "notapackage" not found'