Skip to content

Commit

Permalink
Further DRYing
Browse files Browse the repository at this point in the history
  • Loading branch information
webbnh committed Apr 26, 2024
1 parent d51d2a6 commit 0697645
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions internal/cliwrapper/cliwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@ func (p *cliWrapper) decorateImageName(image string) string {
}

func (p *cliWrapper) ImageExists(image string) (*bool, error) {
cmd := p.getPodmanCmd("image", "ls", "--format", "{{.Repository}}:{{.Tag}}")
var out bytes.Buffer
var errOut bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errOut
p.logger.Debugf("Checking whether image exists with command %v", cmd.Args)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf(
"error while determining if image exists. Stdout: '%s', Stderr: '%s', Cmd error: (%w)",
out.String(), errOut.String(), err)
outStr, err := p.runPodmanCmd(
"checking whether image exists",
"image", "ls", "--format", "{{.Repository}}:{{.Tag}}",
)
if err != nil {
return nil, err
}
outStr := out.String()
outSlice := strings.Split(outStr, "\n")
exists := util.SliceContains(outSlice, p.decorateImageName(image))
return &exists, nil
Expand All @@ -62,18 +57,8 @@ func (p *cliWrapper) PullImage(image string, platform *string) error {
commandArgs = append(commandArgs, "--platform", *platform)
}
commandArgs = append(commandArgs, p.decorateImageName(image))
cmd := p.getPodmanCmd(commandArgs...)
p.logger.Debugf("Pulling image with command %v", cmd.Args)
var out bytes.Buffer
var errOut bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errOut
if err := cmd.Run(); err != nil {
return fmt.Errorf(
"error while pulling image. Stdout: '%s', Stderr: '%s', Cmd error: (%w)",
out.String(), errOut.String(), err)
}
return nil
_, err := p.runPodmanCmd("pulling image", commandArgs...)
return err
}

func (p *cliWrapper) Deploy(image string, podmanArgs []string, containerArgs []string) (io.WriteCloser, io.ReadCloser, error) {
Expand Down Expand Up @@ -104,12 +89,10 @@ func (p *cliWrapper) KillAndClean(containerName string) error {
p.logger.Warningf("successfully killed container %s", containerName)
}

var cmdRmContainerStderr bytes.Buffer
cmdRmContainer := p.getPodmanCmd("rm", "--force", containerName)
p.logger.Debugf("Removing container with command %v", cmdRmContainer.Args)
cmdRmContainer.Stderr = &cmdRmContainerStderr
if err := cmdRmContainer.Run(); err != nil {
p.logger.Errorf("failed to remove container %s: %s", containerName, cmdRmContainerStderr.String())
msg := "removing container " + containerName
_, err := p.runPodmanCmd(msg, "rm", "--force", containerName)
if err != nil {
p.logger.Errorf(err.Error())
} else {
p.logger.Infof("successfully removed container %s", containerName)
}
Expand All @@ -120,3 +103,19 @@ func (p *cliWrapper) getPodmanCmd(cmdArgs ...string) *exec.Cmd {
commandArgs := append(p.runtimeContext, cmdArgs...)

Check failure on line 103 in internal/cliwrapper/cliwrapper.go

View workflow job for this annotation

GitHub Actions / golangci-lint

appendAssign: append result not assigned to the same slice (gocritic)
return exec.Command(p.podmanFullPath, commandArgs...) // FIXME: needs nolint:gosec or similar

Check failure on line 104 in internal/cliwrapper/cliwrapper.go

View workflow job for this annotation

GitHub Actions / golangci-lint

internal/cliwrapper/cliwrapper.go:104: Line contains TODO/BUG/FIXME: "FIXME: needs nolint:gosec or similar" (godox)
}

func (p *cliWrapper) runPodmanCmd(msg string, cmdArgs ...string) (string, error) {
var out bytes.Buffer
var errOut bytes.Buffer

cmd := p.getPodmanCmd(cmdArgs...)
cmd.Stdout = &out
cmd.Stderr = &errOut
p.logger.Debugf(msg+" with command %v", cmd.Args)
if err := cmd.Run(); err != nil {
return "", fmt.Errorf(
"error while %s. Stdout: '%s', Stderr: '%s', Cmd error: (%w)",
msg, out.String(), errOut.String(), err)
}
return out.String(), nil
}

0 comments on commit 0697645

Please sign in to comment.