From 4c1285cf7a30b4fff20563488e156541e738f28b Mon Sep 17 00:00:00 2001 From: Sebastian Spaink Date: Wed, 15 Jun 2022 11:26:32 -0500 Subject: [PATCH] fix: backwards compatible support telegraf *filters* config --- cmd/telegraf/telegraf.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index f8c61c33ce015..8990769954a25 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -449,12 +449,39 @@ func main() { if err != nil { log.Fatal("E! " + err.Error()) } + + // The sub_Filters are populated when the filter flags are set after the subcommand config + // e.g. telegraf config --section-filter inputs + subSectionFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubSectionFilters)+":", ":")) + subInputFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubInputFilters)+":", ":")) + subOutputFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubOutputFilters)+":", ":")) + subAggregatorFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fsubAggregatorFilters)+":", ":")) + subProcessorFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubProcessorFilters)+":", ":")) + + // Overwrite the global filters if the subfilters are defined, this allows for backwards compatibility + // Now you can still filter the sample config like so: telegraf --section-filter inputs config + if len(subSectionFilters) > 0 { + sectionFilters = subSectionFilters + } + if len(subInputFilters) > 0 { + inputFilters = subInputFilters + } + if len(subOutputFilters) > 0 { + outputFilters = subOutputFilters + } + if len(subAggregatorFilters) > 0 { + aggregatorFilters = subAggregatorFilters + } + if len(subProcessorFilters) > 0 { + processorFilters = subProcessorFilters + } + config.PrintSampleConfig( - deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubSectionFilters)+":", ":")), - deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubInputFilters)+":", ":")), - deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubOutputFilters)+":", ":")), - deleteEmpty(strings.Split(":"+strings.TrimSpace(*fsubAggregatorFilters)+":", ":")), - deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubProcessorFilters)+":", ":")), + sectionFilters, + inputFilters, + outputFilters, + aggregatorFilters, + processorFilters, ) return }