Skip to content

Commit

Permalink
bib: simplify getContainerSize()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvo5 authored and achilleas-k committed Apr 19, 2024
1 parent c102e26 commit 2eef394
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
23 changes: 11 additions & 12 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"strconv"
"strings"

"github.com/osbuild/bootc-image-builder/bib/internal/setup"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"

"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/cloud/awscloud"
Expand All @@ -21,9 +24,9 @@ import (
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"

"github.com/osbuild/bootc-image-builder/bib/internal/setup"
"github.com/osbuild/bootc-image-builder/bib/internal/util"
)

//go:embed fedora-eln.json
Expand Down Expand Up @@ -113,15 +116,11 @@ func loadConfig(path string) (*BuildConfig, error) {

// getContainerSize returns the size of an already pulled container image in bytes
func getContainerSize(imgref string) (uint64, error) {
var stdout, stderr strings.Builder

cmd := exec.Command("podman", "image", "inspect", imgref, "--format", "{{.Size}}")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return 0, fmt.Errorf("failed inspect image: %w:\n%s", err, stderr.String())
output, err := exec.Command("podman", "image", "inspect", imgref, "--format", "{{.Size}}").Output()
if err != nil {
return 0, fmt.Errorf("failed inspect image: %w", util.OutputErr(err))
}
size, err := strconv.ParseUint(strings.TrimSpace(stdout.String()), 10, 64)
size, err := strconv.ParseUint(strings.TrimSpace(string(output)), 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse image size: %w", err)
}
Expand Down
9 changes: 9 additions & 0 deletions bib/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 21 additions & 0 deletions bib/internal/util/util_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 2eef394

Please sign in to comment.