|
| 1 | +// Copyright 2023 The CUE Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + |
| 24 | + "cuelang.org/go/internal/mod/modfile" |
| 25 | + "cuelang.org/go/internal/mod/modregistry" |
| 26 | + "cuelang.org/go/internal/mod/module" |
| 27 | + "cuelang.org/go/internal/mod/modzip" |
| 28 | +) |
| 29 | + |
| 30 | +func newModUploadCmd(c *Command) *cobra.Command { |
| 31 | + cmd := &cobra.Command{ |
| 32 | + // TODO: this command is still experimental, don't show it in |
| 33 | + // the documentation just yet. |
| 34 | + Hidden: true, |
| 35 | + |
| 36 | + Use: "upload <version>", |
| 37 | + Short: "upload the current module to a registry", |
| 38 | + Long: `WARNING: THIS COMMAND IS EXPERIMENTAL. |
| 39 | +
|
| 40 | +Upload the current module to an OCI registry. |
| 41 | +Currently this command must be run in the module's root directory. |
| 42 | +Also note that this command does no dependency or other checks at the moment. |
| 43 | +`, |
| 44 | + RunE: mkRunE(c, runModUpload), |
| 45 | + Args: cobra.ExactArgs(1), |
| 46 | + } |
| 47 | + |
| 48 | + return cmd |
| 49 | +} |
| 50 | + |
| 51 | +func runModUpload(cmd *Command, args []string) error { |
| 52 | + reg, err := getRegistry() |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + if reg == nil { |
| 57 | + return fmt.Errorf("no registry configured to upload to") |
| 58 | + } |
| 59 | + modfileData, err := os.ReadFile("cue.mod/module.cue") |
| 60 | + if err != nil { |
| 61 | + if os.IsNotExist(err) { |
| 62 | + return fmt.Errorf("no cue.mod/module.cue file found; cue mod upload must be run in the module's root directory") |
| 63 | + } |
| 64 | + return err |
| 65 | + } |
| 66 | + mf, err := modfile.Parse(modfileData, "cue.mod/module.cue") |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + mv, err := module.NewVersion(mf.Module, args[0]) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("cannot form module version: %v", err) |
| 73 | + } |
| 74 | + zf, err := os.CreateTemp("", "cue-upload-") |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + defer os.Remove(zf.Name()) |
| 79 | + defer zf.Close() |
| 80 | + |
| 81 | + // TODO verify that all dependencies exist in the registry. |
| 82 | + if err := modzip.CreateFromDir(zf, mv, "."); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + info, err := zf.Stat() |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + rclient := modregistry.NewClient(reg) |
| 91 | + if err := rclient.PutModule(context.Background(), mv, zf, info.Size()); err != nil { |
| 92 | + return fmt.Errorf("cannot put module: %v", err) |
| 93 | + } |
| 94 | + fmt.Printf("uploaded %s\n", mv) |
| 95 | + return nil |
| 96 | +} |
0 commit comments