From 04d9c8cf9c591f39b7e88369fd850dad5bde245a Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Fri, 10 May 2024 09:31:46 +0900 Subject: [PATCH 1/2] fix: set GOOS=windows when building go_install package with AQUA_GOOS=windows --- pkg/config/package.go | 2 +- pkg/controller/cp/copy.go | 2 +- pkg/controller/install/install.go | 2 +- pkg/cosign/exe_path.go | 2 +- pkg/cosign/verify.go | 2 +- pkg/installpackage/aqua.go | 4 ++-- pkg/installpackage/const.go | 5 +++++ pkg/installpackage/download.go | 2 +- pkg/installpackage/go_install.go | 15 +++++++++------ pkg/installpackage/mock_go_install.go | 2 +- pkg/runtime/runtime.go | 4 ++++ pkg/slsa/exe_path.go | 2 +- 12 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 pkg/installpackage/const.go diff --git a/pkg/config/package.go b/pkg/config/package.go index 6cccbbca3..5da403e6b 100644 --- a/pkg/config/package.go +++ b/pkg/config/package.go @@ -189,7 +189,7 @@ func getArch(rosetta2, windowsARMEmulation bool, replacements registry.Replaceme // Rosetta 2 return replace("amd64", replacements) } - if windowsARMEmulation && rt.GOOS == "windows" && rt.GOARCH == "arm64" { + if windowsARMEmulation && rt.IsWindows() && rt.GOARCH == "arm64" { // Windows ARM Emulation return replace("amd64", replacements) } diff --git a/pkg/controller/cp/copy.go b/pkg/controller/cp/copy.go index 4654af9f6..325b8c114 100644 --- a/pkg/controller/cp/copy.go +++ b/pkg/controller/cp/copy.go @@ -11,7 +11,7 @@ import ( func (c *Controller) copy(logE *logrus.Entry, param *config.Param, findResult *which.FindResult, exeName string) error { p := filepath.Join(param.Dest, exeName) - if c.runtime.GOOS == "windows" && filepath.Ext(exeName) == "" { + if c.runtime.IsWindows() && filepath.Ext(exeName) == "" { p += ".exe" } logE.WithFields(logrus.Fields{ diff --git a/pkg/controller/install/install.go b/pkg/controller/install/install.go index e4f3d744f..76a9092b1 100644 --- a/pkg/controller/install/install.go +++ b/pkg/controller/install/install.go @@ -58,7 +58,7 @@ func (c *Controller) mkBinBatDir() error { if err := osfile.MkdirAll(c.fs, rootBin); err != nil { return fmt.Errorf("create the directory: %w", err) } - if c.runtime.GOOS == "windows" { + if c.runtime.IsWindows() { if err := osfile.MkdirAll(c.fs, filepath.Join(c.rootDir, "bat")); err != nil { return fmt.Errorf("create the directory: %w", err) } diff --git a/pkg/cosign/exe_path.go b/pkg/cosign/exe_path.go index a9d03aeb4..bc982a902 100644 --- a/pkg/cosign/exe_path.go +++ b/pkg/cosign/exe_path.go @@ -14,7 +14,7 @@ type ParamExePath struct { func ExePath(param *ParamExePath) string { assetName := fmt.Sprintf("cosign-%s-%s", param.Runtime.GOOS, param.Runtime.GOARCH) - if param.Runtime.GOOS == "windows" { + if param.Runtime.IsWindows() { assetName += ".exe" } return filepath.Join(param.RootDir, "pkgs", "github_release", "github.com", "sigstore", "cosign", Version, assetName, assetName) diff --git a/pkg/cosign/verify.go b/pkg/cosign/verify.go index 2059efcf6..7d2d07900 100644 --- a/pkg/cosign/verify.go +++ b/pkg/cosign/verify.go @@ -46,7 +46,7 @@ func NewVerifier(executor Executor, fs afero.Fs, downloader download.ClientAPI, Runtime: rt, }), // assets for windows/arm64 aren't released. - disabled: rt.GOOS == "windows" && rt.GOARCH == "arm64", + disabled: rt.IsWindows() && rt.GOARCH == "arm64", } } diff --git a/pkg/installpackage/aqua.go b/pkg/installpackage/aqua.go index a4b8eab21..3184bc0a6 100644 --- a/pkg/installpackage/aqua.go +++ b/pkg/installpackage/aqua.go @@ -28,7 +28,7 @@ func (is *Installer) InstallAqua(ctx context.Context, logE *logrus.Entry, versio Format: "tar.gz", Overrides: []*registry.Override{ { - GOOS: "windows", + GOOS: osWindows, Format: "zip", }, }, @@ -132,7 +132,7 @@ func (is *Installer) InstallAqua(ctx context.Context, logE *logrus.Entry, versio return fmt.Errorf("get the executable file path: %w", err) } - if is.runtime.GOOS == "windows" { + if is.runtime.IsWindows() { return is.Copy(filepath.Join(is.rootDir, "bin", "aqua.exe"), exePath) } diff --git a/pkg/installpackage/const.go b/pkg/installpackage/const.go new file mode 100644 index 000000000..d72e058de --- /dev/null +++ b/pkg/installpackage/const.go @@ -0,0 +1,5 @@ +package installpackage + +const ( + osWindows = "windows" +) diff --git a/pkg/installpackage/download.go b/pkg/installpackage/download.go index 342d17bb3..152c26abb 100644 --- a/pkg/installpackage/download.go +++ b/pkg/installpackage/download.go @@ -57,7 +57,7 @@ func (is *Installer) download(ctx context.Context, logE *logrus.Entry, param *Do pkgInfo := param.Package.PackageInfo if pkgInfo.Type == "go_install" { - return is.downloadGoInstall(ctx, ppkg, param.Dest, logE) + return is.downloadGoInstall(ctx, logE, ppkg, param.Dest, is.runtime.GOOS) } if pkgInfo.Type == "cargo" { diff --git a/pkg/installpackage/go_install.go b/pkg/installpackage/go_install.go index f1cefe8a8..77bb43d37 100644 --- a/pkg/installpackage/go_install.go +++ b/pkg/installpackage/go_install.go @@ -9,7 +9,7 @@ import ( ) type GoInstallInstaller interface { - Install(ctx context.Context, path, gobin string) error + Install(ctx context.Context, path, gobin, goos string) error } type GoInstallInstallerImpl struct { @@ -22,15 +22,18 @@ func NewGoInstallInstallerImpl(exec Executor) *GoInstallInstallerImpl { } } -func (is *GoInstallInstallerImpl) Install(ctx context.Context, path, gobin string) error { - _, err := is.exec.ExecWithEnvs(ctx, "go", []string{"install", path}, []string{"GOBIN=" + gobin}) - if err != nil { +func (is *GoInstallInstallerImpl) Install(ctx context.Context, path, gobin, goos string) error { + envs := []string{"GOBIN=" + gobin} + if goos == "windows" { + envs = append(envs, "GOOS=windows") + } + if _, err := is.exec.ExecWithEnvs(ctx, "go", []string{"install", path}, envs); err != nil { return fmt.Errorf("install a go package: %w", err) } return nil } -func (is *Installer) downloadGoInstall(ctx context.Context, pkg *config.Package, dest string, logE *logrus.Entry) error { +func (is *Installer) downloadGoInstall(ctx context.Context, logE *logrus.Entry, pkg *config.Package, dest, goos string) error { p, err := pkg.RenderPath() if err != nil { return fmt.Errorf("render Go Module Path: %w", err) @@ -40,7 +43,7 @@ func (is *Installer) downloadGoInstall(ctx context.Context, pkg *config.Package, "gobin": dest, "go_package_path": goPkgPath, }).Info("Installing a Go tool") - if err := is.goInstallInstaller.Install(ctx, goPkgPath, dest); err != nil { + if err := is.goInstallInstaller.Install(ctx, goPkgPath, dest, goos); err != nil { return fmt.Errorf("build Go tool: %w", err) } return nil diff --git a/pkg/installpackage/mock_go_install.go b/pkg/installpackage/mock_go_install.go index 0d696bec5..9f82ff384 100644 --- a/pkg/installpackage/mock_go_install.go +++ b/pkg/installpackage/mock_go_install.go @@ -8,6 +8,6 @@ type MockGoInstallInstaller struct { Err error } -func (m *MockGoInstallInstaller) Install(ctx context.Context, path, gobin string) error { +func (m *MockGoInstallInstaller) Install(ctx context.Context, path, gobin, goos string) error { return m.Err } diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index f4ddce9ec..138a8edc7 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -29,6 +29,10 @@ func (rt *Runtime) Env() string { return fmt.Sprintf("%s/%s", rt.GOOS, rt.GOARCH) } +func (rt *Runtime) IsWindows() bool { + return rt.GOOS == "windows" +} + func goos() string { if s := os.Getenv("AQUA_GOOS"); s != "" { return s diff --git a/pkg/slsa/exe_path.go b/pkg/slsa/exe_path.go index a24139112..1b1afd0c2 100644 --- a/pkg/slsa/exe_path.go +++ b/pkg/slsa/exe_path.go @@ -14,7 +14,7 @@ type ParamExePath struct { func ExePath(param *ParamExePath) string { assetName := fmt.Sprintf("slsa-verifier-%s-%s", param.Runtime.GOOS, param.Runtime.GOARCH) - if param.Runtime.GOOS == "windows" { + if param.Runtime.IsWindows() { assetName += ".exe" } return filepath.Join(param.RootDir, "pkgs", "github_release", "github.com", "slsa-framework", "slsa-verifier", Version, assetName, assetName) From e5a5b6b5995eea32e5170297114966ed0e080423 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Fri, 10 May 2024 10:21:47 +0900 Subject: [PATCH 2/2] fix: ignore AQUA_GOOS and AQUA_GOARCH in case of exec command --- pkg/cli/exec.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cli/exec.go b/pkg/cli/exec.go index 637ecad4e..43e70b791 100644 --- a/pkg/cli/exec.go +++ b/pkg/cli/exec.go @@ -8,6 +8,7 @@ import ( "github.com/aquaproj/aqua/v2/pkg/config" "github.com/aquaproj/aqua/v2/pkg/controller" + "github.com/aquaproj/aqua/v2/pkg/runtime" "github.com/urfave/cli/v2" ) @@ -53,7 +54,7 @@ func (r *Runner) execAction(c *cli.Context) error { if err := r.setParam(c, "exec", param); err != nil { return fmt.Errorf("parse the command line arguments: %w", err) } - ctrl := controller.InitializeExecCommandController(c.Context, param, http.DefaultClient, r.Runtime) + ctrl := controller.InitializeExecCommandController(c.Context, param, http.DefaultClient, runtime.NewR()) exeName, args, err := parseExecArgs(c.Args().Slice()) if err != nil { return err