Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored container wait and make it public #2384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions pkg/cmd/container/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package container
import (
"context"
"fmt"
"io"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/api/types"
Expand Down Expand Up @@ -49,29 +48,27 @@ func Wait(ctx context.Context, client *containerd.Client, reqs []string, options
var allErr error
w := options.Stdout
for _, container := range containers {
if waitErr := waitContainer(ctx, w, container); waitErr != nil {
code, waitErr := WaitContainer(ctx, container)
fmt.Fprintln(w, code)
if waitErr != nil {
allErr = multierror.Append(allErr, waitErr)
}
}
return allErr
}

func waitContainer(ctx context.Context, w io.Writer, container containerd.Container) error {
func WaitContainer(ctx context.Context, container containerd.Container) (code int64, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@ningziwen ningziwen Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to keep the pattern consistent with other recent refactoring, instead of making methods public, we can also move stdout to cmd from pkg.
#2333
#2342

task, err := container.Task(ctx, nil)
if err != nil {
return err
return -1, err
}

statusC, err := task.Wait(ctx)
if err != nil {
return err
return -1, err
}

status := <-statusC
code, _, err := status.Result()
if err != nil {
return err
}
fmt.Fprintln(w, code)
return nil
rawcode, _, err := status.Result()
return int64(rawcode), err
}