diff options
Diffstat (limited to 'libgo/go/cmd/go/internal/modcmd/edit.go')
-rw-r--r-- | libgo/go/cmd/go/internal/modcmd/edit.go | 45 |
1 files changed, 35 insertions, 10 deletions
diff --git a/libgo/go/cmd/go/internal/modcmd/edit.go b/libgo/go/cmd/go/internal/modcmd/edit.go index 5fea3e48e08..5066e4ddf75 100644 --- a/libgo/go/cmd/go/internal/modcmd/edit.go +++ b/libgo/go/cmd/go/internal/modcmd/edit.go @@ -7,6 +7,7 @@ package modcmd import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -15,6 +16,7 @@ import ( "strings" "cmd/go/internal/base" + "cmd/go/internal/modfetch" "cmd/go/internal/modfile" "cmd/go/internal/modload" "cmd/go/internal/module" @@ -62,6 +64,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 +78,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 +107,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 +136,7 @@ func init() { func runEdit(cmd *base.Command, args []string) { anyFlags := *editModule != "" || + *editGo != "" || *editJSON || *editPrint || *editFmt || @@ -151,8 +157,7 @@ func runEdit(cmd *base.Command, args []string) { if len(args) == 1 { gomod = args[0] } else { - modload.MustInit() - gomod = filepath.Join(modload.ModRoot, "go.mod") + gomod = filepath.Join(modload.ModRoot(), "go.mod") } if *editModule != "" { @@ -161,7 +166,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 { @@ -174,7 +183,13 @@ func runEdit(cmd *base.Command, args []string) { } if *editModule != "" { - modFile.AddModuleStmt(modload.CmdModModule) + modFile.AddModuleStmt(*editModule) + } + + if *editGo != "" { + if err := modFile.AddGoStmt(*editGo); err != nil { + base.Fatalf("go: internal error: %v", err) + } } if len(edits) > 0 { @@ -190,17 +205,23 @@ func runEdit(cmd *base.Command, args []string) { return } - data, err = modFile.Format() + out, err := modFile.Format() if err != nil { base.Fatalf("go: %v", err) } if *editPrint { - os.Stdout.Write(data) + os.Stdout.Write(out) return } - if err := ioutil.WriteFile(gomod, data, 0666); err != nil { + unlock := modfetch.SideLock() + defer unlock() + lockedData, err := ioutil.ReadFile(gomod) + if err == nil && !bytes.Equal(lockedData, data) { + base.Fatalf("go: go.mod changed during editing; not overwriting") + } + if err := ioutil.WriteFile(gomod, out, 0666); err != nil { base.Fatalf("go: %v", err) } } @@ -344,6 +365,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 +386,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}) } |