Skip to content

Commit

Permalink
add support to run go vendor commands (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: hectorj2f <[email protected]>
  • Loading branch information
hectorj2f authored Feb 20, 2024
1 parent d7faf41 commit 8b182eb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/run/gorun.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"

versionutil "k8s.io/apimachinery/pkg/util/version"
Expand Down Expand Up @@ -39,6 +40,52 @@ func GoModTidy(modroot, goVersion, compat string) (string, error) {
return "", nil
}

func findWorkspaceFile(dir string) (root string) {
dir = filepath.Clean(dir)
// Look for enclosing go.mod.
for {
f := filepath.Join(dir, "go.work")
if fi, err := os.Stat(f); err == nil && !fi.IsDir() {
return f
}
d := filepath.Dir(dir)
if d == dir {
break
}
dir = d
}
return ""
}

func findGoWork(modroot string) string {
switch gowork := os.Getenv("GOWORK"); gowork {
case "off":
return ""
case "", "auto":
return findWorkspaceFile(modroot)
default:
return gowork
}
}

func GoVendor(dir string) (string, error) {
if findGoWork(dir) == "" {
log.Print("Running go mod vendor...")
cmd := exec.Command("go", "mod", "vendor")
if bytes, err := cmd.CombinedOutput(); err != nil {
return strings.TrimSpace(string(bytes)), err
}
} else {
log.Print("Running go work vendor...")
cmd := exec.Command("go", "work", "vendor")
if bytes, err := cmd.CombinedOutput(); err != nil {
return strings.TrimSpace(string(bytes)), err
}
}

return "", nil
}

func GoGetModule(name, version, modroot string) (string, error) {
cmd := exec.Command("go", "get", fmt.Sprintf("%s@%s", name, version)) //nolint:gosec
cmd.Dir = modroot
Expand Down
7 changes: 7 additions & 0 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfil
}
}

if _, err := os.Stat(path.Join(cfg.Modroot, "vendor")); err == nil {
output, err := run.GoVendor(cfg.Modroot)
if err != nil {
return nil, fmt.Errorf("failed to run 'go vendor': %v with output: %v", err, output)
}
}

return newModFile, nil
}

Expand Down

0 comments on commit 8b182eb

Please sign in to comment.