From 750553c86629a020e7aa7a170fded7b6cbc29c9b Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Mon, 6 Feb 2023 21:21:29 +1100 Subject: [PATCH] introduce compose logs --index to select a replica container Signed-off-by: Amit Saha Signed-off-by: Nicolas De Loof --- cmd/compose/logs.go | 10 +++++++++- docs/reference/compose_logs.md | 2 +- docs/reference/docker_compose_logs.yaml | 5 ++--- pkg/api/api.go | 1 + pkg/compose/logs.go | 19 ++++++++++++++----- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/cmd/compose/logs.go b/cmd/compose/logs.go index 3df3f5a69..2df7bb59f 100644 --- a/cmd/compose/logs.go +++ b/cmd/compose/logs.go @@ -18,6 +18,7 @@ package compose import ( "context" + "errors" "github.com/docker/cli/cli/command" "github.com/spf13/cobra" @@ -49,11 +50,17 @@ func logsCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) RunE: Adapt(func(ctx context.Context, args []string) error { return runLogs(ctx, dockerCli, backend, opts, args) }), + PreRunE: func(cmd *cobra.Command, args []string) error { + if opts.index > 0 && len(args) != 1 { + return errors.New("--index requires one service to be selected") + } + return nil + }, ValidArgsFunction: completeServiceNames(dockerCli, p), } flags := logsCmd.Flags() flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output.") - flags.IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].") + flags.IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas") 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)") flags.BoolVar(&opts.noColor, "no-color", false, "Produce monochrome output.") @@ -73,6 +80,7 @@ func runLogs(ctx context.Context, dockerCli command.Cli, backend api.Service, op Project: project, Services: services, Follow: opts.follow, + Index: opts.index, Tail: opts.tail, Since: opts.since, Until: opts.until, diff --git a/docs/reference/compose_logs.md b/docs/reference/compose_logs.md index 20c29e923..b6c705bab 100644 --- a/docs/reference/compose_logs.md +++ b/docs/reference/compose_logs.md @@ -9,7 +9,7 @@ View output from containers |:---------------------|:---------|:--------|:-----------------------------------------------------------------------------------------------| | `--dry-run` | | | Execute command in dry run mode | | `-f`, `--follow` | | | Follow log output. | -| `--index` | `int` | `1` | index of the container if there are multiple instances of a service [default: 1]. | +| `--index` | `int` | `0` | index of the container if service has multiple replicas | | `--no-color` | | | Produce monochrome output. | | `--no-log-prefix` | | | Don't print prefix in logs. | | `--since` | `string` | | Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) | diff --git a/docs/reference/docker_compose_logs.yaml b/docs/reference/docker_compose_logs.yaml index ed4753258..fe6bbdb42 100644 --- a/docs/reference/docker_compose_logs.yaml +++ b/docs/reference/docker_compose_logs.yaml @@ -18,9 +18,8 @@ options: swarm: false - option: index value_type: int - default_value: "1" - description: | - index of the container if there are multiple instances of a service [default: 1]. + default_value: "0" + description: index of the container if service has multiple replicas deprecated: false hidden: false experimental: false diff --git a/pkg/api/api.go b/pkg/api/api.go index 42578638a..926462efe 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -488,6 +488,7 @@ type ServiceStatus struct { // LogOptions defines optional parameters for the `Log` API type LogOptions struct { Project *types.Project + Index int Services []string Tail string Since string diff --git a/pkg/compose/logs.go b/pkg/compose/logs.go index 6546ffe04..bb145672f 100644 --- a/pkg/compose/logs.go +++ b/pkg/compose/logs.go @@ -20,7 +20,6 @@ import ( "context" "errors" "io" - "strings" "time" "github.com/docker/docker/api/types" @@ -39,11 +38,21 @@ func (s *composeService) Logs( consumer api.LogConsumer, options api.LogOptions, ) error { - projectName = strings.ToLower(projectName) - containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...) - if err != nil { - return err + var containers Containers + var err error + + if options.Index > 0 { + container, err := s.getSpecifiedContainer(ctx, projectName, oneOffExclude, true, options.Services[0], options.Index) + if err != nil { + return err + } + containers = append(containers, container) + } else { + containers, err = s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...) + if err != nil { + return err + } } if options.Project != nil && len(options.Services) == 0 {