Skip to content

Commit fd0b303

Browse files
committed
Combine checks and parsing of versioned packages
1 parent 7eff5e1 commit fd0b303

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
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/impl/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"context"
88
"fmt"
99

10-
"go.jetpack.io/devbox/internal/lock"
10+
"go.jetpack.io/devbox/internal/fileutil"
1111
"go.jetpack.io/devbox/internal/searcher"
1212
"go.jetpack.io/devbox/internal/ux"
1313
"go.jetpack.io/devbox/internal/wrapnix"
@@ -27,7 +27,7 @@ func (d *Devbox) Update(ctx context.Context, pkgs ...string) error {
2727
}
2828

2929
for _, pkg := range pkgsToUpdate {
30-
if !lock.IsVersionedPackage(pkg) {
30+
if _, _, versioned := fileutil.ParseVersionedPackage(pkg); !versioned {
3131
fmt.Fprintf(d.writer, "Skipping %s because it is not a versioned package\n", pkg)
3232
continue
3333
}

internal/lock/lockfile.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"go.jetpack.io/devbox/internal/boxcli/featureflag"
1414
"go.jetpack.io/devbox/internal/cuecfg"
15+
"go.jetpack.io/devbox/internal/fileutil"
1516
)
1617

1718
const lockFileVersion = "1"
@@ -72,7 +73,7 @@ func (l *File) Resolve(pkg string) (*Package, error) {
7273
if entry, ok := l.Packages[pkg]; !ok || entry.Resolved == "" {
7374
locked := &Package{}
7475
var err error
75-
if IsVersionedPackage(pkg) {
76+
if _, _, versioned := fileutil.ParseVersionedPackage(pkg); versioned {
7677
locked, err = l.resolver.Resolve(pkg)
7778
if err != nil {
7879
return nil, err
@@ -110,15 +111,11 @@ func (l *File) LegacyNixpkgsPath(pkg string) string {
110111
)
111112
}
112113

113-
func IsVersionedPackage(pkg string) bool {
114-
name, version, found := strings.Cut(pkg, "@")
115-
return found && name != "" && version != ""
116-
}
117-
118114
// This probably belongs in input.go but can't add it there because it will
119115
// create a circular dependency. We could move Input into own package.
120116
func IsLegacyPackage(pkg string) bool {
121-
return !IsVersionedPackage(pkg) &&
117+
_, _, versioned := fileutil.ParseVersionedPackage(pkg)
118+
return !versioned &&
122119
!strings.Contains(pkg, ":") &&
123120
// We don't support absolute paths without "path:" prefix, but adding here
124121
// just inc ase 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)