Skip to content

Commit

Permalink
Add stdin parameter to RunCmdStdMapping()
Browse files Browse the repository at this point in the history
In some cases we will need to pass a string to stdin.
  • Loading branch information
cbosdo committed Jul 12, 2024
1 parent a9fc1b3 commit 3a174d3
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mgradm/cmd/install/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func installForPodman(

if path, err := exec.LookPath("uyuni-payg-extract-data"); err == nil {
// the binary is installed
err = utils.RunCmdStdMapping(zerolog.DebugLevel, path)
err = utils.RunCmdStdMapping(zerolog.DebugLevel, "", path)
if err != nil {
return utils.Errorf(err, L("failed to extract payg data"))
}
Expand Down
2 changes: 1 addition & 1 deletion mgradm/shared/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func CallCloudGuestRegistryAuth() error {
path, err := exec.LookPath(cloudguestregistryauth)
if err == nil {
// the binary is installed
return utils.RunCmdStdMapping(zerolog.DebugLevel, path)
return utils.RunCmdStdMapping(zerolog.DebugLevel, "", path)
}
// silently ignore error if it is missing
return nil
Expand Down
2 changes: 1 addition & 1 deletion mgrpxy/cmd/logs/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func kubernetesLogs(
commandArgs = append(commandArgs, args...)
}

return utils.RunCmdStdMapping(zerolog.DebugLevel, "kubectl", commandArgs...)
return utils.RunCmdStdMapping(zerolog.DebugLevel, "", "kubectl", commandArgs...)
}

func isRFC3339(timestamp string) bool {
Expand Down
2 changes: 1 addition & 1 deletion mgrpxy/cmd/logs/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func podmanLogs(
commandArgs = append(commandArgs, args...)
}

return utils.RunCmdStdMapping(zerolog.DebugLevel, "podman", commandArgs...)
return utils.RunCmdStdMapping(zerolog.DebugLevel, "", "podman", commandArgs...)
}
6 changes: 3 additions & 3 deletions shared/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (c *Connection) Copy(src string, dst string, user string, group string) err
return fmt.Errorf(L("unknown container kind: %s"), command)
}

if err := utils.RunCmdStdMapping(zerolog.DebugLevel, command, commandArgs...); err != nil {
if err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", command, commandArgs...); err != nil {
return err
}

Expand All @@ -241,7 +241,7 @@ func (c *Connection) Copy(src string, dst string, user string, group string) err
owner = user + ":" + group
}
execArgs = append(execArgs, "chown", owner, strings.Replace(dst, "server:", "", 1))
return utils.RunCmdStdMapping(zerolog.DebugLevel, command, execArgs...)
return utils.RunCmdStdMapping(zerolog.DebugLevel, "", command, execArgs...)
}
return nil
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *Connection) CopyCaCertificate(fqdn string) error {
}

log.Info().Msg(L("Updating host trusted certificates"))
return utils.RunCmdStdMapping(zerolog.DebugLevel, "update-ca-certificates")
return utils.RunCmdStdMapping(zerolog.DebugLevel, "", "update-ca-certificates")
}

// ChoosePodmanOrKubernetes selects either the podman or the kubernetes function based on the backend.
Expand Down
2 changes: 1 addition & 1 deletion shared/kubernetes/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func HelmUpgrade(kubeconfig string, namespace string, install bool,
if install {
command = "install"
}
if err := utils.RunCmdStdMapping(zerolog.DebugLevel, "helm", helmArgs...); err != nil {
if err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", "helm", helmArgs...); err != nil {
// TODO We cannot use the command variable in the message as that would break localization
if command == "upgrade" {
return utils.Errorf(err, L("failed to upgrade helm chart %[1]s in namespace %[2]s"), chart, namespace)
Expand Down
2 changes: 1 addition & 1 deletion shared/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func RunPod(podname string, filter string, image string, pullPolicy string, comm
}

arguments = append(arguments, "--command", "--", command)
err := utils.RunCmdStdMapping(zerolog.DebugLevel, "kubectl", arguments...)
err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", "kubectl", arguments...)
if err != nil {
return utils.Errorf(err, PL("The first placeholder is a command",
"cannot run %[1]s using image %[2]s"), command, image)
Expand Down
2 changes: 1 addition & 1 deletion shared/podman/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func pullImage(image string, args ...string) error {
log.Debug().Msg("Additional arguments for pull command will not be shown.")
}

return utils.RunCmdStdMapping(loglevel, "podman", podmanArgs...)
return utils.RunCmdStdMapping(loglevel, "", "podman", podmanArgs...)
}

// ShowAvailableTag returns the list of available tag for a given image.
Expand Down
2 changes: 1 addition & 1 deletion shared/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func RunContainer(name string, image string, volumes []types.VolumeMount, extraA
podmanArgs = append(podmanArgs, image)
podmanArgs = append(podmanArgs, cmd...)

err := utils.RunCmdStdMapping(zerolog.DebugLevel, "podman", podmanArgs...)
err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", "podman", podmanArgs...)
if err != nil {
return utils.Errorf(err, L("failed to run %s container"), name)
}
Expand Down
10 changes: 7 additions & 3 deletions shared/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package utils

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -45,15 +46,18 @@ func RunCmd(command string, args ...string) error {
}

// RunCmdStdMapping execute a shell command mapping the stdout and stderr.
func RunCmdStdMapping(logLevel zerolog.Level, command string, args ...string) error {
func RunCmdStdMapping(logLevel zerolog.Level, stdin string, command string, args ...string) error {
localLogger := log.Level(logLevel)
localLogger.Debug().Msgf("Running: %s %s", command, strings.Join(args, " "))

runCmd := exec.Command(command, args...)
if stdin != "" {
runCmd.Stdin = bytes.NewBuffer([]byte(stdin))
}

runCmd.Stdout = os.Stdout
runCmd.Stderr = os.Stderr
err := runCmd.Run()
return err
return runCmd.Run()
}

// RunCmdOutput execute a shell command and collects output.
Expand Down
2 changes: 1 addition & 1 deletion shared/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func InspectHost(serverHost bool) (*HostInspectData, error) {
return nil, err
}

if err := RunCmdStdMapping(zerolog.DebugLevel, inspector.GetScriptPath()); err != nil {
if err := RunCmdStdMapping(zerolog.DebugLevel, "", inspector.GetScriptPath()); err != nil {
return nil, Errorf(err, L("failed to run inspect script in host system"))
}

Expand Down

0 comments on commit 3a174d3

Please sign in to comment.