Skip to content

Commit 3a20d96

Browse files
committed
feat: Add --compose
1 parent 6acfe63 commit 3a20d96

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The `container query` is a regular expression of the container name; you could p
3838
-----------------------------|---------------------------------|---------
3939
`--color` | `auto` | Force set color output. 'auto': colorize if tty attached, 'always': always colorize, 'never': never colorize.
4040
`--completion` | | Output stern command-line completion code for the specified shell. Can be 'bash', 'zsh' or 'fish'.
41+
`--compose` | `[]` | Compose project name to match (regular expression)
4142
`--compose-colors` | | Specifies the colors used to highlight container names. Provide colors as a comma-separated list using SGR (Select Graphic Rendition) sequences, e.g., "91,92,93,94,95,96".
4243
`--config` | `~/.config/tailfin/config.yaml` | Path to the tailfin config file
4344
`--container-colors` | | Specifies the colors used to highlight compose project names. Use the same format as --container-colors. Defaults to the values of --container-colors if omitted, and must match its length.
@@ -171,12 +172,12 @@ Tail all logs
171172
```
172173
tailfin .
173174
```
174-
<!--
175-
*TODO* Tail the `test` compose project without printing any prior logs
175+
176+
Tail the `test` compose project without printing any prior logs
176177
```
177-
tailfin . -c test --tail 0
178+
tailfin . --compose test --tail 0
178179
```
179-
-->
180+
180181
Tail everything excluding logs from `backend` container
181182
```
182183
tailfin --exclude-container backend .

cmd/tailfincmd/cmd.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type options struct {
6262
color string
6363
version bool
6464
completion string
65+
compose []string
6566
template string
6667
templateFile string
6768
output string
@@ -155,6 +156,11 @@ func (o *options) tailfinConfig() (*stern.DockerConfig, error) {
155156
return nil, errors.Wrap(err, "failed to compile regular expression for excluded container query")
156157
}
157158

159+
compose, err := compileREs(o.compose)
160+
if err != nil {
161+
return nil, errors.Wrap(err, "failed to compile regular expression for compose filter")
162+
}
163+
158164
exclude, err := compileREs(o.exclude)
159165
if err != nil {
160166
return nil, errors.Wrap(err, "failed to compile regular expression for exclusion filter")
@@ -218,6 +224,7 @@ func (o *options) tailfinConfig() (*stern.DockerConfig, error) {
218224

219225
return &stern.DockerConfig{
220226
ContainerQuery: container,
227+
ComposeProjectQuery: compose,
221228
Timestamps: timestampFormat != "",
222229
TimestampFormat: timestampFormat,
223230
Location: location,
@@ -325,6 +332,7 @@ func (o *options) overrideFlagSetDefaultFromConfig(fs *pflag.FlagSet) error {
325332
func (o *options) AddFlags(fs *pflag.FlagSet) {
326333
fs.StringVar(&o.color, "color", o.color, "Force set color output. 'auto': colorize if tty attached, 'always': always colorize, 'never': never colorize.")
327334
fs.StringVar(&o.completion, "completion", o.completion, "Output stern command-line completion code for the specified shell. Can be 'bash', 'zsh' or 'fish'.")
335+
fs.StringArrayVar(&o.compose, "compose", o.compose, "Compose project name to match (regular expression)")
328336
fs.StringArrayVarP(&o.exclude, "exclude", "e", o.exclude, "Log lines to exclude. (regular expression)")
329337
fs.StringArrayVarP(&o.excludeContainer, "exclude-container", "E", o.excludeContainer, "Container name to exclude. (regular expression)")
330338
fs.BoolVar(&o.noFollow, "no-follow", o.noFollow, "Exit when all logs have been shown.")
@@ -347,7 +355,6 @@ func (o *options) AddFlags(fs *pflag.FlagSet) {
347355
fs.StringSliceVar(&o.containerColors, "compose-colors", o.containerColors, "Specifies the colors used to highlight container names. Provide colors as a comma-separated list using SGR (Select Graphic Rendition) sequences, e.g., \"91,92,93,94,95,96\".")
348356
fs.StringSliceVar(&o.composeColors, "container-colors", o.composeColors, "Specifies the colors used to highlight compose project names. Use the same format as --container-colors. Defaults to the values of --container-colors if omitted, and must match its length.")
349357
// TODO: --context for docker context? Seems to be a `docker` thing, not a dockerd thing.
350-
// TODO: --compose/-c to limit to a compose project
351358
// TODO: --ignore-compose to make it unaware of compose (e.g. use full container name)
352359
// TODO: --label/-l
353360

stern/docker_config.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type DockerConfig struct {
1414
Location *time.Location
1515
ContainerQuery []*regexp.Regexp
1616
ExcludeContainerQuery []*regexp.Regexp
17+
ComposeProjectQuery []*regexp.Regexp
1718
Exclude []*regexp.Regexp
1819
ImageQuery []*regexp.Regexp
1920
Include []*regexp.Regexp

stern/docker_stern.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func RunDocker(ctx context.Context, client *dockerclient.Client, config *DockerC
5353
filter := newDockerTargetFilter(dockerTargetFilterConfig{
5454
containerFilter: config.ContainerQuery,
5555
containerExcludeFilter: config.ExcludeContainerQuery,
56+
composeProjectFilter: config.ComposeProjectQuery,
5657
imageFilter: config.ImageQuery,
5758
})
5859

stern/docker_target.go

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type DockerTarget struct {
2222
type dockerTargetFilterConfig struct {
2323
containerFilter []*regexp.Regexp
2424
containerExcludeFilter []*regexp.Regexp
25+
composeProjectFilter []*regexp.Regexp
2526
imageFilter []*regexp.Regexp
2627
}
2728

@@ -49,6 +50,7 @@ func (f *dockerTargetFilter) visit(container types.ContainerJSON, visitor func(t
4950
}
5051

5152
if !f.matchingNameFilter(containerName) ||
53+
!f.matchingComposeFilter(composeProject) ||
5254
!f.matchingImageFilter(container.Config.Image) ||
5355
f.matchingNameExcludeFilter(containerName) {
5456
return
@@ -121,6 +123,20 @@ func (f *dockerTargetFilter) matchingNameExcludeFilter(containerName string) boo
121123
return false
122124
}
123125

126+
func (f *dockerTargetFilter) matchingComposeFilter(composeProject string) bool {
127+
if len(f.config.composeProjectFilter) == 0 {
128+
return true
129+
} else if len(composeProject) > 0 {
130+
for _, re := range f.config.composeProjectFilter {
131+
if re.MatchString(composeProject) {
132+
return true
133+
}
134+
}
135+
}
136+
klog.V(7).InfoS("Compose project name does not match filters", "compose", composeProject)
137+
return false
138+
}
139+
124140
func (f *dockerTargetFilter) matchingImageFilter(containerImage string) bool {
125141
if len(f.config.imageFilter) == 0 {
126142
return true

0 commit comments

Comments
 (0)