1
2
3
4
5
6
7 package workcmd
8
9 import (
10 "cmd/go/internal/base"
11 "cmd/go/internal/modload"
12 "context"
13 "path/filepath"
14 )
15
16 var cmdInit = &base.Command{
17 UsageLine: "go work init [moddirs]",
18 Short: "initialize workspace file",
19 Long: `Init initializes and writes a new go.work file in the
20 current directory, in effect creating a new workspace at the current
21 directory.
22
23 go work init optionally accepts paths to the workspace modules as
24 arguments. If the argument is omitted, an empty workspace with no
25 modules will be created.
26
27 Each argument path is added to a use directive in the go.work file. The
28 current go version will also be listed in the go.work file.
29
30 See the workspaces reference at https://go.dev/ref/mod#workspaces
31 for more information.
32 `,
33 Run: runInit,
34 }
35
36 func init() {
37 base.AddModCommonFlags(&cmdInit.Flag)
38 }
39
40 func runInit(ctx context.Context, cmd *base.Command, args []string) {
41 modload.InitWorkfile()
42
43 modload.ForceUseModules = true
44
45 workFile := modload.WorkFilePath()
46 if workFile == "" {
47 workFile = filepath.Join(base.Cwd(), "go.work")
48 }
49
50 modload.CreateWorkFile(ctx, workFile, args)
51 }
52
View as plain text