this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: add mod fix command

This wires up the `modfile.FixLegacy` function to a new
`cue mod fix` command.

Fixes #3137

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Idbb071493c4596feabf3b2c6c9ee8a53847d6127
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194780
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+104
+1
cmd/cue/cmd/mod.go
··· 45 45 } 46 46 47 47 cmd.AddCommand(newModEditCmd(c)) 48 + cmd.AddCommand(newModFixCmd(c)) 48 49 cmd.AddCommand(newModGetCmd(c)) 49 50 cmd.AddCommand(newModInitCmd(c)) 50 51 cmd.AddCommand(newModRegistryCmd(c))
+80
cmd/cue/cmd/modfix.go
··· 1 + // Copyright 2024 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 + "bytes" 19 + "fmt" 20 + "os" 21 + "path/filepath" 22 + 23 + "cuelang.org/go/mod/modfile" 24 + "github.com/spf13/cobra" 25 + ) 26 + 27 + func newModFixCmd(c *Command) *cobra.Command { 28 + cmd := &cobra.Command{ 29 + Use: "fix", 30 + Short: "fix a legacy cue.mod/module.cue file", 31 + Long: `WARNING: THIS COMMAND IS EXPERIMENTAL. 32 + 33 + Fix provides a way to migrate from a legacy module.cue file 34 + to the new standard syntax. It 35 + 36 + - adds a language.version field 37 + - moves unrecognized fields into the custom.legacy field 38 + - adds a major version to the module path 39 + 40 + If there is no module path, it chooses an arbitrary path (test.example@v0). 41 + 42 + If the module.cue file is already compatible with the new syntax, 43 + it is just formatted without making any other changes. 44 + 45 + Note: you must enable the modules experiment with: 46 + export CUE_EXPERIMENT=modules 47 + for this command to work. 48 + `, 49 + RunE: mkRunE(c, runModFix), 50 + Args: cobra.ExactArgs(0), 51 + } 52 + return cmd 53 + } 54 + 55 + func runModFix(cmd *Command, args []string) error { 56 + modRoot, err := findModuleRoot() 57 + if err != nil { 58 + return err 59 + } 60 + modPath := filepath.Join(modRoot, "cue.mod", "module.cue") 61 + data, err := os.ReadFile(modPath) 62 + if err != nil { 63 + return err 64 + } 65 + mf, err := modfile.FixLegacy(data, modPath) 66 + if err != nil { 67 + return err 68 + } 69 + newData, err := mf.Format() 70 + if err != nil { 71 + return fmt.Errorf("internal error: invalid module.cue file generated: %v", err) 72 + } 73 + if bytes.Equal(newData, data) { 74 + return nil 75 + } 76 + if err := os.WriteFile(modPath, newData, 0o666); err != nil { 77 + return err 78 + } 79 + return nil 80 + }
+23
cmd/cue/cmd/testdata/script/modfix_initial.txtar
··· 1 + # This test checks that the legacy module.cue fixing code 2 + # is wired up correctly. More comprehensive tests for 3 + # this functionality are inside the modfile package. 4 + 5 + exec cue mod fix 6 + cmp cue.mod/module.cue want-module 7 + 8 + exec cue mod fix 9 + cmp cue.mod/module.cue want-module 10 + 11 + -- cue.mod/module.cue -- 12 + module: "foo.com" 13 + foo: "bar" 14 + -- want-module -- 15 + module: "foo.com@v0" 16 + language: { 17 + version: "v0.9.0" 18 + } 19 + custom: { 20 + legacy: { 21 + foo: "bar" 22 + } 23 + }