fix: Linter fixes for config/config.go (#10710)

This commit is contained in:
Paweł Żak 2022-02-23 02:47:04 +01:00 committed by GitHub
parent 066ab25ae8
commit f3904d3e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 42 deletions

View File

@ -71,7 +71,7 @@ linters-settings:
- name: unconditional-recursion - name: unconditional-recursion
- name: unexported-naming - name: unexported-naming
- name: unhandled-error - name: unhandled-error
arguments: ["fmt.Printf", "fmt.Println"] arguments: ["fmt.Printf", "fmt.Println", "fmt.Print"]
- name: unnecessary-stmt - name: unnecessary-stmt
- name: unreachable-code - name: unreachable-code
# - name: unused-parameter # - name: unused-parameter

View File

@ -456,7 +456,7 @@ func PrintSampleConfig(
processorFilters []string, processorFilters []string,
) { ) {
// print headers // print headers
fmt.Printf(header) fmt.Print(header)
if len(sectionFilters) == 0 { if len(sectionFilters) == 0 {
sectionFilters = sectionDefaults sectionFilters = sectionDefaults
@ -467,11 +467,11 @@ func PrintSampleConfig(
if sliceContains("outputs", sectionFilters) { if sliceContains("outputs", sectionFilters) {
if len(outputFilters) != 0 { if len(outputFilters) != 0 {
if len(outputFilters) >= 3 && outputFilters[1] != "none" { if len(outputFilters) >= 3 && outputFilters[1] != "none" {
fmt.Printf(outputHeader) fmt.Print(outputHeader)
} }
printFilteredOutputs(outputFilters, false) printFilteredOutputs(outputFilters, false)
} else { } else {
fmt.Printf(outputHeader) fmt.Print(outputHeader)
printFilteredOutputs(outputDefaults, false) printFilteredOutputs(outputDefaults, false)
// Print non-default outputs, commented // Print non-default outputs, commented
var pnames []string var pnames []string
@ -489,11 +489,11 @@ func PrintSampleConfig(
if sliceContains("processors", sectionFilters) { if sliceContains("processors", sectionFilters) {
if len(processorFilters) != 0 { if len(processorFilters) != 0 {
if len(processorFilters) >= 3 && processorFilters[1] != "none" { if len(processorFilters) >= 3 && processorFilters[1] != "none" {
fmt.Printf(processorHeader) fmt.Print(processorHeader)
} }
printFilteredProcessors(processorFilters, false) printFilteredProcessors(processorFilters, false)
} else { } else {
fmt.Printf(processorHeader) fmt.Print(processorHeader)
pnames := []string{} pnames := []string{}
for pname := range processors.Processors { for pname := range processors.Processors {
pnames = append(pnames, pname) pnames = append(pnames, pname)
@ -507,11 +507,11 @@ func PrintSampleConfig(
if sliceContains("aggregators", sectionFilters) { if sliceContains("aggregators", sectionFilters) {
if len(aggregatorFilters) != 0 { if len(aggregatorFilters) != 0 {
if len(aggregatorFilters) >= 3 && aggregatorFilters[1] != "none" { if len(aggregatorFilters) >= 3 && aggregatorFilters[1] != "none" {
fmt.Printf(aggregatorHeader) fmt.Print(aggregatorHeader)
} }
printFilteredAggregators(aggregatorFilters, false) printFilteredAggregators(aggregatorFilters, false)
} else { } else {
fmt.Printf(aggregatorHeader) fmt.Print(aggregatorHeader)
pnames := []string{} pnames := []string{}
for pname := range aggregators.Aggregators { for pname := range aggregators.Aggregators {
pnames = append(pnames, pname) pnames = append(pnames, pname)
@ -525,11 +525,11 @@ func PrintSampleConfig(
if sliceContains("inputs", sectionFilters) { if sliceContains("inputs", sectionFilters) {
if len(inputFilters) != 0 { if len(inputFilters) != 0 {
if len(inputFilters) >= 3 && inputFilters[1] != "none" { if len(inputFilters) >= 3 && inputFilters[1] != "none" {
fmt.Printf(inputHeader) fmt.Print(inputHeader)
} }
printFilteredInputs(inputFilters, false) printFilteredInputs(inputFilters, false)
} else { } else {
fmt.Printf(inputHeader) fmt.Print(inputHeader)
printFilteredInputs(inputDefaults, false) printFilteredInputs(inputDefaults, false)
// Print non-default inputs, commented // Print non-default inputs, commented
var pnames []string var pnames []string
@ -607,8 +607,7 @@ func printFilteredInputs(inputFilters []string, commented bool) {
creator := inputs.Inputs[pname] creator := inputs.Inputs[pname]
input := creator() input := creator()
switch p := input.(type) { if p, ok := input.(telegraf.ServiceInput); ok {
case telegraf.ServiceInput:
servInputs[pname] = p servInputs[pname] = p
servInputNames = append(servInputNames, pname) servInputNames = append(servInputNames, pname)
continue continue
@ -623,7 +622,7 @@ func printFilteredInputs(inputFilters []string, commented bool) {
} }
sort.Strings(servInputNames) sort.Strings(servInputNames)
fmt.Printf(serviceInputHeader) fmt.Print(serviceInputHeader)
for _, name := range servInputNames { for _, name := range servInputNames {
printConfig(name, servInputs[name], "inputs", commented, inputs.Deprecations[name]) printConfig(name, servInputs[name], "inputs", commented, inputs.Deprecations[name])
} }
@ -649,11 +648,11 @@ func printFilteredOutputs(outputFilters []string, commented bool) {
func printFilteredGlobalSections(sectionFilters []string) { func printFilteredGlobalSections(sectionFilters []string) {
if sliceContains("global_tags", sectionFilters) { if sliceContains("global_tags", sectionFilters) {
fmt.Printf(globalTagsConfig) fmt.Print(globalTagsConfig)
} }
if sliceContains("agent", sectionFilters) { if sliceContains("agent", sectionFilters) {
fmt.Printf(agentConfig) fmt.Print(agentConfig)
} }
} }
@ -698,21 +697,23 @@ func sliceContains(name string, list []string) bool {
// PrintInputConfig prints the config usage of a single input. // PrintInputConfig prints the config usage of a single input.
func PrintInputConfig(name string) error { func PrintInputConfig(name string) error {
if creator, ok := inputs.Inputs[name]; ok { creator, ok := inputs.Inputs[name]
printConfig(name, creator(), "inputs", false, inputs.Deprecations[name]) if !ok {
} else { return fmt.Errorf("input %s not found", name)
return fmt.Errorf("Input %s not found", name)
} }
printConfig(name, creator(), "inputs", false, inputs.Deprecations[name])
return nil return nil
} }
// PrintOutputConfig prints the config usage of a single output. // PrintOutputConfig prints the config usage of a single output.
func PrintOutputConfig(name string) error { func PrintOutputConfig(name string) error {
if creator, ok := outputs.Outputs[name]; ok { creator, ok := outputs.Outputs[name]
printConfig(name, creator(), "outputs", false, outputs.Deprecations[name]) if !ok {
} else { return fmt.Errorf("output %s not found", name)
return fmt.Errorf("Output %s not found", name)
} }
printConfig(name, creator(), "outputs", false, outputs.Deprecations[name])
return nil return nil
} }
@ -1192,14 +1193,13 @@ func (c *Config) addOutput(name string, table *ast.Table) error {
printHistoricPluginDeprecationNotice("outputs", name, di) printHistoricPluginDeprecationNotice("outputs", name, di)
return fmt.Errorf("plugin deprecated") return fmt.Errorf("plugin deprecated")
} }
return fmt.Errorf("Undefined but requested output: %s", name) return fmt.Errorf("undefined but requested output: %s", name)
} }
output := creator() output := creator()
// If the output has a SetSerializer function, then this means it can write // If the output has a SetSerializer function, then this means it can write
// arbitrary types of output, so build the serializer and set it. // arbitrary types of output, so build the serializer and set it.
switch t := output.(type) { if t, ok := output.(serializers.SerializerOutput); ok {
case serializers.SerializerOutput:
serializer, err := c.buildSerializer(table) serializer, err := c.buildSerializer(table)
if err != nil { if err != nil {
return err return err
@ -1943,16 +1943,16 @@ func (c *Config) getFieldInt64(tbl *ast.Table, fieldName string, target *int64)
func (c *Config) getFieldStringSlice(tbl *ast.Table, fieldName string, target *[]string) { func (c *Config) getFieldStringSlice(tbl *ast.Table, fieldName string, target *[]string) {
if node, ok := tbl.Fields[fieldName]; ok { if node, ok := tbl.Fields[fieldName]; ok {
if kv, ok := node.(*ast.KeyValue); ok { if kv, ok := node.(*ast.KeyValue); ok {
if ary, ok := kv.Value.(*ast.Array); ok { ary, ok := kv.Value.(*ast.Array)
for _, elem := range ary.Value { if !ok {
if str, ok := elem.(*ast.String); ok {
*target = append(*target, str.Value)
}
}
} else {
c.addError(tbl, fmt.Errorf("found unexpected format while parsing %q, expecting string array/slice format", fieldName)) c.addError(tbl, fmt.Errorf("found unexpected format while parsing %q, expecting string array/slice format", fieldName))
return return
} }
for _, elem := range ary.Value {
if str, ok := elem.(*ast.String); ok {
*target = append(*target, str.Value)
}
}
} }
} }
} }
@ -1962,18 +1962,19 @@ func (c *Config) getFieldTagFilter(tbl *ast.Table, fieldName string, target *[]m
if subtbl, ok := node.(*ast.Table); ok { if subtbl, ok := node.(*ast.Table); ok {
for name, val := range subtbl.Fields { for name, val := range subtbl.Fields {
if kv, ok := val.(*ast.KeyValue); ok { if kv, ok := val.(*ast.KeyValue); ok {
tagfilter := models.TagFilter{Name: name} ary, ok := kv.Value.(*ast.Array)
if ary, ok := kv.Value.(*ast.Array); ok { if !ok {
for _, elem := range ary.Value {
if str, ok := elem.(*ast.String); ok {
tagfilter.Filter = append(tagfilter.Filter, str.Value)
}
}
} else {
c.addError(tbl, fmt.Errorf("found unexpected format while parsing %q, expecting string array/slice format on each entry", fieldName)) c.addError(tbl, fmt.Errorf("found unexpected format while parsing %q, expecting string array/slice format on each entry", fieldName))
return return
} }
*target = append(*target, tagfilter)
tagFilter := models.TagFilter{Name: name}
for _, elem := range ary.Value {
if str, ok := elem.(*ast.String); ok {
tagFilter.Filter = append(tagFilter.Filter, str.Value)
}
}
*target = append(*target, tagFilter)
} }
} }
} }