From: Conrad Irwin Date: Fri, 19 Jul 2024 02:26:13 +0000 (-0600) Subject: cmd/mod/edit: add -tool and -droptool support X-Git-Tag: go1.24rc1~1177 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=27093581b2828a2752a6d2711def09517eb2513b;p=gostls13.git cmd/mod/edit: add -tool and -droptool support For golang/go#48429 Change-Id: I1a7bd8ffddbc65e3b687dc1d40f3853702e1b5dc Reviewed-on: https://go-review.googlesource.com/c/go/+/521958 LUCI-TryBot-Result: Go LUCI Reviewed-by: Sam Thanawalla Reviewed-by: Michael Matloob TryBot-Result: Gopher Robot --- diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index f86d315f5f..e28f9c30b4 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -1251,9 +1251,12 @@ // 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. @@ -1304,6 +1307,10 @@ // 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. // diff --git a/src/cmd/go/internal/modcmd/edit.go b/src/cmd/go/internal/modcmd/edit.go index 9b0c768ba2..da84c9ff11 100644 --- a/src/cmd/go/internal/modcmd/edit.go +++ b/src/cmd/go/internal/modcmd/edit.go @@ -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) diff --git a/src/cmd/go/testdata/script/mod_edit.txt b/src/cmd/go/testdata/script/mod_edit.txt index 49ff464fa2..0b676cf6f9 100644 --- a/src/cmd/go/testdata/script/mod_edit.txt +++ b/src/cmd/go/testdata/script/mod_edit.txt @@ -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 +)