Skip to content

Combine checks and parsing of versioned packages #1116

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 1 commit into from
Jun 14, 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
1 change: 1 addition & 0 deletions internal/devconfig/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/fatih/color"

"go.jetpack.io/devbox/internal/cuecfg"
"go.jetpack.io/devbox/internal/initrec"
)
Expand Down
15 changes: 15 additions & 0 deletions internal/devpkg/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 devpkg

import (
"strings"
)

// ParseVersionedPackage checks if the given package is a versioned package (`[email protected]`)
// and returns its name and version
func ParseVersionedPackage(pkg string) (string, string, bool) {
name, version, found := strings.Cut(pkg, "@")
return name, version, found && name != "" && version != ""
}
12 changes: 5 additions & 7 deletions internal/lock/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"strings"

"github.com/samber/lo"

"go.jetpack.io/devbox/internal/boxcli/featureflag"
"go.jetpack.io/devbox/internal/cuecfg"
"go.jetpack.io/devbox/internal/devpkg"
)

const lockFileVersion = "1"
Expand Down Expand Up @@ -73,7 +75,7 @@ func (l *File) Resolve(pkg string) (*Package, error) {
if entry, ok := l.Packages[pkg]; !ok || entry.Resolved == "" {
locked := &Package{}
var err error
if IsVersionedPackage(pkg) {
if _, _, versioned := devpkg.ParseVersionedPackage(pkg); versioned {
locked, err = l.resolver.Resolve(pkg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -122,15 +124,11 @@ func (l *File) LegacyNixpkgsPath(pkg string) string {
)
}

func IsVersionedPackage(pkg string) bool {
name, version, found := strings.Cut(pkg, "@")
return found && name != "" && version != ""
}

// This probably belongs in input.go but can't add it there because it will
// create a circular dependency. We could move Input into own package.
func IsLegacyPackage(pkg string) bool {
return !IsVersionedPackage(pkg) &&
_, _, versioned := devpkg.ParseVersionedPackage(pkg)
return !versioned &&
!strings.Contains(pkg, ":") &&
// We don't support absolute paths without "path:" prefix, but adding here
// just in case we ever do.
Expand Down
4 changes: 2 additions & 2 deletions internal/searcher/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"io"
"net/http"
"net/url"
"strings"

"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/devpkg"
"go.jetpack.io/devbox/internal/envir"
"go.jetpack.io/devbox/internal/lock"
"go.jetpack.io/devbox/internal/nix"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (c *client) SearchVersion(query, version string) (*SearchResult, error) {
}

func (c *client) Resolve(pkg string) (*lock.Package, error) {
name, version, _ := strings.Cut(pkg, "@")
name, version, _ := devpkg.ParseVersionedPackage(pkg)
if version == "" {
return nil, usererr.New("No version specified for %q.", name)
}
Expand Down