podman
74 строки · 2.0 Кб
1package docker
2
3import (
4"io"
5"net/http"
6)
7
8// AttachToContainerOptions is the set of options that can be used when
9// attaching to a container.
10//
11// See https://goo.gl/JF10Zk for more details.
12type AttachToContainerOptions struct {
13Container string `qs:"-"`
14InputStream io.Reader `qs:"-"`
15OutputStream io.Writer `qs:"-"`
16ErrorStream io.Writer `qs:"-"`
17
18// If set, after a successful connect, a sentinel will be sent and then the
19// client will block on receive before continuing.
20//
21// It must be an unbuffered channel. Using a buffered channel can lead
22// to unexpected behavior.
23Success chan struct{}
24
25// Override the key sequence for detaching a container.
26DetachKeys string
27
28// Use raw terminal? Usually true when the container contains a TTY.
29RawTerminal bool `qs:"-"`
30
31// Get container logs, sending it to OutputStream.
32Logs bool
33
34// Stream the response?
35Stream bool
36
37// Attach to stdin, and use InputStream.
38Stdin bool
39
40// Attach to stdout, and use OutputStream.
41Stdout bool
42
43// Attach to stderr, and use ErrorStream.
44Stderr bool
45}
46
47// AttachToContainer attaches to a container, using the given options.
48//
49// See https://goo.gl/JF10Zk for more details.
50func (c *Client) AttachToContainer(opts AttachToContainerOptions) error {
51cw, err := c.AttachToContainerNonBlocking(opts)
52if err != nil {
53return err
54}
55return cw.Wait()
56}
57
58// AttachToContainerNonBlocking attaches to a container, using the given options.
59// This function does not block.
60//
61// See https://goo.gl/NKpkFk for more details.
62func (c *Client) AttachToContainerNonBlocking(opts AttachToContainerOptions) (CloseWaiter, error) {
63if opts.Container == "" {
64return nil, &NoSuchContainer{ID: opts.Container}
65}
66path := "/containers/" + opts.Container + "/attach?" + queryString(opts)
67return c.hijack(http.MethodPost, path, hijackOptions{
68success: opts.Success,
69setRawTerminal: opts.RawTerminal,
70in: opts.InputStream,
71stdout: opts.OutputStream,
72stderr: opts.ErrorStream,
73})
74}
75