Skip to content

Commit

Permalink
cmd/mod/edit: add -tool and -droptool support
Browse files Browse the repository at this point in the history
For #48429

Change-Id: I1a7bd8ffddbc65e3b687dc1d40f3853702e1b5dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/521958
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Sam Thanawalla <[email protected]>
Reviewed-by: Michael Matloob <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
ConradIrwin authored and matloob committed Aug 17, 2024
1 parent 0b23e47 commit 2709358
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 58 additions & 2 deletions src/cmd/go/internal/modcmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -526,6 +574,7 @@ type fileJSON struct {
Exclude []module.Version
Replace []replaceJSON
Retract []retractJSON
Tool []toolJSON
}

type editModuleJSON struct {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 41 additions & 4 deletions src/cmd/go/testdata/script/mod_edit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 --
Expand All @@ -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
Expand All @@ -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 comments on commit 2709358

Please sign in to comment.