summaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modcmd/init.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/go/internal/modcmd/init.go')
-rw-r--r--src/cmd/go/internal/modcmd/init.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/cmd/go/internal/modcmd/init.go b/src/cmd/go/internal/modcmd/init.go
new file mode 100644
index 0000000000..f510a46262
--- /dev/null
+++ b/src/cmd/go/internal/modcmd/init.go
@@ -0,0 +1,41 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// go mod init
+
+package modcmd
+
+import (
+ "cmd/go/internal/base"
+ "cmd/go/internal/modload"
+ "os"
+)
+
+var cmdInit = &base.Command{
+ UsageLine: "go mod init [module]",
+ Short: "initialize new module in current directory",
+ Long: `
+Init initializes and writes a new go.mod to the current directory,
+in effect creating a new module rooted at the current directory.
+The file go.mod must not already exist.
+If possible, init will guess the module path from import comments
+(see 'go help importpath') or from version control configuration.
+To override this guess, supply the module path as an argument.
+ `,
+ Run: runInit,
+}
+
+func runInit(cmd *base.Command, args []string) {
+ modload.CmdModInit = true
+ if len(args) > 1 {
+ base.Fatalf("go mod init: too many arguments")
+ }
+ if len(args) == 1 {
+ modload.CmdModModule = args[0]
+ }
+ if _, err := os.Stat("go.mod"); err == nil {
+ base.Fatalf("go mod init: go.mod already exists")
+ }
+ modload.InitMod() // does all the hard work
+}