From 5e41f5e8af0116ee3d5cbb048b0a1dde992e2a83 Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Thu, 25 Feb 2021 16:00:04 +0200 Subject: [PATCH] Move the output extension registry to the main output module --- cmd/outputs.go | 3 +- .../{extensions/registry.go => extensions.go} | 35 +++++++------------ 2 files changed, 14 insertions(+), 24 deletions(-) rename output/{extensions/registry.go => extensions.go} (50%) diff --git a/cmd/outputs.go b/cmd/outputs.go index 46f740a89fa..02fb2ed9de6 100644 --- a/cmd/outputs.go +++ b/cmd/outputs.go @@ -34,7 +34,6 @@ import ( "github.com/loadimpact/k6/lib/consts" "github.com/loadimpact/k6/loader" "github.com/loadimpact/k6/output" - "github.com/loadimpact/k6/output/extensions" "github.com/loadimpact/k6/output/json" "github.com/loadimpact/k6/stats" "github.com/loadimpact/k6/stats/cloud" @@ -123,7 +122,7 @@ func getAllOutputConstructors() (map[string]func(output.Params) (output.Output, }, } - exts := extensions.GetAll() + exts := output.GetExtensions() for k, v := range exts { if _, ok := result[k]; ok { return nil, fmt.Errorf("invalid output extension %s, built-in output with the same type already exists", k) diff --git a/output/extensions/registry.go b/output/extensions.go similarity index 50% rename from output/extensions/registry.go rename to output/extensions.go index 09ea066331f..d136b136234 100644 --- a/output/extensions/registry.go +++ b/output/extensions.go @@ -18,47 +18,38 @@ * */ -package extensions +package output import ( "fmt" "sync" - - "github.com/loadimpact/k6/output" ) //nolint:gochecknoglobals var ( - modules = make(map[string]func(output.Params) (output.Output, error)) - mx sync.RWMutex + extensions = make(map[string]func(Params) (Output, error)) + mx sync.RWMutex ) -// GetAll returns all registered extensions. -func GetAll() map[string]func(output.Params) (output.Output, error) { +// GetExtensions returns all registered extensions. +func GetExtensions() map[string]func(Params) (Output, error) { mx.RLock() defer mx.RUnlock() - res := make(map[string]func(output.Params) (output.Output, error), len(modules)) - for k, v := range modules { + res := make(map[string]func(Params) (Output, error), len(extensions)) + for k, v := range extensions { res[k] = v } return res } -// Get returns the output module constructor with the specified name. -func Get(name string) func(output.Params) (output.Output, error) { - mx.RLock() - defer mx.RUnlock() - return modules[name] -} - -// Register the given output module constructor. This function panics if a -// module with the same name is already registered. -func Register(name string, mod func(output.Params) (output.Output, error)) { +// RegisterExtension registers the given output extension constructor. This +// function panics if a module with the same name is already registered. +func RegisterExtension(name string, mod func(Params) (Output, error)) { mx.Lock() defer mx.Unlock() - if _, ok := modules[name]; ok { - panic(fmt.Sprintf("output module already registered: %s", name)) + if _, ok := extensions[name]; ok { + panic(fmt.Sprintf("output extension already registered: %s", name)) } - modules[name] = mod + extensions[name] = mod }