Skip to content

Commit c737691

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

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

internal/devconfig/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/fatih/color"
13+
1314
"go.jetpack.io/devbox/internal/cuecfg"
1415
"go.jetpack.io/devbox/internal/initrec"
1516
)

internal/devpkg/pkg.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
4+
package devpkg
5+
6+
import (
7+
"strings"
8+
)
9+
10+
// ParseVersionedPackage checks if the given package is a versioned package (`[email protected]`)
11+
// and returns its name and version
12+
func ParseVersionedPackage(pkg string) (string, string, bool) {
13+
name, version, found := strings.Cut(pkg, "@")
14+
return name, version, found && name != "" && version != ""
15+
}

internal/lock/lockfile.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"strings"
1212

1313
"github.com/samber/lo"
14+
1415
"go.jetpack.io/devbox/internal/boxcli/featureflag"
1516
"go.jetpack.io/devbox/internal/cuecfg"
17+
"go.jetpack.io/devbox/internal/devpkg"
1618
)
1719

1820
const lockFileVersion = "1"
@@ -73,7 +75,7 @@ func (l *File) Resolve(pkg string) (*Package, error) {
7375
if entry, ok := l.Packages[pkg]; !ok || entry.Resolved == "" {
7476
locked := &Package{}
7577
var err error
76-
if IsVersionedPackage(pkg) {
78+
if _, _, versioned := devpkg.ParseVersionedPackage(pkg); versioned {
7779
locked, err = l.resolver.Resolve(pkg)
7880
if err != nil {
7981
return nil, err
@@ -122,15 +124,11 @@ func (l *File) LegacyNixpkgsPath(pkg string) string {
122124
)
123125
}
124126

125-
func IsVersionedPackage(pkg string) bool {
126-
name, version, found := strings.Cut(pkg, "@")
127-
return found && name != "" && version != ""
128-
}
129-
130127
// This probably belongs in input.go but can't add it there because it will
131128
// create a circular dependency. We could move Input into own package.
132129
func IsLegacyPackage(pkg string) bool {
133-
return !IsVersionedPackage(pkg) &&
130+
_, _, versioned := devpkg.ParseVersionedPackage(pkg)
131+
return !versioned &&
134132
!strings.Contains(pkg, ":") &&
135133
// We don't support absolute paths without "path:" prefix, but adding here
136134
// 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,9 +9,9 @@ import (
99
"io"
1010
"net/http"
1111
"net/url"
12-
"strings"
1312

1413
"go.jetpack.io/devbox/internal/boxcli/usererr"
14+
"go.jetpack.io/devbox/internal/devpkg"
1515
"go.jetpack.io/devbox/internal/envir"
1616
"go.jetpack.io/devbox/internal/lock"
1717
"go.jetpack.io/devbox/internal/nix"
@@ -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, _ := devpkg.ParseVersionedPackage(pkg)
5656
if version == "" {
5757
return nil, usererr.New("No version specified for %q.", name)
5858
}

0 commit comments

Comments
 (0)