Skip to content

Commit df22c88

Browse files
committed
Combine checks and parsing of versioned packages
1 parent a42eacb commit df22c88

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

internal/fileutil/fileutil.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ func FileContains(path string, substring string) (bool, error) {
5555
}
5656
return strings.Contains(string(data), substring), nil
5757
}
58+
59+
// ParseVersionedPackage checks if the given package is a versioned package (`[email protected]`)
60+
// and returns its name and version
61+
func ParseVersionedPackage(pkg string) (string, string, bool) {
62+
name, version, found := strings.Cut(pkg, "@")
63+
return name, version, found && name != "" && version != ""
64+
}

internal/lock/lockfile.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/samber/lo"
1414
"go.jetpack.io/devbox/internal/boxcli/featureflag"
1515
"go.jetpack.io/devbox/internal/cuecfg"
16+
"go.jetpack.io/devbox/internal/fileutil"
1617
)
1718

1819
const lockFileVersion = "1"
@@ -73,7 +74,7 @@ func (l *File) Resolve(pkg string) (*Package, error) {
7374
if entry, ok := l.Packages[pkg]; !ok || entry.Resolved == "" {
7475
locked := &Package{}
7576
var err error
76-
if IsVersionedPackage(pkg) {
77+
if _, _, versioned := fileutil.ParseVersionedPackage(pkg); versioned {
7778
locked, err = l.resolver.Resolve(pkg)
7879
if err != nil {
7980
return nil, err
@@ -122,15 +123,11 @@ func (l *File) LegacyNixpkgsPath(pkg string) string {
122123
)
123124
}
124125

125-
func IsVersionedPackage(pkg string) bool {
126-
name, version, found := strings.Cut(pkg, "@")
127-
return found && name != "" && version != ""
128-
}
129-
130126
// This probably belongs in input.go but can't add it there because it will
131127
// create a circular dependency. We could move Input into own package.
132128
func IsLegacyPackage(pkg string) bool {
133-
return !IsVersionedPackage(pkg) &&
129+
_, _, versioned := fileutil.ParseVersionedPackage(pkg)
130+
return !versioned &&
134131
!strings.Contains(pkg, ":") &&
135132
// We don't support absolute paths without "path:" prefix, but adding here
136133
// just in case we ever do.

internal/searcher/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"io"
1010
"net/http"
1111
"net/url"
12-
"strings"
1312

1413
"go.jetpack.io/devbox/internal/boxcli/usererr"
1514
"go.jetpack.io/devbox/internal/envir"
15+
"go.jetpack.io/devbox/internal/fileutil"
1616
"go.jetpack.io/devbox/internal/lock"
1717
"go.jetpack.io/devbox/internal/nix"
1818
)
@@ -52,7 +52,7 @@ func (c *client) SearchVersion(query, version string) (*SearchResult, error) {
5252
}
5353

5454
func (c *client) Resolve(pkg string) (*lock.Package, error) {
55-
name, version, _ := strings.Cut(pkg, "@")
55+
name, version, _ := fileutil.ParseVersionedPackage(pkg)
5656
if version == "" {
5757
return nil, usererr.New("No version specified for %q.", name)
5858
}

0 commit comments

Comments
 (0)