Skip to content

Commit

Permalink
fix: redirect stdout to stderr (#3129)
Browse files Browse the repository at this point in the history
* fix: redirect stdout to stderr

* refactor: remove unused methods

* refactor: remove an unused method
  • Loading branch information
suzuki-shunsuke committed Sep 26, 2024
1 parent de6fc82 commit 2d1e865
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pkg/cosign/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (v *Verifier) Verify(ctx context.Context, logE *logrus.Entry, rt *runtime.R
}

type Executor interface {
ExecAndGetCombinedOutput(cmd *osexec.Cmd) (string, int, error)
ExecStderrAndGetCombinedOutput(cmd *osexec.Cmd) (string, int, error)
}

type ParamVerify struct {
Expand All @@ -129,7 +129,7 @@ func (v *Verifier) exec(ctx context.Context, args []string) (string, error) {
// https://github.com/aquaproj/aqua/issues/1555
mutex.Lock()
defer mutex.Unlock()
out, _, err := v.executor.ExecAndGetCombinedOutput(osexec.Command(ctx, v.cosignExePath, args...))
out, _, err := v.executor.ExecStderrAndGetCombinedOutput(osexec.Command(ctx, v.cosignExePath, args...))
return out, err //nolint:wrapcheck
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ghattestation/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type CommandExecutor interface {
Exec(cmd *osexec.Cmd) (int, error)
ExecStderr(cmd *osexec.Cmd) (int, error)
}

type Executor interface {
Expand Down Expand Up @@ -57,7 +57,7 @@ func wait(ctx context.Context, logE *logrus.Entry, retryCount int) error {
}

func (e *ExecutorImpl) exec(ctx context.Context, args []string) error {
_, err := e.executor.Exec(osexec.Command(ctx, e.exePath, args...))
_, err := e.executor.ExecStderr(osexec.Command(ctx, e.exePath, args...))
return err //nolint:wrapcheck
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/installpackage/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func getCargoArgs(version string, opts *registry.Cargo) []string {

func (is *CargoPackageInstallerImpl) Install(ctx context.Context, logE *logrus.Entry, crate, version, root string, opts *registry.Cargo) error {
args := getCargoArgs(version, opts)
if _, err := is.exec.Exec(osexec.Command(ctx, "cargo", append(args, "--root", root, crate)...)); err != nil {
if _, err := is.exec.ExecStderr(osexec.Command(ctx, "cargo", append(args, "--root", root, crate)...)); err != nil {
// Clean up root
logE := logE.WithField("install_dir", root)
logE.Info("removing the install directory because the installation failed")
Expand Down
2 changes: 1 addition & 1 deletion pkg/installpackage/go_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewGoBuildInstallerImpl(exec Executor) *GoBuildInstallerImpl {
func (is *GoBuildInstallerImpl) Install(ctx context.Context, exePath, exeDir, src string) error {
cmd := osexec.Command(ctx, "go", "build", "-o", exePath, src)
cmd.Dir = exeDir
if _, err := is.exec.Exec(cmd); err != nil {
if _, err := is.exec.ExecStderr(cmd); err != nil {
return fmt.Errorf("build a go package: %w", err)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/installpackage/go_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewGoInstallInstallerImpl(exec Executor) *GoInstallInstallerImpl {
func (is *GoInstallInstallerImpl) Install(ctx context.Context, path, gobin string) error {
cmd := osexec.Command(ctx, "go", "install", path)
cmd.Env = append(os.Environ(), "GOBIN="+gobin)
_, err := is.exec.Exec(cmd)
_, err := is.exec.ExecStderr(cmd)
if err != nil {
return fmt.Errorf("install a go package: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/installpackage/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

type Executor interface {
Exec(cmd *osexec.Cmd) (int, error)
ExecStderr(cmd *osexec.Cmd) (int, error)
ExecAndOutputWhenFailure(cmd *osexec.Cmd) (int, error)
}
4 changes: 2 additions & 2 deletions pkg/minisign/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type CommandExecutor interface {
Exec(cmd *osexec.Cmd) (int, error)
ExecStderr(cmd *osexec.Cmd) (int, error)
}

type Executor interface {
Expand Down Expand Up @@ -57,7 +57,7 @@ func wait(ctx context.Context, logE *logrus.Entry, retryCount int) error {
}

func (e *ExecutorImpl) exec(ctx context.Context, args []string) error {
_, err := e.executor.Exec(osexec.Command(ctx, e.minisignExePath, args...))
_, err := e.executor.ExecStderr(osexec.Command(ctx, e.minisignExePath, args...))
return err //nolint:wrapcheck
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/osexec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,20 @@ func (e *Executor) ExecAndOutputWhenFailure(cmd *exec.Cmd) (int, error) {
return 0, nil
}

func (e *Executor) ExecAndGetCombinedOutput(cmd *exec.Cmd) (string, int, error) {
func (e *Executor) execAndGetCombinedOutput(cmd *exec.Cmd) (string, int, error) {
out := &bytes.Buffer{}
cmd.Stdout = io.MultiWriter(cmd.Stdout, out)
cmd.Stderr = io.MultiWriter(cmd.Stderr, out)
code, err := e.Exec(cmd)
return out.String(), code, err
}

func (e *Executor) ExecStderrAndGetCombinedOutput(cmd *exec.Cmd) (string, int, error) {
cmd.Stdout = cmd.Stderr
return e.execAndGetCombinedOutput(cmd)
}

func (e *Executor) ExecStderr(cmd *exec.Cmd) (int, error) {
cmd.Stdout = cmd.Stderr
return e.Exec(cmd)
}
6 changes: 5 additions & 1 deletion pkg/osexec/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func (e *Mock) Exec(cmd *Cmd) (int, error) {
return e.ExitCode, e.Err
}

func (e *Mock) ExecStderr(cmd *Cmd) (int, error) {
return e.ExitCode, e.Err
}

func (e *Mock) ExecXSys(exePath string, args ...string) error {
return e.Err
}
Expand All @@ -18,6 +22,6 @@ func (e *Mock) ExecAndOutputWhenFailure(cmd *Cmd) (int, error) {
return e.ExitCode, e.Err
}

func (e *Mock) ExecAndGetCombinedOutput(cmd *Cmd) (string, int, error) {
func (e *Mock) ExecStderrAndGetCombinedOutput(cmd *Cmd) (string, int, error) {
return e.Output, e.ExitCode, e.Err
}
4 changes: 2 additions & 2 deletions pkg/slsa/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type CommandExecutor interface {
ExecAndGetCombinedOutput(cmd *osexec.Cmd) (string, int, error)
ExecStderrAndGetCombinedOutput(cmd *osexec.Cmd) (string, int, error)
}

type Executor interface {
Expand Down Expand Up @@ -56,7 +56,7 @@ func (e *ExecutorImpl) exec(ctx context.Context, args []string) (string, error)
mutex := cosign.GetMutex()
mutex.Lock()
defer mutex.Unlock()
out, _, err := e.executor.ExecAndGetCombinedOutput(osexec.Command(ctx, e.verifierExePath, args...))
out, _, err := e.executor.ExecStderrAndGetCombinedOutput(osexec.Command(ctx, e.verifierExePath, args...))
return out, err //nolint:wrapcheck
}

Expand Down
1 change: 0 additions & 1 deletion pkg/unarchive/dmg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type dmgUnarchiver struct {
}

type Executor interface {
Exec(cmd *osexec.Cmd) (int, error)
ExecAndOutputWhenFailure(cmd *osexec.Cmd) (int, error)
}

Expand Down

0 comments on commit 2d1e865

Please sign in to comment.