Refactor: About the RawInput process

Refactor the RawInput process of the `rm` and
`start` subcommands, like the other subcommands
such as `restart, stop, etc`.

[NO NEW TESTS NEEDED]

Signed-off-by: Toshiki Sonoda <sonoda.toshiki@fujitsu.com>
This commit is contained in:
Toshiki Sonoda
2022-08-23 09:58:34 +09:00
parent 51d4b88ce9
commit 716ac1c866
3 changed files with 27 additions and 20 deletions

View File

@ -149,7 +149,8 @@ func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit
return err return err
} }
for _, r := range responses { for _, r := range responses {
if r.Err != nil { switch {
case r.Err != nil:
if errors.Is(r.Err, define.ErrWillDeadlock) { if errors.Is(r.Err, define.ErrWillDeadlock) {
logrus.Errorf("Potential deadlock detected - please run 'podman system renumber' to resolve") logrus.Errorf("Potential deadlock detected - please run 'podman system renumber' to resolve")
} }
@ -160,8 +161,10 @@ func removeContainers(namesOrIDs []string, rmOptions entities.RmOptions, setExit
setExitCode(r.Err) setExitCode(r.Err)
} }
errs = append(errs, r.Err) errs = append(errs, r.Err)
} else { case r.RawInput != "":
fmt.Println(r.RawInput) fmt.Println(r.RawInput)
default:
fmt.Println(r.Id)
} }
} }
return errs.PrintErrors() return errs.PrintErrors()

View File

@ -381,7 +381,7 @@ func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string,
// this will fail and code will fall through to removing the container from libpod.` // this will fail and code will fall through to removing the container from libpod.`
tmpNames := []string{} tmpNames := []string{}
for _, ctr := range names { for _, ctr := range names {
report := reports.RmReport{Id: ctr, RawInput: ctr} report := reports.RmReport{Id: ctr}
report.Err = ic.Libpod.RemoveStorageContainer(ctr, options.Force) report.Err = ic.Libpod.RemoveStorageContainer(ctr, options.Force)
//nolint:gocritic //nolint:gocritic
if report.Err == nil { if report.Err == nil {
@ -938,13 +938,15 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
if err != nil { if err != nil {
return nil, err return nil, err
} }
idToRawInput := map[string]string{}
if len(rawInputs) == len(ctrs) {
for i := range ctrs {
idToRawInput[ctrs[i].ID()] = rawInputs[i]
}
}
// There can only be one container if attach was used // There can only be one container if attach was used
for i := range ctrs { for i := range ctrs {
ctr := ctrs[i] ctr := ctrs[i]
rawInput := ctr.ID()
if !options.All {
rawInput = rawInputs[i]
}
ctrState, err := ctr.State() ctrState, err := ctr.State()
if err != nil { if err != nil {
return nil, err return nil, err
@ -958,7 +960,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
// Exit cleanly immediately // Exit cleanly immediately
reports = append(reports, &entities.ContainerStartReport{ reports = append(reports, &entities.ContainerStartReport{
Id: ctr.ID(), Id: ctr.ID(),
RawInput: rawInput, RawInput: idToRawInput[ctr.ID()],
Err: nil, Err: nil,
ExitCode: 0, ExitCode: 0,
}) })
@ -969,7 +971,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
logrus.Debugf("Deadlock error: %v", err) logrus.Debugf("Deadlock error: %v", err)
reports = append(reports, &entities.ContainerStartReport{ reports = append(reports, &entities.ContainerStartReport{
Id: ctr.ID(), Id: ctr.ID(),
RawInput: rawInput, RawInput: idToRawInput[ctr.ID()],
Err: err, Err: err,
ExitCode: define.ExitCode(err), ExitCode: define.ExitCode(err),
}) })
@ -979,7 +981,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
if ctrRunning { if ctrRunning {
reports = append(reports, &entities.ContainerStartReport{ reports = append(reports, &entities.ContainerStartReport{
Id: ctr.ID(), Id: ctr.ID(),
RawInput: rawInput, RawInput: idToRawInput[ctr.ID()],
Err: nil, Err: nil,
ExitCode: 0, ExitCode: 0,
}) })
@ -989,7 +991,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
if err != nil { if err != nil {
reports = append(reports, &entities.ContainerStartReport{ reports = append(reports, &entities.ContainerStartReport{
Id: ctr.ID(), Id: ctr.ID(),
RawInput: rawInput, RawInput: idToRawInput[ctr.ID()],
Err: err, Err: err,
ExitCode: exitCode, ExitCode: exitCode,
}) })
@ -1004,7 +1006,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
exitCode = ic.GetContainerExitCode(ctx, ctr) exitCode = ic.GetContainerExitCode(ctx, ctr)
reports = append(reports, &entities.ContainerStartReport{ reports = append(reports, &entities.ContainerStartReport{
Id: ctr.ID(), Id: ctr.ID(),
RawInput: rawInput, RawInput: idToRawInput[ctr.ID()],
Err: err, Err: err,
ExitCode: exitCode, ExitCode: exitCode,
}) })
@ -1017,7 +1019,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
// If the container is in a pod, also set to recursively start dependencies // If the container is in a pod, also set to recursively start dependencies
report := &entities.ContainerStartReport{ report := &entities.ContainerStartReport{
Id: ctr.ID(), Id: ctr.ID(),
RawInput: rawInput, RawInput: idToRawInput[ctr.ID()],
ExitCode: 125, ExitCode: 125,
} }
if err := ctr.Start(ctx, true); err != nil { if err := ctr.Start(ctx, true); err != nil {

View File

@ -672,10 +672,16 @@ func logIfRmError(id string, err error, reports []*reports.RmReport) {
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) { func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
reports := []*entities.ContainerStartReport{} reports := []*entities.ContainerStartReport{}
var exitCode = define.ExecErrorCodeGeneric var exitCode = define.ExecErrorCodeGeneric
ctrs, namesOrIds, err := getContainersAndInputByContext(ic.ClientCtx, options.All, false, namesOrIds, options.Filters) ctrs, rawInputs, err := getContainersAndInputByContext(ic.ClientCtx, options.All, false, namesOrIds, options.Filters)
if err != nil { if err != nil {
return nil, err return nil, err
} }
idToRawInput := map[string]string{}
if len(rawInputs) == len(ctrs) {
for i := range ctrs {
idToRawInput[ctrs[i].ID] = rawInputs[i]
}
}
removeOptions := new(containers.RemoveOptions).WithVolumes(true).WithForce(false) removeOptions := new(containers.RemoveOptions).WithVolumes(true).WithForce(false)
removeContainer := func(id string) { removeContainer := func(id string) {
reports, err := containers.Remove(ic.ClientCtx, id, removeOptions) reports, err := containers.Remove(ic.ClientCtx, id, removeOptions)
@ -683,15 +689,11 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
} }
// There can only be one container if attach was used // There can only be one container if attach was used
for i, ctr := range ctrs { for _, ctr := range ctrs {
name := ctr.ID name := ctr.ID
rawInput := ctr.ID
if !options.All {
rawInput = namesOrIds[i]
}
report := entities.ContainerStartReport{ report := entities.ContainerStartReport{
Id: name, Id: name,
RawInput: rawInput, RawInput: idToRawInput[name],
ExitCode: exitCode, ExitCode: exitCode,
} }
ctrRunning := ctr.State == define.ContainerStateRunning.String() ctrRunning := ctr.State == define.ContainerStateRunning.String()