| 1 |
// Copyright 2022 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 |
package workcmd |
| 6 |
|
| 7 |
import ( |
| 8 |
"cmd/go/internal/base" |
| 9 |
"cmd/go/internal/cfg" |
| 10 |
"cmd/go/internal/modcmd" |
| 11 |
"cmd/go/internal/modload" |
| 12 |
"context" |
| 13 |
) |
| 14 |
|
| 15 |
var cmdVendor = &base.Command{ |
| 16 |
UsageLine: "go work vendor [-e] [-v] [-o outdir]", |
| 17 |
Short: "make vendored copy of dependencies", |
| 18 |
Long: ` |
| 19 |
Vendor resets the workspace's vendor directory to include all packages |
| 20 |
needed to build and test all the workspace's packages. |
| 21 |
It does not include test code for vendored packages. |
| 22 |
|
| 23 |
The -v flag causes vendor to print the names of vendored |
| 24 |
modules and packages to standard error. |
| 25 |
|
| 26 |
The -e flag causes vendor to attempt to proceed despite errors |
| 27 |
encountered while loading packages. |
| 28 |
|
| 29 |
The -o flag causes vendor to create the vendor directory at the given |
| 30 |
path instead of "vendor". The go command can only use a vendor directory |
| 31 |
named "vendor" within the module root directory, so this flag is |
| 32 |
primarily useful for other tools.`, |
| 33 |
|
| 34 |
Run: runVendor, |
| 35 |
} |
| 36 |
|
| 37 |
var vendorE bool // if true, report errors but proceed anyway |
| 38 |
var vendorO string // if set, overrides the default output directory |
| 39 |
|
| 40 |
func init() { |
| 41 |
cmdVendor.Flag.BoolVar(&cfg.BuildV, "v", false, "") |
| 42 |
cmdVendor.Flag.BoolVar(&vendorE, "e", false, "") |
| 43 |
cmdVendor.Flag.StringVar(&vendorO, "o", "", "") |
| 44 |
base.AddChdirFlag(&cmdVendor.Flag) |
| 45 |
base.AddModCommonFlags(&cmdVendor.Flag) |
| 46 |
} |
| 47 |
|
| 48 |
func runVendor(ctx context.Context, cmd *base.Command, args []string) { |
| 49 |
moduleLoaderState := modload.NewState() |
| 50 |
moduleLoaderState.InitWorkfile() |
| 51 |
if modload.WorkFilePath(moduleLoaderState) == "" { |
| 52 |
base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)") |
| 53 |
} |
| 54 |
|
| 55 |
modcmd.RunVendor(moduleLoaderState, ctx, vendorE, vendorO, args) |
| 56 |
} |