github.com/bir3/gocompiler@v0.3.205/src/cmd/gocmd/internal/workcmd/init.go (about) 1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // go work init 6 7 package workcmd 8 9 import ( 10 "github.com/bir3/gocompiler/src/cmd/gocmd/internal/base" 11 "github.com/bir3/gocompiler/src/cmd/gocmd/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.AddChdirFlag(&cmdInit.Flag) 38 base.AddModCommonFlags(&cmdInit.Flag) 39 } 40 41 func runInit(ctx context.Context, cmd *base.Command, args []string) { 42 modload.InitWorkfile() 43 44 modload.ForceUseModules = true 45 46 workFile := modload.WorkFilePath() 47 if workFile == "" { 48 workFile = filepath.Join(base.Cwd(), "go.work") 49 } 50 51 modload.CreateWorkFile(ctx, workFile, args) 52 }