From ce85f802fe139b36e41560c5acdd0478ab3eb375 Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Mon, 14 Oct 2019 15:14:17 +0300 Subject: [PATCH] refactor: split newCollector() to silence the funlen linter --- cmd/collectors.go | 123 +++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/cmd/collectors.go b/cmd/collectors.go index 68aacabe6f7..4ea70f195a3 100644 --- a/cmd/collectors.go +++ b/cmd/collectors.go @@ -64,75 +64,76 @@ func parseCollector(s string) (t, arg string) { } } -func newCollector(collectorName, arg string, src *loader.SourceData, conf Config) (lib.Collector, error) { - getCollector := func() (lib.Collector, error) { - switch collectorName { - case collectorJSON: - return jsonc.New(afero.NewOsFs(), arg) - case collectorInfluxDB: - config := influxdb.NewConfig().Apply(conf.Collectors.InfluxDB) - if err := envconfig.Process("k6", &config); err != nil { - return nil, err - } - urlConfig, err := influxdb.ParseURL(arg) +//TODO: totally refactor this... +func getCollector(collectorName, arg string, src *loader.SourceData, conf Config) (lib.Collector, error) { + switch collectorName { + case collectorJSON: + return jsonc.New(afero.NewOsFs(), arg) + case collectorInfluxDB: + config := influxdb.NewConfig().Apply(conf.Collectors.InfluxDB) + if err := envconfig.Process("k6", &config); err != nil { + return nil, err + } + urlConfig, err := influxdb.ParseURL(arg) + if err != nil { + return nil, err + } + config = config.Apply(urlConfig) + return influxdb.New(config) + case collectorCloud: + config := cloud.NewConfig().Apply(conf.Collectors.Cloud) + if err := envconfig.Process("k6", &config); err != nil { + return nil, err + } + if arg != "" { + config.Name = null.StringFrom(arg) + } + return cloud.New(config, src, conf.Options, consts.Version) + case collectorKafka: + config := kafka.NewConfig().Apply(conf.Collectors.Kafka) + if err := envconfig.Process("k6", &config); err != nil { + return nil, err + } + if arg != "" { + cmdConfig, err := kafka.ParseArg(arg) if err != nil { return nil, err } - config = config.Apply(urlConfig) - return influxdb.New(config) - case collectorCloud: - config := cloud.NewConfig().Apply(conf.Collectors.Cloud) - if err := envconfig.Process("k6", &config); err != nil { - return nil, err - } - if arg != "" { - config.Name = null.StringFrom(arg) - } - return cloud.New(config, src, conf.Options, consts.Version) - case collectorKafka: - config := kafka.NewConfig().Apply(conf.Collectors.Kafka) - if err := envconfig.Process("k6", &config); err != nil { - return nil, err - } - if arg != "" { - cmdConfig, err := kafka.ParseArg(arg) - if err != nil { - return nil, err - } - config = config.Apply(cmdConfig) - } - return kafka.New(config) - case collectorStatsD: - config := common.NewConfig().Apply(conf.Collectors.StatsD) - if err := envconfig.Process("k6_statsd", &config); err != nil { - return nil, err - } - return statsd.New(config) - case collectorDatadog: - config := datadog.NewConfig().Apply(conf.Collectors.Datadog) - if err := envconfig.Process("k6_datadog", &config); err != nil { - return nil, err - } - return datadog.New(config) - case collectorCSV: - config := csv.NewConfig().Apply(conf.Collectors.CSV) - if err := envconfig.Process("k6", &config); err != nil { + config = config.Apply(cmdConfig) + } + return kafka.New(config) + case collectorStatsD: + config := common.NewConfig().Apply(conf.Collectors.StatsD) + if err := envconfig.Process("k6_statsd", &config); err != nil { + return nil, err + } + return statsd.New(config) + case collectorDatadog: + config := datadog.NewConfig().Apply(conf.Collectors.Datadog) + if err := envconfig.Process("k6_datadog", &config); err != nil { + return nil, err + } + return datadog.New(config) + case collectorCSV: + config := csv.NewConfig().Apply(conf.Collectors.CSV) + if err := envconfig.Process("k6", &config); err != nil { + return nil, err + } + if arg != "" { + cmdConfig, err := csv.ParseArg(arg) + if err != nil { return nil, err } - if arg != "" { - cmdConfig, err := csv.ParseArg(arg) - if err != nil { - return nil, err - } - config = config.Apply(cmdConfig) - } - return csv.New(afero.NewOsFs(), conf.SystemTags, config) - default: - return nil, errors.Errorf("unknown output type: %s", collectorName) + config = config.Apply(cmdConfig) } + return csv.New(afero.NewOsFs(), conf.SystemTags, config) + default: + return nil, errors.Errorf("unknown output type: %s", collectorName) } +} - collector, err := getCollector() +func newCollector(collectorName, arg string, src *loader.SourceData, conf Config) (lib.Collector, error) { + collector, err := getCollector(collectorName, arg, src, conf) if err != nil { return collector, err }