-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds an experimental (and perhaps temporary) command that can be used to upload a module to an OCI registry. To repeat: this command is EXPERIMENTAL; usage and name will change in the future. Signed-off-by: Roger Peppe <rogpeppe@gmail.com> Change-Id: I5aa976b528a1d7c9ae50335adb15921cdf8e5465 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1171801 TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
- Loading branch information
Showing
6 changed files
with
321 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2023 The CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"cuelang.org/go/internal/mod/modfile" | ||
"cuelang.org/go/internal/mod/modregistry" | ||
"cuelang.org/go/internal/mod/module" | ||
"cuelang.org/go/internal/mod/modzip" | ||
) | ||
|
||
func newModUploadCmd(c *Command) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
// TODO: this command is still experimental, don't show it in | ||
// the documentation just yet. | ||
Hidden: true, | ||
|
||
Use: "upload <version>", | ||
Short: "upload the current module to a registry", | ||
Long: `WARNING: THIS COMMAND IS EXPERIMENTAL. | ||
Upload the current module to an OCI registry. | ||
Currently this command must be run in the module's root directory. | ||
Also note that this command does no dependency or other checks at the moment. | ||
`, | ||
RunE: mkRunE(c, runModUpload), | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runModUpload(cmd *Command, args []string) error { | ||
reg, err := getRegistry() | ||
if err != nil { | ||
return err | ||
} | ||
if reg == nil { | ||
return fmt.Errorf("no registry configured to upload to") | ||
} | ||
modfileData, err := os.ReadFile("cue.mod/module.cue") | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return fmt.Errorf("no cue.mod/module.cue file found; cue mod upload must be run in the module's root directory") | ||
} | ||
return err | ||
} | ||
mf, err := modfile.Parse(modfileData, "cue.mod/module.cue") | ||
if err != nil { | ||
return err | ||
} | ||
mv, err := module.NewVersion(mf.Module, args[0]) | ||
if err != nil { | ||
return fmt.Errorf("cannot form module version: %v", err) | ||
} | ||
zf, err := os.CreateTemp("", "cue-upload-") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.Remove(zf.Name()) | ||
defer zf.Close() | ||
|
||
// TODO verify that all dependencies exist in the registry. | ||
if err := modzip.CreateFromDir(zf, mv, "."); err != nil { | ||
return err | ||
} | ||
info, err := zf.Stat() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rclient := modregistry.NewClient(reg) | ||
if err := rclient.PutModule(context.Background(), mv, zf, info.Size()); err != nil { | ||
return fmt.Errorf("cannot put module: %v", err) | ||
} | ||
fmt.Printf("uploaded %s\n", mv) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Check that we can use the cue mod upload command to upload to a registry. | ||
memregistry MEMREGISTRY | ||
env ORIG_CUE_REGISTRY=$CUE_REGISTRY | ||
env CUE_REGISTRY=example.com=$MEMREGISTRY+insecure,$CUE_REGISTRY | ||
cd example | ||
exec cue mod upload v0.0.1 | ||
cmp stdout ../expect-upload-stdout | ||
cd ../main | ||
exec cue eval . | ||
cmp stdout ../expect-eval-stdout | ||
|
||
# Sanity check that the module isn't present in the fallback registry. | ||
env CUE_REGISTRY=$ORIG_CUE_REGISTRY | ||
! exec cue eval | ||
stderr 'repository name not known to registry' | ||
|
||
-- expect-upload-stdout -- | ||
uploaded example.com@v0.0.1 | ||
-- expect-eval-stdout -- | ||
main: "main" | ||
"foo.com/bar/hello@v0": "v0.2.3" | ||
"bar.com@v0": "v0.5.0" | ||
"baz.org@v0": "v0.10.1" | ||
"example.com@v0": "v0.0.1" | ||
-- main/cue.mod/module.cue -- | ||
module: "main.org" | ||
|
||
deps: "example.com@v0": v: "v0.0.1" | ||
|
||
-- main/main.cue -- | ||
package main | ||
import "example.com@v0:main" | ||
|
||
main | ||
|
||
-- example/cue.mod/module.cue -- | ||
module: "example.com@v0" | ||
deps: { | ||
"foo.com/bar/hello@v0": v: "v0.2.3" | ||
"bar.com@v0": v: "v0.5.0" | ||
} | ||
|
||
-- example/top.cue -- | ||
package main | ||
|
||
// Note: import without a major version takes | ||
// the major version from the module.cue file. | ||
import a "foo.com/bar/hello" | ||
a | ||
main: "main" | ||
"example.com@v0": "v0.0.1" | ||
|
||
-- _registry/foo.com_bar_hello_v0.2.3/cue.mod/module.cue -- | ||
module: "foo.com/bar/hello@v0" | ||
deps: { | ||
"bar.com@v0": v: "v0.0.2" | ||
"baz.org@v0": v: "v0.10.1" | ||
} | ||
|
||
-- _registry/foo.com_bar_hello_v0.2.3/x.cue -- | ||
package hello | ||
import ( | ||
a "bar.com/bar@v0" | ||
b "baz.org@v0:baz" | ||
) | ||
"foo.com/bar/hello@v0": "v0.2.3" | ||
a | ||
b | ||
|
||
|
||
-- _registry/bar.com_v0.0.2/cue.mod/module.cue -- | ||
module: "bar.com@v0" | ||
deps: "baz.org@v0": v: "v0.0.2" | ||
|
||
-- _registry/bar.com_v0.0.2/bar/x.cue -- | ||
package bar | ||
import a "baz.org@v0:baz" | ||
"bar.com@v0": "v0.0.2" | ||
a | ||
|
||
|
||
-- _registry/bar.com_v0.5.0/cue.mod/module.cue -- | ||
module: "bar.com@v0" | ||
deps: "baz.org@v0": v: "v0.5.0" | ||
|
||
-- _registry/bar.com_v0.5.0/bar/x.cue -- | ||
package bar | ||
import a "baz.org@v0:baz" | ||
"bar.com@v0": "v0.5.0" | ||
a | ||
|
||
|
||
-- _registry/baz.org_v0.0.2/cue.mod/module.cue -- | ||
module: "baz.org@v0" | ||
|
||
-- _registry/baz.org_v0.0.2/baz.cue -- | ||
package baz | ||
"baz.org@v0": "v0.0.2" | ||
|
||
|
||
-- _registry/baz.org_v0.1.2/cue.mod/module.cue -- | ||
module: "baz.org@v0" | ||
|
||
-- _registry/baz.org_v0.1.2/baz.cue -- | ||
package baz | ||
"baz.org@v0": "v0.1.2" | ||
|
||
|
||
-- _registry/baz.org_v0.5.0/cue.mod/module.cue -- | ||
module: "baz.org@v0" | ||
|
||
-- _registry/baz.org_v0.5.0/baz.cue -- | ||
package baz | ||
"baz.org@v0": "v0.5.0" | ||
|
||
|
||
-- _registry/baz.org_v0.10.1/cue.mod/module.cue -- | ||
module: "baz.org@v0" | ||
|
||
-- _registry/baz.org_v0.10.1/baz.cue -- | ||
package baz | ||
"baz.org@v0": "v0.10.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Check that we can use the cue mod upload command to upload to a registry | ||
# that's protected by authorization. | ||
|
||
memregistry -auth=foo:bar MEMREGISTRY | ||
env CUE_EXPERIMENT=modules | ||
env CUE_REGISTRY=$MEMREGISTRY+insecure | ||
env DOCKER_CONFIG=$WORK/dockerconfig | ||
env-fill $DOCKER_CONFIG/config.json | ||
|
||
cd example | ||
exec cue mod upload v0.0.1 | ||
cmp stdout ../expect-upload-stdout | ||
cd ../main | ||
exec cue eval . | ||
cmp stdout ../expect-eval-stdout | ||
|
||
-- dockerconfig/config.json -- | ||
{ | ||
"auths": { | ||
"${MEMREGISTRY}": { | ||
"username": "foo", | ||
"password": "bar" | ||
} | ||
} | ||
} | ||
|
||
-- expect-upload-stdout -- | ||
uploaded example.com@v0.0.1 | ||
-- expect-eval-stdout -- | ||
main: "main" | ||
"example.com@v0": "v0.0.1" | ||
-- main/cue.mod/module.cue -- | ||
module: "main.org" | ||
deps: "example.com@v0": v: "v0.0.1" | ||
|
||
-- main/main.cue -- | ||
package main | ||
import "example.com@v0:main" | ||
|
||
main | ||
"main": "main" | ||
|
||
-- example/cue.mod/module.cue -- | ||
module: "example.com@v0" | ||
|
||
-- example/top.cue -- | ||
package main | ||
|
||
"example.com@v0": "v0.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters