podman
42 строки · 1.1 Кб
1package docker
2
3import (
4"context"
5"encoding/json"
6"errors"
7"net/http"
8)
9
10// WaitContainer blocks until the given container stops, return the exit code
11// of the container status.
12//
13// See https://goo.gl/4AGweZ for more details.
14func (c *Client) WaitContainer(id string) (int, error) {
15return c.waitContainer(id, doOptions{})
16}
17
18// WaitContainerWithContext blocks until the given container stops, return the exit code
19// of the container status. The context object can be used to cancel the
20// inspect request.
21//
22// See https://goo.gl/4AGweZ for more details.
23func (c *Client) WaitContainerWithContext(id string, ctx context.Context) (int, error) {
24return c.waitContainer(id, doOptions{context: ctx})
25}
26
27func (c *Client) waitContainer(id string, opts doOptions) (int, error) {
28resp, err := c.do(http.MethodPost, "/containers/"+id+"/wait", opts)
29if err != nil {
30var e *Error
31if errors.As(err, &e) && e.Status == http.StatusNotFound {
32return 0, &NoSuchContainer{ID: id}
33}
34return 0, err
35}
36defer resp.Body.Close()
37var r struct{ StatusCode int }
38if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
39return 0, err
40}
41return r.StatusCode, nil
42}
43