From bff341cb0b2c9dc83a521e9ed663b2b27f97d16c Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 10 Jan 2024 11:41:53 -0500 Subject: [PATCH] Rename utils -> util Per style. --- bib/internal/setup/setup.go | 14 +++++++------- bib/internal/util/util.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 bib/internal/util/util.go diff --git a/bib/internal/setup/setup.go b/bib/internal/setup/setup.go index 08d00a0..944a64d 100644 --- a/bib/internal/setup/setup.go +++ b/bib/internal/setup/setup.go @@ -6,7 +6,7 @@ import ( "path/filepath" "github.com/osbuild/bootc-image-builder/bib/internal/podmanutil" - "github.com/osbuild/bootc-image-builder/bib/internal/utils" + "github.com/osbuild/bootc-image-builder/bib/internal/util" "golang.org/x/sys/unix" ) @@ -14,7 +14,7 @@ import ( // to run in a container environment. This function is idempotent. func EnsureEnvironment() error { osbuildPath := "/usr/bin/osbuild" - if utils.IsMountpoint(osbuildPath) { + if util.IsMountpoint(osbuildPath) { return nil } @@ -36,21 +36,21 @@ func EnsureEnvironment() error { if err := os.MkdirAll(runTmp, 0o755); err != nil { return err } - if !utils.IsMountpoint(runTmp) { - if err := utils.RunCmdSync("mount", "-t", "tmpfs", "tmpfs", runTmp); err != nil { + if !util.IsMountpoint(runTmp) { + if err := util.RunCmdSync("mount", "-t", "tmpfs", "tmpfs", runTmp); err != nil { return err } } destPath := filepath.Join(runTmp, "osbuild") - if err := utils.RunCmdSync("cp", "-p", "/usr/bin/osbuild", destPath); err != nil { + if err := util.RunCmdSync("cp", "-p", "/usr/bin/osbuild", destPath); err != nil { return err } - if err := utils.RunCmdSync("chcon", installType, destPath); err != nil { + if err := util.RunCmdSync("chcon", installType, destPath); err != nil { return err } // Create a bind mount into our target location; we can't copy it because // again we have to perserve the SELinux label. - if err := utils.RunCmdSync("mount", "--bind", destPath, osbuildPath); err != nil { + if err := util.RunCmdSync("mount", "--bind", destPath, osbuildPath); err != nil { return err } return nil diff --git a/bib/internal/util/util.go b/bib/internal/util/util.go new file mode 100644 index 0000000..8d32bde --- /dev/null +++ b/bib/internal/util/util.go @@ -0,0 +1,28 @@ +package util + +import ( + "fmt" + "os" + "os/exec" + "strings" + + "github.com/sirupsen/logrus" +) + +// IsMountpoint checks if the target path is a mount point +func IsMountpoint(path string) bool { + return exec.Command("mountpoint", path).Run() == nil +} + +// Synchronously invoke a command, propagating stdout and stderr +// to the current process's stdout and stderr +func RunCmdSync(cmdName string, args ...string) error { + logrus.Debugf("Running: %s %s", cmdName, strings.Join(args, " ")) + cmd := exec.Command(cmdName, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("error running %s %s: %w", cmdName, strings.Join(args, " "), err) + } + return nil +}