diff options
author | Ian Lance Taylor <iant@golang.org> | 2018-11-02 17:08:24 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2018-11-10 01:42:03 +0000 |
commit | c5aea7a4941aa9c37ed07e3a252bc81c9c90802f (patch) | |
tree | 1b9d66caff02d3559d75ee5297569164016c718a /src/cmd/go/internal/modcmd/edit.go | |
parent | 6887d8b1e258898db4030b10383a853b0dd96318 (diff) | |
download | go-c5aea7a4941aa9c37ed07e3a252bc81c9c90802f.tar.gz go-c5aea7a4941aa9c37ed07e3a252bc81c9c90802f.zip |
cmd/go: add go mod edit -go flag
It can be used to set the Go language version used by the module.
RELNOTES=yes
Updates #28221
Change-Id: Ief0dd185c01195a17be20dff8627c80943c436e7
Reviewed-on: https://go-review.googlesource.com/c/147282
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modcmd/edit.go')
-rw-r--r-- | src/cmd/go/internal/modcmd/edit.go | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/cmd/go/internal/modcmd/edit.go b/src/cmd/go/internal/modcmd/edit.go index 5fea3e48e0..c589c6d4ed 100644 --- a/src/cmd/go/internal/modcmd/edit.go +++ b/src/cmd/go/internal/modcmd/edit.go @@ -62,6 +62,8 @@ The -require, -droprequire, -exclude, -dropexclude, -replace, and -dropreplace editing flags may be repeated, and the changes are applied in the order given. +The -go=version flag sets the expected Go language version. + The -print flag prints the final go.mod in its text format instead of writing it back to go.mod. @@ -74,7 +76,8 @@ writing it back to go.mod. The JSON output corresponds to these Go types: } type GoMod struct { - Module Module + Module Module + Go string Require []Require Exclude []Module Replace []Replace @@ -102,8 +105,8 @@ by invoking 'go mod edit' with -require, -exclude, and so on. } var ( - editFmt = cmdEdit.Flag.Bool("fmt", false, "") - // editGo = cmdEdit.Flag.String("go", "", "") + editFmt = cmdEdit.Flag.Bool("fmt", false, "") + editGo = cmdEdit.Flag.String("go", "", "") editJSON = cmdEdit.Flag.Bool("json", false, "") editPrint = cmdEdit.Flag.Bool("print", false, "") editModule = cmdEdit.Flag.String("module", "", "") @@ -131,6 +134,7 @@ func init() { func runEdit(cmd *base.Command, args []string) { anyFlags := *editModule != "" || + *editGo != "" || *editJSON || *editPrint || *editFmt || @@ -161,7 +165,11 @@ func runEdit(cmd *base.Command, args []string) { } } - // TODO(rsc): Implement -go= once we start advertising it. + if *editGo != "" { + if !modfile.GoVersionRE.MatchString(*editGo) { + base.Fatalf(`go mod: invalid -go option; expecting something like "-go 1.12"`) + } + } data, err := ioutil.ReadFile(gomod) if err != nil { @@ -177,6 +185,12 @@ func runEdit(cmd *base.Command, args []string) { modFile.AddModuleStmt(modload.CmdModModule) } + if *editGo != "" { + if err := modFile.AddGoStmt(*editGo); err != nil { + base.Fatalf("go: internal error: %v", err) + } + } + if len(edits) > 0 { for _, edit := range edits { edit(modFile) @@ -344,6 +358,7 @@ func flagDropReplace(arg string) { // fileJSON is the -json output data structure. type fileJSON struct { Module module.Version + Go string `json:",omitempty"` Require []requireJSON Exclude []module.Version Replace []replaceJSON @@ -364,6 +379,9 @@ type replaceJSON struct { func editPrintJSON(modFile *modfile.File) { var f fileJSON f.Module = modFile.Module.Mod + if modFile.Go != nil { + f.Go = modFile.Go.Version + } for _, r := range modFile.Require { f.Require = append(f.Require, requireJSON{Path: r.Mod.Path, Version: r.Mod.Version, Indirect: r.Indirect}) } |