2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"io"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type logsOptions struct {
|
|
|
|
follow bool
|
|
|
|
since string
|
2017-05-10 08:00:00 -04:00
|
|
|
until string
|
2016-09-08 13:11:39 -04:00
|
|
|
timestamps bool
|
|
|
|
details bool
|
|
|
|
tail string
|
|
|
|
|
|
|
|
container string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogsCommand creates a new cobra.Command for `docker logs`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewLogsCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
var opts logsOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "logs [OPTIONS] CONTAINER",
|
|
|
|
Short: "Fetch the logs of a container",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.container = args[0]
|
|
|
|
return runLogs(dockerCli, &opts)
|
|
|
|
},
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker container logs, docker logs",
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.ContainerNames(dockerCli, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
|
2020-05-28 07:56:07 -04:00
|
|
|
flags.StringVar(&opts.since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)")
|
|
|
|
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)")
|
2017-05-10 08:00:00 -04:00
|
|
|
flags.SetAnnotation("until", "version", []string{"1.35"})
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
|
|
|
|
flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
|
2020-07-23 07:54:12 -04:00
|
|
|
flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs")
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runLogs(dockerCli command.Cli, opts *logsOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-10-22 06:53:22 -04:00
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
options := types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: opts.since,
|
2017-05-10 08:00:00 -04:00
|
|
|
Until: opts.until,
|
2016-09-08 13:11:39 -04:00
|
|
|
Timestamps: opts.timestamps,
|
|
|
|
Follow: opts.follow,
|
|
|
|
Tail: opts.tail,
|
|
|
|
Details: opts.details,
|
|
|
|
}
|
2018-10-22 06:53:22 -04:00
|
|
|
responseBody, err := dockerCli.Client().ContainerLogs(ctx, c.ID, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
|
|
|
if c.Config.Tty {
|
|
|
|
_, err = io.Copy(dockerCli.Out(), responseBody)
|
|
|
|
} else {
|
|
|
|
_, err = stdcopy.StdCopy(dockerCli.Out(), dockerCli.Err(), responseBody)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|