diff options
author | Michael Matloob <matloob@golang.org> | 2021-06-14 19:22:58 -0400 |
---|---|---|
committer | Michael Matloob <matloob@golang.org> | 2021-07-27 21:27:13 +0000 |
commit | b2205eab0efef6cba784aca4436cb0ef8ac0a4de (patch) | |
tree | 08d00ae3db06b43147ebad0eef605de72b97d88f /src/cmd/go/internal/modload/init.go | |
parent | f05f5ceffa6edec89436a825176eefdd1fe828e5 (diff) | |
download | go-b2205eab0efef6cba784aca4436cb0ef8ac0a4de.tar.gz go-b2205eab0efef6cba784aca4436cb0ef8ac0a4de.zip |
[dev.cmdgo] cmd/go: add go mod initwork command
This command is used to create a go.work file with a set of modules
given in the arguments to the command.
For #45713
Change-Id: I09f8cefc5849dd43c234dc4a37091791fcc02ebe
Reviewed-on: https://go-review.googlesource.com/c/go/+/334936
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/init.go')
-rw-r--r-- | src/cmd/go/internal/modload/init.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 607054d1eb..18f0f2b8f8 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -767,6 +767,24 @@ func CreateModFile(ctx context.Context, modPath string) { } } +// CreateWorkFile initializes a new workspace by creating a go.work file. +func CreateWorkFile(ctx context.Context, workFile string, modDirs []string) { + _ = TODOWorkspaces("Report an error if the file already exists.") + + goV := LatestGoVersion() // Use current Go version by default + workF := new(modfile.WorkFile) + workF.Syntax = new(modfile.FileSyntax) + workF.AddGoStmt(goV) + + for _, dir := range modDirs { + _ = TODOWorkspaces("Add the module path of the module.") + workF.AddDirectory(dir, "") + } + + data := modfile.Format(workF.Syntax) + lockedfile.Write(workFile, bytes.NewReader(data), 0644) +} + // fixVersion returns a modfile.VersionFixer implemented using the Query function. // // It resolves commit hashes and branch names to versions, |