Skip to content

Commit

Permalink
refactor: split newCollector() to silence the funlen linter
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Oct 14, 2019
1 parent 087f865 commit ce85f80
Showing 1 changed file with 62 additions and 61 deletions.
123 changes: 62 additions & 61 deletions cmd/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ce85f80

Please sign in to comment.