newLogsCommand creates a new cobra.Command for "docker container logs"
(dockerCLI command.Cli)
| 25 | |
| 26 | // newLogsCommand creates a new cobra.Command for "docker container logs" |
| 27 | func newLogsCommand(dockerCLI command.Cli) *cobra.Command { |
| 28 | var opts logsOptions |
| 29 | |
| 30 | cmd := &cobra.Command{ |
| 31 | Use: "logs [OPTIONS] CONTAINER", |
| 32 | Short: "Fetch the logs of a container", |
| 33 | Args: cli.ExactArgs(1), |
| 34 | RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | opts.container = args[0] |
| 36 | return runLogs(cmd.Context(), dockerCLI, &opts) |
| 37 | }, |
| 38 | Annotations: map[string]string{ |
| 39 | "aliases": "docker container logs, docker logs", |
| 40 | }, |
| 41 | ValidArgsFunction: completion.ContainerNames(dockerCLI, true), |
| 42 | DisableFlagsInUseLine: true, |
| 43 | } |
| 44 | |
| 45 | flags := cmd.Flags() |
| 46 | flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output") |
| 47 | flags.StringVar(&opts.since, "since", "", `Show logs since timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m" for 42 minutes)`) |
| 48 | flags.StringVar(&opts.until, "until", "", `Show logs before a timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m" for 42 minutes)`) |
| 49 | flags.SetAnnotation("until", "version", []string{"1.35"}) |
| 50 | flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps") |
| 51 | flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs") |
| 52 | flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs") |
| 53 | return cmd |
| 54 | } |
| 55 | |
| 56 | func runLogs(ctx context.Context, dockerCli command.Cli, opts *logsOptions) error { |
| 57 | c, err := dockerCli.Client().ContainerInspect(ctx, opts.container, client.ContainerInspectOptions{}) |
no test coverage detected
searching dependent graphs…