bib: simplify getContainerSize()
The getContainerSize() was not using some of the modern go helpers. So let's use `exec.Command().Output()` and introduce a new `util.OutputErr()` helper that will be able to also show stderr to the user if the Output() call returns an error.
This commit is contained in:
parent
9b280f0ba3
commit
b3ef264353
2 changed files with 30 additions and 0 deletions
|
|
@ -26,3 +26,12 @@ func RunCmdSync(cmdName string, args ...string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// OutputErr takes an error from exec.Command().Output() and tries
|
||||
// generate an error with stderr details
|
||||
func OutputErr(err error) error {
|
||||
if err, ok := err.(*exec.ExitError); ok {
|
||||
return fmt.Errorf("%w, stderr:\n%s", err, err.Stderr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
21
bib/internal/util/util_test.go
Normal file
21
bib/internal/util/util_test.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package util_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/osbuild/bootc-image-builder/bib/internal/util"
|
||||
)
|
||||
|
||||
func TestOutputErrPassthrough(t *testing.T) {
|
||||
err := fmt.Errorf("boom")
|
||||
assert.Equal(t, util.OutputErr(err), err)
|
||||
}
|
||||
|
||||
func TestOutputErrExecError(t *testing.T) {
|
||||
_, err := exec.Command("bash", "-c", ">&2 echo some-stderr; exit 1").Output()
|
||||
assert.Equal(t, "exit status 1, stderr:\nsome-stderr\n", util.OutputErr(err).Error())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue