]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/mod/edit: add -tool and -droptool support
authorConrad Irwin <conrad.irwin@gmail.com>
Fri, 19 Jul 2024 02:26:13 +0000 (20:26 -0600)
committerMichael Matloob <matloob@golang.org>
Sat, 17 Aug 2024 03:03:34 +0000 (03:03 +0000)
For golang/go#48429

Change-Id: I1a7bd8ffddbc65e3b687dc1d40f3853702e1b5dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/521958
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/alldocs.go
src/cmd/go/internal/modcmd/edit.go
src/cmd/go/testdata/script/mod_edit.txt

index f86d315f5f57df688e24092e17919a089588cdce..e28f9c30b4a8fe2126369b17ebf1179b45347dfb 100644 (file)
 // like "v1.2.3" or a closed interval like "[v1.1.0,v1.1.9]". Note that
 // -retract=version is a no-op if that retraction already exists.
 //
+// The -tool=path and -droptool=path flags add and drop a tool declaration
+// for the given path.
+//
 // The -godebug, -dropgodebug, -require, -droprequire, -exclude, -dropexclude,
-// -replace, -dropreplace, -retract, and -dropretract editing flags may be
-// repeated, and the changes are applied in the order given.
+// -replace, -dropreplace, -retract, -dropretract, -tool, and -droptool editing
+// flags may be repeated, and the changes are applied in the order given.
 //
 // The -print flag prints the final go.mod in its text format instead of
 // writing it back to go.mod.
 //             Rationale string
 //     }
 //
+//     type Tool struct {
+//             Path string
+//     }
+//
 // Retract entries representing a single version (not an interval) will have
 // the "Low" and "High" fields set to the same value.
 //
index 9b0c768ba224c3f25fa58cd8303910a984f43ccc..da84c9ff11077257da345a00e041c236efb7a189 100644 (file)
@@ -87,9 +87,12 @@ retraction on the given version. The version may be a single version
 like "v1.2.3" or a closed interval like "[v1.1.0,v1.1.9]". Note that
 -retract=version is a no-op if that retraction already exists.
 
+The -tool=path and -droptool=path flags add and drop a tool declaration
+for the given path.
+
 The -godebug, -dropgodebug, -require, -droprequire, -exclude, -dropexclude,
--replace, -dropreplace, -retract, and -dropretract editing flags may be
-repeated, and the changes are applied in the order given.
+-replace, -dropreplace, -retract, -dropretract, -tool, and -droptool editing
+flags may be repeated, and the changes are applied in the order given.
 
 The -print flag prints the final go.mod in its text format instead of
 writing it back to go.mod.
@@ -140,6 +143,10 @@ writing it back to go.mod. The JSON output corresponds to these Go types:
                Rationale string
        }
 
+       type Tool struct {
+               Path string
+       }
+
 Retract entries representing a single version (not an interval) will have
 the "Low" and "High" fields set to the same value.
 
@@ -181,6 +188,8 @@ func init() {
        cmdEdit.Flag.Var(flagFunc(flagDropReplace), "dropreplace", "")
        cmdEdit.Flag.Var(flagFunc(flagRetract), "retract", "")
        cmdEdit.Flag.Var(flagFunc(flagDropRetract), "dropretract", "")
+       cmdEdit.Flag.Var(flagFunc(flagTool), "tool", "")
+       cmdEdit.Flag.Var(flagFunc(flagDropTool), "droptool", "")
 
        base.AddBuildFlagsNX(&cmdEdit.Flag)
        base.AddChdirFlag(&cmdEdit.Flag)
@@ -330,6 +339,25 @@ func parsePath(flag, arg string) (path string) {
        return path
 }
 
+// parsePath parses -flag=arg expecting arg to be path to a tool (allows ./)
+func parseToolPath(flag, arg string) (path string) {
+       if strings.Contains(arg, "@") {
+               base.Fatalf("go: -%s=%s: need just path, not path@version", flag, arg)
+       }
+       if arg == "." {
+               return arg
+       }
+       toCheck := arg
+       if strings.HasPrefix(arg, "./") {
+               toCheck = arg[2:]
+       }
+       if err := module.CheckImportPath(toCheck); err != nil {
+               base.Fatalf("go: -%s=%s: invalid path: %v", flag, arg, err)
+       }
+
+       return arg
+}
+
 // parsePathVersionOptional parses path[@version], using adj to
 // describe any errors.
 func parsePathVersionOptional(adj, arg string, allowDirPath bool) (path, version string, err error) {
@@ -517,6 +545,26 @@ func flagDropRetract(arg string) {
        })
 }
 
+// flagTool implements the -tool flag.
+func flagTool(arg string) {
+       path := parseToolPath("tool", arg)
+       edits = append(edits, func(f *modfile.File) {
+               if err := f.AddTool(path); err != nil {
+                       base.Fatalf("go: -tool=%s: %v", arg, err)
+               }
+       })
+}
+
+// flagDropTool implements the -droptool flag.
+func flagDropTool(arg string) {
+       path := parseToolPath("droptool", arg)
+       edits = append(edits, func(f *modfile.File) {
+               if err := f.DropTool(path); err != nil {
+                       base.Fatalf("go: -droptool=%s: %v", arg, err)
+               }
+       })
+}
+
 // fileJSON is the -json output data structure.
 type fileJSON struct {
        Module    editModuleJSON
@@ -526,6 +574,7 @@ type fileJSON struct {
        Exclude   []module.Version
        Replace   []replaceJSON
        Retract   []retractJSON
+       Tool      []toolJSON
 }
 
 type editModuleJSON struct {
@@ -550,6 +599,10 @@ type retractJSON struct {
        Rationale string `json:",omitempty"`
 }
 
+type toolJSON struct {
+       Path string
+}
+
 // editPrintJSON prints the -json output.
 func editPrintJSON(modFile *modfile.File) {
        var f fileJSON
@@ -577,6 +630,9 @@ func editPrintJSON(modFile *modfile.File) {
        for _, r := range modFile.Retract {
                f.Retract = append(f.Retract, retractJSON{r.Low, r.High, r.Rationale})
        }
+       for _, t := range modFile.Tool {
+               f.Tool = append(f.Tool, toolJSON{t.Path})
+       }
        data, err := json.MarshalIndent(&f, "", "\t")
        if err != nil {
                base.Fatalf("go: internal error: %v", err)
index 49ff464fa2bf61517391b6af06e028ddaa5d8480..0b676cf6f966bdf71d41da3ce5ef7d4429a30c92 100644 (file)
@@ -97,6 +97,20 @@ cmpenv go.mod go.mod.edit
 go mod edit -dropgodebug key
 cmpenv go.mod go.mod.start
 
+# go mod edit -tool
+cd $WORK/h
+cp go.mod.start go.mod
+go mod edit -tool example.com/tool
+cmpenv go.mod go.mod.edit
+go mod edit -tool ./local
+cmpenv go.mod go.mod.edit2
+go mod edit -droptool ./local
+cmpenv go.mod go.mod.edit
+go mod edit -droptool example.com/tool2
+cmpenv go.mod go.mod.edit
+go mod edit -droptool example.com/tool
+cmpenv go.mod go.mod.start
+
 -- x.go --
 package x
 
@@ -184,7 +198,8 @@ require x.3 v1.99.0
                        "Low": "v1.3.0",
                        "High": "v1.4.0"
                }
-       ]
+       ],
+       "Tool": null
 }
 -- $WORK/go.mod.edit3 --
 module x.x/y/z
@@ -321,7 +336,8 @@ retract (
                        "High": "v1.0.2",
                        "Rationale": "c"
                }
-       ]
+       ],
+       "Tool": null
 }
 -- $WORK/go.mod.deprecation --
 // Deprecated: and the new one is not ready yet
@@ -335,7 +351,8 @@ module m
        "Require": null,
        "Exclude": null,
        "Replace": null,
-       "Retract": null
+       "Retract": null,
+       "Tool": null
 }
 -- $WORK/go.mod.empty --
 -- $WORK/go.mod.empty.json --
@@ -346,7 +363,8 @@ module m
        "Require": null,
        "Exclude": null,
        "Replace": null,
-       "Retract": null
+       "Retract": null,
+       "Tool": null
 }
 -- $WORK/g/go.mod.start --
 module g
@@ -358,3 +376,22 @@ module g
 go 1.10
 
 godebug key=value
+-- $WORK/h/go.mod.start --
+module g
+
+go 1.24
+-- $WORK/h/go.mod.edit --
+module g
+
+go 1.24
+
+tool example.com/tool
+-- $WORK/h/go.mod.edit2 --
+module g
+
+go 1.24
+
+tool (
+       ./local
+       example.com/tool
+)