Skip to content

Commit

Permalink
feat: make the command "config" a subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Spaink committed Jun 15, 2022
1 parent 72e91ba commit c818e92
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 33 additions & 5 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion docs/COMMANDS_AND_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down
2 changes: 1 addition & 1 deletion docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion internal/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c818e92

Please sign in to comment.