// Copyright 2020 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.package gocommandimport ()// ModuleJSON holds information about a module.typeModuleJSONstruct {Pathstring// module pathVersionstring// module versionVersions []string// available module versions (with -versions)Replace *ModuleJSON// replaced by this moduleTime *time.Time// time version was createdUpdate *ModuleJSON// available update, if any (with -u)Mainbool// is this the main module?Indirectbool// is this module only an indirect dependency of main module?Dirstring// directory holding files for this module, if anyGoModstring// path to go.mod file used when loading this module, if anyGoVersionstring// go version used in module}varmodFlagRegexp = regexp.MustCompile(`-mod[ =](\w+)`)// VendorEnabled reports whether vendoring is enabled. It takes a *Runner to execute Go commands// with the supplied context.Context and Invocation. The Invocation can contain pre-defined fields,// of which only Verb and Args are modified to run the appropriate Go command.// Inspired by setDefaultBuildMod in modload/init.gofunc ( context.Context, Invocation, *Runner) (bool, *ModuleJSON, error) { , , := getMainModuleAnd114(, , )if != nil {returnfalse, nil, }// We check the GOFLAGS to see if there is anything overridden or not. .Verb = "env" .Args = []string{"GOFLAGS"} , := .Run(, )if != nil {returnfalse, nil, } := string(bytes.TrimSpace(.Bytes())) := modFlagRegexp.FindStringSubmatch()varstringiflen() != 0 { = [1] }// Don't override an explicit '-mod=' argument.if == "vendor" {returntrue, , nil } elseif != "" {returnfalse, nil, nil }if == nil || ! {returnfalse, nil, nil }// Check 1.14's automatic vendor mode.if , := os.Stat(filepath.Join(.Dir, "vendor")); == nil && .IsDir() {if .GoVersion != "" && semver.Compare("v"+.GoVersion, "v1.14") >= 0 {// The Go version is at least 1.14, and a vendor directory exists. // Set -mod=vendor by default.returntrue, , nil } }returnfalse, nil, nil}// getMainModuleAnd114 gets one of the main modules' information and whether the// go command in use is 1.14+. This is the information needed to figure out// if vendoring should be enabled.func ( context.Context, Invocation, *Runner) (*ModuleJSON, bool, error) {const = `{{.Path}}{{.Dir}}{{.GoMod}}{{.GoVersion}}{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}` .Verb = "list" .Args = []string{"-m", "-f", } , := .Run(, )if != nil {returnnil, false, } := strings.Split(.String(), "\n")iflen() < 5 {returnnil, false, fmt.Errorf("unexpected stdout: %q", .String()) } := &ModuleJSON{Path: [0],Dir: [1],GoMod: [2],GoVersion: [3],Main: true, }return , [4] == "go1.14", nil}
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)