Skip to content
Snippets Groups Projects
Commit 27093581 authored by Conrad Irwin's avatar Conrad Irwin Committed by Michael Matloob
Browse files

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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: default avatarSam Thanawalla <samthanawalla@google.com>
Reviewed-by: default avatarMichael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
parent 0b23e477
Branches
Tags
No related merge requests found
......@@ -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.
//
......
......@@ -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)
......
......@@ -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
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment