blob: bc4620a2a8d3f2121145ee64038ba0329fda599d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// 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"
"context"
)
var cmdInit = &base.Command{
UsageLine: "go mod init [module-path]",
Short: "initialize new module in current directory",
Long: `
Init initializes and writes a new go.mod file in the current directory, in
effect creating a new module rooted at the current directory. The go.mod file
must not already exist.
Init accepts one optional argument, the module path for the new module. If the
module path argument is omitted, init will attempt to infer the module path
using import comments in .go files, vendoring tool configuration files (like
Gopkg.lock), and the current directory (if in GOPATH).
If a configuration file for a vendoring tool is present, init will attempt to
import module requirements from it.
See https://golang.org/ref/mod#go-mod-init for more about 'go mod init'.
`,
Run: runInit,
}
func init() {
base.AddModCommonFlags(&cmdInit.Flag)
}
func runInit(ctx context.Context, cmd *base.Command, args []string) {
if len(args) > 1 {
base.Fatalf("go: 'go mod init' accepts at most one argument")
}
var modPath string
if len(args) == 1 {
modPath = args[0]
}
modload.ForceUseModules = true
modload.CreateModFile(ctx, modPath) // does all the hard work
}
|