// 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.
//
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.
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)
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) {
})
}
+// 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
Exclude []module.Version
Replace []replaceJSON
Retract []retractJSON
+ Tool []toolJSON
}
type editModuleJSON struct {
Rationale string `json:",omitempty"`
}
+type toolJSON struct {
+ Path string
+}
+
// editPrintJSON prints the -json output.
func editPrintJSON(modFile *modfile.File) {
var f fileJSON
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)
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
"Low": "v1.3.0",
"High": "v1.4.0"
}
- ]
+ ],
+ "Tool": null
}
-- $WORK/go.mod.edit3 --
module x.x/y/z
"High": "v1.0.2",
"Rationale": "c"
}
- ]
+ ],
+ "Tool": null
}
-- $WORK/go.mod.deprecation --
// Deprecated: and the new one is not ready yet
"Require": null,
"Exclude": null,
"Replace": null,
- "Retract": null
+ "Retract": null,
+ "Tool": null
}
-- $WORK/go.mod.empty --
-- $WORK/go.mod.empty.json --
"Require": null,
"Exclude": null,
"Replace": null,
- "Retract": null
+ "Retract": null,
+ "Tool": null
}
-- $WORK/g/go.mod.start --
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
+)