From f21c1d238da393cb11c75e408a736336b979e6d5 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Thu, 28 Sep 2023 15:41:19 +0200 Subject: [PATCH] podman: run --replace prints only the new container id print only the new container ID when using --replace instead of the terminated container ID if it was stopped. Closes: https://github.com/containers/podman/issues/20185 Signed-off-by: Giuseppe Scrivano --- cmd/podman/containers/create.go | 2 +- cmd/podman/containers/rm.go | 12 ++++++++---- test/e2e/run_test.go | 5 ++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/podman/containers/create.go b/cmd/podman/containers/create.go index ef58af06a3..2da313c68a 100644 --- a/cmd/podman/containers/create.go +++ b/cmd/podman/containers/create.go @@ -203,7 +203,7 @@ func replaceContainer(name string) error { Force: true, // force stop & removal Ignore: true, // ignore errors when a container doesn't exit } - return removeContainers([]string{name}, rmOptions, false) + return removeContainers([]string{name}, rmOptions, false, true) } func createOrUpdateFlags(cmd *cobra.Command, vals *entities.ContainerCreateOptions) error { diff --git a/cmd/podman/containers/rm.go b/cmd/podman/containers/rm.go index 705ac55082..a29233cf3c 100644 --- a/cmd/podman/containers/rm.go +++ b/cmd/podman/containers/rm.go @@ -133,14 +133,14 @@ func rm(cmd *cobra.Command, args []string) error { rmOptions.Depend = true } - return removeContainers(utils.RemoveSlash(args), rmOptions, true) + return removeContainers(utils.RemoveSlash(args), rmOptions, true, false) } // removeContainers will remove the specified containers (names or IDs). // Allows for sharing removal logic across commands. If setExit is set, // removeContainers will set the exit code according to the `podman-rm` man // page. -func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit bool) error { +func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit bool, quiet bool) error { var errs utils.OutputErrors responses, err := registry.ContainerEngine().ContainerRm(context.Background(), namesOrIDs, rmOptions) if err != nil { @@ -166,9 +166,13 @@ func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit } errs = append(errs, r.Err) case r.RawInput != "": - fmt.Println(r.RawInput) + if !quiet { + fmt.Println(r.RawInput) + } default: - fmt.Println(r.Id) + if !quiet { + fmt.Println(r.Id) + } } } return errs.PrintErrors() diff --git a/test/e2e/run_test.go b/test/e2e/run_test.go index 0b65122c9c..423c2e44c4 100644 --- a/test/e2e/run_test.go +++ b/test/e2e/run_test.go @@ -1615,9 +1615,12 @@ VOLUME %s`, ALPINE, volPath, volPath) // Run and replace 5 times in a row the "same" container. ctrName := "testCtr" for i := 0; i < 5; i++ { - session := podmanTest.Podman([]string{"run", "--detach", "--replace", "--name", ctrName, ALPINE, "/bin/sh"}) + session := podmanTest.Podman([]string{"run", "--detach", "--replace", "--name", ctrName, ALPINE, "top"}) session.WaitWithDefaultTimeout() Expect(session).Should(ExitCleanly()) + + // make sure Podman prints only one ID + Expect(session.OutputToString()).To(HaveLen(64)) } })