|
5 | 5 | "log"
|
6 | 6 | "os"
|
7 | 7 | "os/exec"
|
| 8 | + "path/filepath" |
8 | 9 | "strings"
|
9 | 10 |
|
10 | 11 | versionutil "k8s.io/apimachinery/pkg/util/version"
|
@@ -39,6 +40,52 @@ func GoModTidy(modroot, goVersion, compat string) (string, error) {
|
39 | 40 | return "", nil
|
40 | 41 | }
|
41 | 42 |
|
| 43 | +func findWorkspaceFile(dir string) (root string) { |
| 44 | + dir = filepath.Clean(dir) |
| 45 | + // Look for enclosing go.mod. |
| 46 | + for { |
| 47 | + f := filepath.Join(dir, "go.work") |
| 48 | + if fi, err := os.Stat(f); err == nil && !fi.IsDir() { |
| 49 | + return f |
| 50 | + } |
| 51 | + d := filepath.Dir(dir) |
| 52 | + if d == dir { |
| 53 | + break |
| 54 | + } |
| 55 | + dir = d |
| 56 | + } |
| 57 | + return "" |
| 58 | +} |
| 59 | + |
| 60 | +func findGoWork(modroot string) string { |
| 61 | + switch gowork := os.Getenv("GOWORK"); gowork { |
| 62 | + case "off": |
| 63 | + return "" |
| 64 | + case "", "auto": |
| 65 | + return findWorkspaceFile(modroot) |
| 66 | + default: |
| 67 | + return gowork |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func GoVendor(dir string) (string, error) { |
| 72 | + if findGoWork(dir) == "" { |
| 73 | + log.Print("Running go mod vendor...") |
| 74 | + cmd := exec.Command("go", "mod", "vendor") |
| 75 | + if bytes, err := cmd.CombinedOutput(); err != nil { |
| 76 | + return strings.TrimSpace(string(bytes)), err |
| 77 | + } |
| 78 | + } else { |
| 79 | + log.Print("Running go work vendor...") |
| 80 | + cmd := exec.Command("go", "work", "vendor") |
| 81 | + if bytes, err := cmd.CombinedOutput(); err != nil { |
| 82 | + return strings.TrimSpace(string(bytes)), err |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return "", nil |
| 87 | +} |
| 88 | + |
42 | 89 | func GoGetModule(name, version, modroot string) (string, error) {
|
43 | 90 | cmd := exec.Command("go", "get", fmt.Sprintf("%s@%s", name, version)) //nolint:gosec
|
44 | 91 | cmd.Dir = modroot
|
|
0 commit comments