github.com/akashshinde/docker@v1.9.1/daemon/attach.go (about) 1 package daemon 2 3 import ( 4 "io" 5 6 "github.com/docker/docker/pkg/stdcopy" 7 ) 8 9 // ContainerAttachWithLogsConfig holds the streams to use when connecting to a container to view logs. 10 type ContainerAttachWithLogsConfig struct { 11 InStream io.ReadCloser 12 OutStream io.Writer 13 UseStdin, UseStdout, UseStderr bool 14 Logs, Stream bool 15 } 16 17 // ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig. 18 func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error { 19 container, err := daemon.Get(prefixOrName) 20 if err != nil { 21 return err 22 } 23 24 var errStream io.Writer 25 26 if !container.Config.Tty { 27 errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr) 28 c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout) 29 } else { 30 errStream = c.OutStream 31 } 32 33 var stdin io.ReadCloser 34 var stdout, stderr io.Writer 35 36 if c.UseStdin { 37 stdin = c.InStream 38 } 39 if c.UseStdout { 40 stdout = c.OutStream 41 } 42 if c.UseStderr { 43 stderr = errStream 44 } 45 46 return container.attachWithLogs(stdin, stdout, stderr, c.Logs, c.Stream) 47 } 48 49 // ContainerWsAttachWithLogsConfig attach with websockets, since all 50 // stream data is delegated to the websocket to handle there. 51 type ContainerWsAttachWithLogsConfig struct { 52 InStream io.ReadCloser 53 OutStream, ErrStream io.Writer 54 Logs, Stream bool 55 } 56 57 // ContainerWsAttachWithLogs websocket connection 58 func (daemon *Daemon) ContainerWsAttachWithLogs(prefixOrName string, c *ContainerWsAttachWithLogsConfig) error { 59 container, err := daemon.Get(prefixOrName) 60 if err != nil { 61 return err 62 } 63 return container.attachWithLogs(c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream) 64 }