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 <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2023-09-28 15:41:19 +02:00
parent 35824b647c
commit f21c1d238d
3 changed files with 13 additions and 6 deletions

View File

@@ -203,7 +203,7 @@ func replaceContainer(name string) error {
Force: true, // force stop & removal Force: true, // force stop & removal
Ignore: true, // ignore errors when a container doesn't exit 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 { func createOrUpdateFlags(cmd *cobra.Command, vals *entities.ContainerCreateOptions) error {

View File

@@ -133,14 +133,14 @@ func rm(cmd *cobra.Command, args []string) error {
rmOptions.Depend = true 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). // removeContainers will remove the specified containers (names or IDs).
// Allows for sharing removal logic across commands. If setExit is set, // Allows for sharing removal logic across commands. If setExit is set,
// removeContainers will set the exit code according to the `podman-rm` man // removeContainers will set the exit code according to the `podman-rm` man
// page. // 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 var errs utils.OutputErrors
responses, err := registry.ContainerEngine().ContainerRm(context.Background(), namesOrIDs, rmOptions) responses, err := registry.ContainerEngine().ContainerRm(context.Background(), namesOrIDs, rmOptions)
if err != nil { if err != nil {
@@ -166,11 +166,15 @@ func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit
} }
errs = append(errs, r.Err) errs = append(errs, r.Err)
case r.RawInput != "": case r.RawInput != "":
if !quiet {
fmt.Println(r.RawInput) fmt.Println(r.RawInput)
}
default: default:
if !quiet {
fmt.Println(r.Id) fmt.Println(r.Id)
} }
} }
}
return errs.PrintErrors() return errs.PrintErrors()
} }

View File

@@ -1615,9 +1615,12 @@ VOLUME %s`, ALPINE, volPath, volPath)
// Run and replace 5 times in a row the "same" container. // Run and replace 5 times in a row the "same" container.
ctrName := "testCtr" ctrName := "testCtr"
for i := 0; i < 5; i++ { 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() session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly()) Expect(session).Should(ExitCleanly())
// make sure Podman prints only one ID
Expect(session.OutputToString()).To(HaveLen(64))
} }
}) })