mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Make better default usage on context.Context on the `api/client` package to share the context (it is useless if not shared, which was the case for a lot of commands). Signed-off-by: Vincent Demeester <vincent@sbr.pm>
		
			
				
	
	
		
			68 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package client
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"io"
 | 
						|
 | 
						|
	"golang.org/x/net/context"
 | 
						|
 | 
						|
	Cli "github.com/docker/docker/cli"
 | 
						|
	flag "github.com/docker/docker/pkg/mflag"
 | 
						|
	"github.com/docker/docker/pkg/stdcopy"
 | 
						|
	"github.com/docker/engine-api/types"
 | 
						|
)
 | 
						|
 | 
						|
var validDrivers = map[string]bool{
 | 
						|
	"json-file": true,
 | 
						|
	"journald":  true,
 | 
						|
}
 | 
						|
 | 
						|
// CmdLogs fetches the logs of a given container.
 | 
						|
//
 | 
						|
// docker logs [OPTIONS] CONTAINER
 | 
						|
func (cli *DockerCli) CmdLogs(args ...string) error {
 | 
						|
	cmd := Cli.Subcmd("logs", []string{"CONTAINER"}, Cli.DockerCommands["logs"].Description, true)
 | 
						|
	follow := cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
 | 
						|
	since := cmd.String([]string{"-since"}, "", "Show logs since timestamp")
 | 
						|
	times := cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
 | 
						|
	details := cmd.Bool([]string{"-details"}, false, "Show extra details provided to logs")
 | 
						|
	tail := cmd.String([]string{"-tail"}, "all", "Number of lines to show from the end of the logs")
 | 
						|
	cmd.Require(flag.Exact, 1)
 | 
						|
 | 
						|
	cmd.ParseFlags(args, true)
 | 
						|
 | 
						|
	name := cmd.Arg(0)
 | 
						|
 | 
						|
	ctx := context.Background()
 | 
						|
 | 
						|
	c, err := cli.client.ContainerInspect(ctx, name)
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
 | 
						|
	if !validDrivers[c.HostConfig.LogConfig.Type] {
 | 
						|
		return fmt.Errorf("\"logs\" command is supported only for \"json-file\" and \"journald\" logging drivers (got: %s)", c.HostConfig.LogConfig.Type)
 | 
						|
	}
 | 
						|
 | 
						|
	options := types.ContainerLogsOptions{
 | 
						|
		ShowStdout: true,
 | 
						|
		ShowStderr: true,
 | 
						|
		Since:      *since,
 | 
						|
		Timestamps: *times,
 | 
						|
		Follow:     *follow,
 | 
						|
		Tail:       *tail,
 | 
						|
		Details:    *details,
 | 
						|
	}
 | 
						|
	responseBody, err := cli.client.ContainerLogs(ctx, name, options)
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
	defer responseBody.Close()
 | 
						|
 | 
						|
	if c.Config.Tty {
 | 
						|
		_, err = io.Copy(cli.out, responseBody)
 | 
						|
	} else {
 | 
						|
		_, err = stdcopy.StdCopy(cli.out, cli.err, responseBody)
 | 
						|
	}
 | 
						|
	return err
 | 
						|
}
 |