Rename utils -> util

Per style.
This commit is contained in:
Colin Walters 2024-01-10 11:41:53 -05:00 committed by Simon de Vlieger
parent 3c717fde11
commit bff341cb0b
2 changed files with 35 additions and 7 deletions

View file

@ -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

28
bib/internal/util/util.go Normal file
View file

@ -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
}