this repo has no description
0
fork

Configure Feed

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

cmd/cue: make mod resolve work with no args

When there are no arguments, `cue mod resolve` should
provide information about the current module. Currently
it does nothing in this case.

Also treat "." as a special case, so `cue mod resolve`
is equivalent to `cue mod resolve .` much as `cue eval` is
equivalent to `cue eval .`.

Fixes #3195.

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

+52 -13
+1 -11
cmd/cue/cmd/modedit.go
··· 18 18 "bytes" 19 19 "fmt" 20 20 "os" 21 - "path/filepath" 22 21 "strconv" 23 22 24 23 "cuelang.org/go/internal/cueversion" ··· 71 70 } 72 71 73 72 func (c *modEditCmd) run(cmd *Command, args []string) error { 74 - modRoot, err := findModuleRoot() 75 - if err != nil { 76 - return err 77 - } 78 - modPath := filepath.Join(modRoot, "cue.mod", "module.cue") 79 - data, err := os.ReadFile(modPath) 80 - if err != nil { 81 - return err 82 - } 83 - mf, err := modfile.ParseNonStrict(data, modPath) 73 + modPath, mf, data, err := readModuleFile() 84 74 if err != nil { 85 75 return err 86 76 }
+18
cmd/cue/cmd/modget.go
··· 25 25 26 26 "cuelang.org/go/internal/httplog" 27 27 "cuelang.org/go/internal/mod/modload" 28 + "cuelang.org/go/mod/modfile" 28 29 ) 29 30 30 31 func newModGetCmd(c *Command) *cobra.Command { ··· 98 99 return err 99 100 } 100 101 return nil 102 + } 103 + 104 + func readModuleFile() (string, *modfile.File, []byte, error) { 105 + modRoot, err := findModuleRoot() 106 + if err != nil { 107 + return "", nil, nil, err 108 + } 109 + modPath := filepath.Join(modRoot, "cue.mod", "module.cue") 110 + data, err := os.ReadFile(modPath) 111 + if err != nil { 112 + return "", nil, nil, err 113 + } 114 + mf, err := modfile.ParseNonStrict(data, modPath) 115 + if err != nil { 116 + return "", nil, nil, err 117 + } 118 + return modPath, mf, data, nil 101 119 } 102 120 103 121 func findModuleRoot() (string, error) {
+23 -2
cmd/cue/cmd/modresolve.go
··· 21 21 "cuelabs.dev/go/oci/ociregistry/ociref" 22 22 "github.com/spf13/cobra" 23 23 24 + "cuelang.org/go/mod/modfile" 24 25 "cuelang.org/go/mod/module" 25 26 ) 26 27 27 28 func newModResolveCmd(c *Command) *cobra.Command { 28 29 cmd := &cobra.Command{ 29 - Use: "resolve <modulepath>[@<version>] ...", 30 + Use: "resolve [<modulepath>[@<version>] ...]", 30 31 Short: "Show how a module path resolves to a registry", 31 32 Long: `This command prints information about how a given 32 33 module path will resolve to an actual registry in the ··· 39 40 with respect to the registry configuration (see "cue help registryconfig") 40 41 and does not make any network calls to check whether 41 42 the module exists. 43 + 44 + If no arguments are provided, the current module path is used. 45 + This is equivalent to specifying "." as an argument, which 46 + also refers to the current module. 42 47 43 48 Note that this command is not yet stable and may be changed. 44 49 `, ··· 55 60 if resolver == nil { 56 61 return fmt.Errorf("modules experiment not enabled (enable with CUE_EXPERIMENT=modules)") 57 62 } 63 + var mf *modfile.File 64 + if len(args) == 0 { 65 + // Use the current module if no arguments are provided. 66 + args = []string{"."} 67 + } 58 68 59 69 for _, arg := range args { 70 + if arg == "." { 71 + if mf == nil { 72 + var err error 73 + _, mf, _, err = readModuleFile() 74 + if err != nil { 75 + return err 76 + } 77 + } 78 + arg = mf.Module 79 + } 80 + 60 81 mpath, vers, ok := strings.Cut(arg, "@") 61 82 if ok { 62 83 if _, err := module.ParseVersion(arg); err != nil { 63 84 return fmt.Errorf("invalid module path: %v", err) 64 85 } 65 86 } else { 66 - mpath = args[0] 87 + mpath = arg 67 88 if err := module.CheckPathWithoutVersion(arg); err != nil { 68 89 return fmt.Errorf("invalid module path: %v", err) 69 90 }
+10
cmd/cue/cmd/testdata/script/registry_resolve.txtar
··· 26 26 exec cue mod resolve stripped.org/bar/baz/p@v0.1.2 27 27 cmp stdout want-resolve6 28 28 29 + exec cue mod resolve 30 + cmp stdout want-resolve7 31 + 32 + exec cue mod resolve . 33 + cmp stdout want-resolve7 34 + -- cue.mod/module.cue -- 35 + module: "a.com/foo/bar" 36 + language: version: "v0.9.2" 29 37 -- registry-config.cue -- 30 38 moduleRegistries: { 31 39 "a.com": { ··· 58 66 r2.example/xxx/0764601cd4e1d9d84efc2f4ddc23191e599fa9826f46b16b84269b6fe3679184:cue-v1.2.3 59 67 -- want-resolve6 -- 60 68 r3.example/repo/baz/p:v0.1.2 69 + -- want-resolve7 -- 70 + r2.example/xxx/7c9813cd44d6d9348eb223e71d608e491805730547b8ff6f4b1ccb964b913148