Skip to content

Commit

Permalink
introduce compose logs --index to select a replica container
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Saha <asaha@atlassian.com>
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
asaha123 authored and ndeloof committed Nov 29, 2023
1 parent 8c964f5 commit 750553c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
10 changes: 9 additions & 1 deletion cmd/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package compose

import (
"context"
"errors"

"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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.")
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/compose_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
5 changes: 2 additions & 3 deletions docs/reference/docker_compose_logs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions pkg/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"io"
"strings"
"time"

"github.com/docker/docker/api/types"
Expand All @@ -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 {
Expand Down

0 comments on commit 750553c

Please sign in to comment.