From c818e9238e48fdc4cbfc8d12be3ff62235998c6c Mon Sep 17 00:00:00 2001 From: Sebastian Spaink Date: Fri, 10 Jun 2022 14:38:51 -0500 Subject: [PATCH] feat: make the command "config" a subcommand --- README.md | 2 +- cmd/telegraf/telegraf.go | 38 +++++++++++++++++++++++++++++++++----- docs/COMMANDS_AND_FLAGS.md | 2 +- docs/CONFIGURATION.md | 2 +- internal/usage.go | 2 +- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a85b3dde5dd39..4ded217a5c2a8 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ telegraf config > telegraf.conf ### Generate config with only cpu input & influxdb output plugins defined ```shell -telegraf --section-filter agent:inputs:outputs --input-filter cpu --output-filter influxdb config +telegraf config --section-filter agent:inputs:outputs --input-filter cpu --output-filter influxdb ``` ### Run a single telegraf collection, outputting metrics to stdout diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 8284ab5ae874b..f8c61c33ce015 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -84,6 +84,20 @@ var fProcessorFilters = flag.String("processor-filter", "", var fUsage = flag.String("usage", "", "print usage for a plugin, ie, 'telegraf --usage mysql'") +// Initialize the subcommand `telegraf config` +// This duplicates the above filters which are used for `telegraf --sample-config` and `telegraf --deprecation-list` +var configCmd = flag.NewFlagSet("config", flag.ExitOnError) +var fSubSectionFilters = configCmd.String("section-filter", "", + "filter the sections to print, separator is ':'. Valid values are 'agent', 'global_tags', 'outputs', 'processors', 'aggregators' and 'inputs'") +var fSubInputFilters = configCmd.String("input-filter", "", + "filter the inputs to enable, separator is :") +var fSubOutputFilters = configCmd.String("output-filter", "", + "filter the outputs to enable, separator is :") +var fsubAggregatorFilters = configCmd.String("aggregator-filter", "", + "filter the aggregators to enable, separator is :") +var fSubProcessorFilters = configCmd.String("processor-filter", "", + "filter the processors to enable, separator is :") + //nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows var fService = flag.String("service", "", "operate on the service (windows only)") @@ -356,6 +370,16 @@ func formatFullVersion() string { return strings.Join(parts, " ") } +func deleteEmpty(s []string) []string { + var r []string + for _, str := range s { + if str != "" { + r = append(r, str) + } + } + return r +} + func main() { flag.Var(&fConfigs, "config", "configuration file to load") flag.Var(&fConfigDirs, "config-directory", "directory containing additional *.conf files") @@ -421,12 +445,16 @@ func main() { fmt.Println(formatFullVersion()) return case "config": + err := configCmd.Parse(args[1:]) + if err != nil { + log.Fatal("E! " + err.Error()) + } config.PrintSampleConfig( - sectionFilters, - inputFilters, - outputFilters, - aggregatorFilters, - processorFilters, + 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)+":", ":")), ) return } diff --git a/docs/COMMANDS_AND_FLAGS.md b/docs/COMMANDS_AND_FLAGS.md index 4579e84488058..d906fd651f1c9 100644 --- a/docs/COMMANDS_AND_FLAGS.md +++ b/docs/COMMANDS_AND_FLAGS.md @@ -49,7 +49,7 @@ telegraf [flags] **Generate config with only cpu input & influxdb output plugins defined:** -`telegraf --input-filter cpu --output-filter influxdb config` +`telegraf config --input-filter cpu --output-filter influxdb` **Run a single telegraf collection, outputting metrics to stdout:** diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 06644d12bbe56..e1b22023b6aa3 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -19,7 +19,7 @@ To generate a file with specific inputs and outputs, you can use the --input-filter and --output-filter flags: ```sh -telegraf --input-filter cpu:mem:net:swap --output-filter influxdb:kafka config +telegraf config --input-filter cpu:mem:net:swap --output-filter influxdb:kafka ``` [View the full list][flags] of Telegraf commands and flags or by running `telegraf --help`. diff --git a/internal/usage.go b/internal/usage.go index 3e6e4fb14fc80..65fb378669df7 100644 --- a/internal/usage.go +++ b/internal/usage.go @@ -51,7 +51,7 @@ Examples: telegraf config > telegraf.conf # generate config with only cpu input & influxdb output plugins defined - telegraf --input-filter cpu --output-filter influxdb config + telegraf config --input-filter cpu --output-filter influxdb # run a single telegraf collection, outputting metrics to stdout telegraf --config telegraf.conf --test