-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This wires up the `modfile.FixLegacy` function to a new `cue mod fix` command. Fixes #3137 Signed-off-by: Roger Peppe <[email protected]> Change-Id: Idbb071493c4596feabf3b2c6c9ee8a53847d6127 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194780 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 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,80 @@ | ||
// Copyright 2024 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 ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"cuelang.org/go/mod/modfile" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newModFixCmd(c *Command) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "fix", | ||
Short: "fix a legacy cue.mod/module.cue file", | ||
Long: `WARNING: THIS COMMAND IS EXPERIMENTAL. | ||
Fix provides a way to migrate from a legacy module.cue file | ||
to the new standard syntax. It | ||
- adds a language.version field | ||
- moves unrecognized fields into the custom.legacy field | ||
- adds a major version to the module path | ||
If there is no module path, it chooses an arbitrary path (test.example@v0). | ||
If the module.cue file is already compatible with the new syntax, | ||
it is just formatted without making any other changes. | ||
Note: you must enable the modules experiment with: | ||
export CUE_EXPERIMENT=modules | ||
for this command to work. | ||
`, | ||
RunE: mkRunE(c, runModFix), | ||
Args: cobra.ExactArgs(0), | ||
} | ||
return cmd | ||
} | ||
|
||
func runModFix(cmd *Command, args []string) error { | ||
modRoot, err := findModuleRoot() | ||
if err != nil { | ||
return err | ||
} | ||
modPath := filepath.Join(modRoot, "cue.mod", "module.cue") | ||
data, err := os.ReadFile(modPath) | ||
if err != nil { | ||
return err | ||
} | ||
mf, err := modfile.FixLegacy(data, modPath) | ||
if err != nil { | ||
return err | ||
} | ||
newData, err := mf.Format() | ||
if err != nil { | ||
return fmt.Errorf("internal error: invalid module.cue file generated: %v", err) | ||
} | ||
if bytes.Equal(newData, data) { | ||
return nil | ||
} | ||
if err := os.WriteFile(modPath, newData, 0o666); err != nil { | ||
return err | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This test checks that the legacy module.cue fixing code | ||
# is wired up correctly. More comprehensive tests for | ||
# this functionality are inside the modfile package. | ||
|
||
exec cue mod fix | ||
cmp cue.mod/module.cue want-module | ||
|
||
exec cue mod fix | ||
cmp cue.mod/module.cue want-module | ||
|
||
-- cue.mod/module.cue -- | ||
module: "foo.com" | ||
foo: "bar" | ||
-- want-module -- | ||
module: "foo.com@v0" | ||
language: { | ||
version: "v0.9.0" | ||
} | ||
custom: { | ||
legacy: { | ||
foo: "bar" | ||
} | ||
} |