Skip to content

Commit

Permalink
cmd/cue: add mod edit command
Browse files Browse the repository at this point in the history
This command allows some automatic editing of the module.cue file.
The design is patterned after the `go mod edit` command.

For #3017.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ia46be7fa6cdfd84aff131f2196e052268cdc3b03
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193116
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Apr 18, 2024
1 parent 0616149 commit b7c1a0d
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cue/cmd/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ See also:
}),
}

cmd.AddCommand(newModEditCmd(c))
cmd.AddCommand(newModGetCmd(c))
cmd.AddCommand(newModInitCmd(c))
cmd.AddCommand(newModRegistryCmd(c))
Expand Down
210 changes: 210 additions & 0 deletions cmd/cue/cmd/modedit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// 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"
"strconv"

"cuelang.org/go/mod/modfile"
"cuelang.org/go/mod/module"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func newModEditCmd(c *Command) *cobra.Command {
editCmd := &modEditCmd{}
cmd := &cobra.Command{
Use: "edit",
Short: "edit cue.mod/module.cue",
Long: `WARNING: THIS COMMAND IS EXPERIMENTAL.
Edit provides a command-line interface for editing cue.mod/module.cue.
It reads only that file; it does not look up information about the modules
involved.
The editing flags specify a sequence of editing operations.
The -require=path@version and -drop-require=path@majorversion flags add
and drop a requirement on the given module path and version. Note that
-require overrides any existing requirements on path. These flags are
mainly for tools that understand the module graph. Users should prefer
'cue get path@version' which makes other go.mod adjustments as needed
to satisfy constraints imposed by other modules.
The --module flag changes the module's path (the module.cue file's module field).
The --source flag changes the module's declared source.
The --drop-source flag removes the source field.
Note: you must enable the modules experiment with:
export CUE_EXPERIMENT=modules
for this command to work.
`,
RunE: mkRunE(c, editCmd.run),
Args: cobra.ExactArgs(0),
}
addFlagVar(cmd, flagFunc(editCmd.flagSource), "source", "set the source field")
addFlagVar(cmd, boolFlagFunc(editCmd.flagDropSource), "drop-source", "remove the source field")
addFlagVar(cmd, flagFunc(editCmd.flagModule), "module", "set the module path")
addFlagVar(cmd, flagFunc(editCmd.flagRequire), "require", "add a required module@version")
addFlagVar(cmd, flagFunc(editCmd.flagDropRequire), "drop-require", "remove a requirement")

return cmd
}

type modEditCmd struct {
edits []func(*modfile.File) error
}

func (c *modEditCmd) run(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.Parse(data, modPath)
if err != nil {
return err
}
for _, edit := range c.edits {
if err := edit(mf); 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
}

func (c *modEditCmd) addEdit(f func(*modfile.File) error) {
c.edits = append(c.edits, f)
}

func (c *modEditCmd) flagSource(arg string) error {
if arg != "git" && arg != "none" {
return fmt.Errorf("unrecognized source kind %q", arg)
}
c.addEdit(func(f *modfile.File) error {
if f.Source == nil {
f.Source = &modfile.Source{}
}
f.Source.Kind = arg
return nil
})
return nil
}

func (c *modEditCmd) flagDropSource(arg bool) error {
if !arg {
return fmt.Errorf("cannot set --drop-source to false")
}
c.addEdit(func(f *modfile.File) error {
f.Source = nil
return nil
})
return nil
}

func (c *modEditCmd) flagModule(arg string) error {
if err := module.CheckPath(arg); err != nil {
return err
}
c.addEdit(func(f *modfile.File) error {
f.Module = arg
return nil
})
return nil
}

func (c *modEditCmd) flagRequire(arg string) error {
v, err := module.ParseVersion(arg)
if err != nil {
return err
}
c.addEdit(func(f *modfile.File) error {
if f.Deps == nil {
f.Deps = make(map[string]*modfile.Dep)
}
vm := v.Path()
dep := f.Deps[vm]
if dep == nil {
dep = &modfile.Dep{}
f.Deps[vm] = dep
}
dep.Version = v.Version()
return nil
})
return nil
}

func (c *modEditCmd) flagDropRequire(arg string) error {
if err := module.CheckPath(arg); err != nil {
return err
}
// TODO allow dropping a requirement without specifying
// the major version - we can use the default field to disambiguate.
c.addEdit(func(f *modfile.File) error {
delete(f.Deps, arg)
return nil
})
return nil
}

func addFlagVar(cmd *cobra.Command, v pflag.Value, name string, usage string) {
flags := cmd.Flags()
flags.Var(v, name, usage)
// This works around https://github.com/spf13/pflag/issues/281
if v, ok := v.(interface{ IsBoolFlag() bool }); ok && v.IsBoolFlag() {
flags.Lookup(name).NoOptDefVal = "true"
}
}

type flagFunc func(string) error

func (f flagFunc) String() string { return "" }
func (f flagFunc) Set(s string) error { return f(s) }
func (f flagFunc) Type() string {
return "string"
}

type boolFlagFunc func(bool) error

func (f boolFlagFunc) String() string { return "" }
func (f boolFlagFunc) Set(s string) error {
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
return f(b)
}
func (f boolFlagFunc) Type() string {
return "bool"
}
func (f boolFlagFunc) IsBoolFlag() bool { return true }
41 changes: 41 additions & 0 deletions cmd/cue/cmd/testdata/script/modedit_initial.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
exec cue mod edit --source git
cmp cue.mod/module.cue want-module-1

exec cue mod edit --drop-source
cmp cue.mod/module.cue want-module-2

exec cue mod edit --require foo.bar@v0.2.3
cmp cue.mod/module.cue want-module-3

! exec cue mod edit --require bad-module!
cmp stderr want-stderr

exec cue mod edit --drop-require foo.bar@v0
cmp cue.mod/module.cue want-module-4

exec cue mod edit --module othermain.org@v1
cmp cue.mod/module.cue want-module-5


-- cue.mod/module.cue --
module: "main.org@v0"
-- want-module-1 --
module: "main.org@v0"
source: {
kind: "git"
}
-- want-module-2 --
module: "main.org@v0"
-- want-module-3 --
module: "main.org@v0"
deps: {
"foo.bar@v0": {
v: "v0.2.3"
}
}
-- want-stderr --
invalid argument "bad-module!" for "--require" flag: invalid module path@version "bad-module!"
-- want-module-4 --
module: "main.org@v0"
-- want-module-5 --
module: "othermain.org@v1"

0 comments on commit b7c1a0d

Please sign in to comment.