Skip to content

Commit fefeb36

Browse files
mvdanrogpeppe
authored andcommitted
cue/load: make Config.Registry an ociregistry.Interface
We want to allow using cue/load with a registry implementation that doesn't have to be exposed via an HTTP server. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I2cc33b7d08d93a06dae561d5debce3d72ba65261 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1169977 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 5f19042 commit fefeb36

File tree

10 files changed

+26
-36
lines changed

10 files changed

+26
-36
lines changed

cue/load/config.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
package load
1616

1717
import (
18-
"fmt"
1918
"io"
20-
"net/url"
2119
"os"
2220
"path/filepath"
2321
"strings"
2422

23+
"cuelabs.dev/go/oci/ociregistry"
2524
"cuelang.org/go/cue/ast"
2625
"cuelang.org/go/cue/build"
2726
"cuelang.org/go/cue/errors"
@@ -276,9 +275,13 @@ type Config struct {
276275
// the corresponding build.File will be associated with the full buffer.
277276
Stdin io.Reader
278277

279-
// Registry holds the URL of the CUE registry. If it has no scheme, https:// is assumed
280-
// as a prefix. THIS IS EXPERIMENTAL FOR NOW. DO NOT USE.
281-
Registry string
278+
// Registry is used to fetch CUE module dependencies.
279+
//
280+
// When nil, dependencies will be resolved in legacy mode:
281+
// reading from cue.mod/pkg, cue.mod/usr, and cue.mod/gen.
282+
//
283+
// THIS IS EXPERIMENTAL FOR NOW. DO NOT USE.
284+
Registry ociregistry.Interface
282285

283286
fileSystem fileSystem
284287
}
@@ -365,17 +368,6 @@ func (c Config) complete() (cfg *Config, err error) {
365368
} else if !filepath.IsAbs(c.ModuleRoot) {
366369
c.ModuleRoot = filepath.Join(c.Dir, c.ModuleRoot)
367370
}
368-
//c.Registry = "registry.cue.works"
369-
if c.Registry != "" {
370-
u, err := url.Parse(c.Registry)
371-
if err != nil {
372-
return nil, fmt.Errorf("invalid registry URL %q: %v", c.Registry, err)
373-
}
374-
if u.Scheme == "" {
375-
u.Scheme = "https"
376-
c.Registry = u.String()
377-
}
378-
}
379371
if err := c.loadModule(); err != nil {
380372
return nil, err
381373
}

cue/load/import.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
389389

390390
default:
391391
// TODO predicate registry-aware lookup on module.cue-declared CUE version?
392-
if l.cfg.Registry != "" {
392+
if l.cfg.Registry != nil {
393393
var err error
394394
absDir, err = l.externalPackageDir(p)
395395
if err != nil {

cue/load/instances.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Instances(args []string, c *Config) []*build.Instance {
5151
// TODO use predictable location
5252
var deps *dependencies
5353
var regClient *registryClient
54-
if c.Registry != "" {
54+
if c.Registry != nil {
5555
// TODO use configured cache directory.
5656
tmpDir, err := os.MkdirTemp("", "cue-load-")
5757
if err != nil {

cue/load/module.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *Config) loadModule() error {
4040
return err
4141
}
4242
parseModFile := modfile.ParseNonStrict
43-
if c.Registry == "" {
43+
if c.Registry == nil {
4444
parseModFile = modfile.ParseLegacy
4545
}
4646
mf, err := parseModFile(data, mod)

cue/load/module_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7+
"cuelabs.dev/go/oci/ociregistry/ociclient"
78
"cuelang.org/go/cue/cuecontext"
89
"cuelang.org/go/cue/errors"
910
"cuelang.org/go/internal/cuetxtar"
@@ -21,7 +22,7 @@ func TestModuleFetch(t *testing.T) {
2122
t.Fatal(err)
2223
}
2324
defer r.Close()
24-
t.LoadConfig.Registry = r.URL()
25+
t.LoadConfig.Registry = ociclient.New(r.URL(), nil)
2526
ctx := cuecontext.New()
2627
insts := t.RawInstances()
2728
if len(insts) != 1 {

cue/load/registry.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path"
99
"path/filepath"
1010

11+
"cuelabs.dev/go/oci/ociregistry"
1112
"cuelang.org/go/internal/mod/modfile"
1213
"cuelang.org/go/internal/mod/modregistry"
1314
"cuelang.org/go/internal/mod/module"
@@ -27,8 +28,8 @@ type registryClient struct {
2728
// in the given cache directory. It assumes that information
2829
// in the registry is immutable, so if it's in the cache, a module
2930
// will not be downloaded again.
30-
func newRegistryClient(registryHost string, cacheDir string) (*registryClient, error) {
31-
client, err := modregistry.NewClient(registryHost, "cue/") // TODO configurable prefix
31+
func newRegistryClient(registry ociregistry.Interface, cacheDir string) (*registryClient, error) {
32+
client, err := modregistry.NewClient(registry, "cue/") // TODO configurable prefix
3233
if err != nil {
3334
return nil, err
3435
}

internal/mod/modregistry/client.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"strings"
2626

2727
"cuelabs.dev/go/oci/ociregistry"
28-
"cuelabs.dev/go/oci/ociregistry/ociclient"
2928
"cuelang.org/go/internal/mod/semver"
3029
digest "github.com/opencontainers/go-digest"
3130
specs "github.com/opencontainers/image-spec/specs-go"
@@ -58,10 +57,9 @@ const (
5857
// TODO pass in an ociregistry.Interface instead of a URL,
5958
// thus allowing a locally defined composition of registries
6059
// rather than always assuming everything is behind a single host.
61-
func NewClient(registryURL string, prefix string) (*Client, error) {
62-
r := ociclient.New(registryURL, nil)
60+
func NewClient(registry ociregistry.Interface, prefix string) (*Client, error) {
6361
return &Client{
64-
registry: r,
62+
registry: registry,
6563
prefix: prefix,
6664
}, nil
6765
}

internal/mod/modregistry/client_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bytes"
1919
"context"
2020
"io"
21-
"net/http/httptest"
2221
"os"
2322
"path"
2423
"testing"
@@ -29,17 +28,13 @@ import (
2928
"golang.org/x/tools/txtar"
3029

3130
"cuelabs.dev/go/oci/ociregistry/ocimem"
32-
"cuelabs.dev/go/oci/ociregistry/ociserver"
3331

3432
"cuelang.org/go/internal/mod/module"
3533
modzip "cuelang.org/go/internal/mod/zip"
3634
)
3735

3836
func newTestClient(t *testing.T) *Client {
39-
srv := httptest.NewServer(ociserver.New(ocimem.New(), nil))
40-
t.Cleanup(srv.Close)
41-
42-
c, err := NewClient(srv.URL, "")
37+
c, err := NewClient(ocimem.New(), "")
4338
qt.Assert(t, qt.IsNil(err))
4439
return c
4540
}

internal/registrytest/registry.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http/httptest"
1111
"strings"
1212

13+
"cuelabs.dev/go/oci/ociregistry/ociclient"
1314
"cuelabs.dev/go/oci/ociregistry/ocimem"
1415
"cuelabs.dev/go/oci/ociregistry/ociserver"
1516
"golang.org/x/tools/txtar"
@@ -23,7 +24,7 @@ import (
2324
)
2425

2526
// New starts a registry instance that serves modules found inside the
26-
// _registry path inside ar. It serves the OCI registry protocol.
27+
// _registry path inside fsys. It serves the OCI registry protocol.
2728
//
2829
// Each module should be inside a directory named path_vers, where
2930
// slashes in path have been replaced with underscores and should
@@ -32,7 +33,7 @@ import (
3233
// The Registry should be closed after use.
3334
func New(fsys fs.FS) (*Registry, error) {
3435
srv := httptest.NewServer(ociserver.New(ocimem.New(), nil))
35-
client, err := modregistry.NewClient(srv.URL, "cue/")
36+
client, err := modregistry.NewClient(ociclient.New(srv.URL, nil), "cue/")
3637
if err != nil {
3738
return nil, fmt.Errorf("cannot make client: %v", err)
3839
}

internal/registrytest/registry_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"testing"
1111

12+
"cuelabs.dev/go/oci/ociregistry"
13+
"cuelabs.dev/go/oci/ociregistry/ociclient"
1214
"golang.org/x/tools/txtar"
1315

1416
"cuelang.org/go/internal/mod/modregistry"
@@ -37,12 +39,12 @@ func TestRegistry(t *testing.T) {
3739
t.Fatal(err)
3840
}
3941
defer r.Close()
40-
runTest(t, r.URL(), string(ar.Comment), ar)
42+
runTest(t, ociclient.New(r.URL(), nil), string(ar.Comment), ar)
4143
})
4244
}
4345
}
4446

45-
func runTest(t *testing.T, registry string, script string, ar *txtar.Archive) {
47+
func runTest(t *testing.T, registry ociregistry.Interface, script string, ar *txtar.Archive) {
4648
ctx := context.Background()
4749
client, err := modregistry.NewClient(registry, "cue/")
4850
if err != nil {

0 commit comments

Comments
 (0)