From 0b86bbb4f5fc5e88378693ef9b65100a5726ca7b Mon Sep 17 00:00:00 2001 From: Cody Soyland Date: Wed, 18 Jul 2018 11:58:27 -0500 Subject: [PATCH] Fix linter issues: gochecknoinits --- Makefile | 1 + broadcast.go | 6 +----- cmd/check.go | 6 +----- cmd/config.go | 4 ---- cmd/export.go | 6 +----- cmd/generate_config.go | 6 +----- cmd/import.go | 4 ---- cmd/inspect.go | 6 +----- cmd/root.go | 16 +++++++++------- cmd/server.go | 4 ---- enterprise/b/btree.go | 13 ++----------- enterprise/enterprise.go | 2 +- gc.go | 6 +----- http/client_test.go | 19 ++++++------------- logger.go | 6 +----- server/server.go | 7 +++---- stats.go | 6 +----- version.go | 4 +++- 18 files changed, 33 insertions(+), 89 deletions(-) diff --git a/Makefile b/Makefile index 65df8f894..9fd7c00ca 100644 --- a/Makefile +++ b/Makefile @@ -113,6 +113,7 @@ gometalinter: require-gometalinter gometalinter --vendor --disable-all \ --deadline=60s \ --enable=deadcode \ + --enable=gochecknoinits \ --enable=gofmt \ --enable=goimports \ --enable=gotype \ diff --git a/broadcast.go b/broadcast.go index a3ea01a4f..6f2245992 100644 --- a/broadcast.go +++ b/broadcast.go @@ -37,12 +37,8 @@ type broadcaster interface { // TODO add at least a single "isMessage()" method. type Message interface{} -func init() { - NopBroadcaster = &nopBroadcaster{} -} - // NopBroadcaster represents a Broadcaster that doesn't do anything. -var NopBroadcaster broadcaster +var NopBroadcaster broadcaster = &nopBroadcaster{} type nopBroadcaster struct{} diff --git a/cmd/check.go b/cmd/check.go index 8785a78e1..f9adf0980 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -27,7 +27,7 @@ import ( var checker *ctl.CheckCommand -func newCheckCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command { +func newCheckCommand(_ io.Reader, _, _ io.Writer) *cobra.Command { checker = ctl.NewCheckCommand(os.Stdin, os.Stdout, os.Stderr) checkCmd := &cobra.Command{ Use: "check [path2]...", @@ -48,7 +48,3 @@ Performs a consistency check on data files. } return checkCmd } - -func init() { - subcommandFns["check"] = newCheckCommand -} diff --git a/cmd/config.go b/cmd/config.go index 3d65fa131..0288c34f0 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -49,7 +49,3 @@ func newConfigCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command return confCmd } - -func init() { - subcommandFns["config"] = newConfigCommand -} diff --git a/cmd/export.go b/cmd/export.go index d0f63edbf..0a087e82a 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -26,7 +26,7 @@ import ( var Exporter *ctl.ExportCommand -func newExportCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command { +func newExportCommand(_ io.Reader, _, _ io.Writer) *cobra.Command { Exporter = ctl.NewExportCommand(os.Stdin, os.Stdout, os.Stderr) exportCmd := &cobra.Command{ Use: "export", @@ -58,7 +58,3 @@ The file does not contain any headers. return exportCmd } - -func init() { - subcommandFns["export"] = newExportCommand -} diff --git a/cmd/generate_config.go b/cmd/generate_config.go index 0b5b81462..7ff4b0833 100644 --- a/cmd/generate_config.go +++ b/cmd/generate_config.go @@ -26,7 +26,7 @@ import ( var generateConf *ctl.GenerateConfigCommand -func newGenerateConfigCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command { +func newGenerateConfigCommand(_ io.Reader, _, _ io.Writer) *cobra.Command { generateConf = ctl.NewGenerateConfigCommand(os.Stdin, os.Stdout, os.Stderr) confCmd := &cobra.Command{ Use: "generate-config", @@ -43,7 +43,3 @@ func newGenerateConfigCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra. return confCmd } - -func init() { - subcommandFns["generate-config"] = newGenerateConfigCommand -} diff --git a/cmd/import.go b/cmd/import.go index 7b4d00efd..c726b805e 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -65,7 +65,3 @@ omitted. If it is present then its format should be YYYY-MM-DDTHH:MM. return importCmd } - -func init() { - subcommandFns["import"] = newImportCommand -} diff --git a/cmd/inspect.go b/cmd/inspect.go index 096787337..f0f948807 100644 --- a/cmd/inspect.go +++ b/cmd/inspect.go @@ -27,7 +27,7 @@ import ( var inspector *ctl.InspectCommand -func newInspectCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command { +func newInspectCommand(_ io.Reader, _, _ io.Writer) *cobra.Command { inspector = ctl.NewInspectCommand(os.Stdin, os.Stdout, os.Stderr) inspectCmd := &cobra.Command{ @@ -51,7 +51,3 @@ Inspects a data file and provides stats. } return inspectCmd } - -func init() { - subcommandFns["inspect"] = newInspectCommand -} diff --git a/cmd/root.go b/cmd/root.go index fd55cbcef..64b8b1ca9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,10 +25,6 @@ import ( "github.com/spf13/viper" ) -// TODO maybe give this an Add method which will ensure two command -// with same name aren't added -var subcommandFns = map[string]func(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command{} - func NewRootCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command { productName := "Pilosa " + pilosa.Version if pilosa.EnterpriseEnabled { @@ -69,9 +65,15 @@ Build Time: ` + pilosa.BuildTime + "\n", rc.PersistentFlags().Bool("dry-run", false, "stop before executing") _ = rc.PersistentFlags().MarkHidden("dry-run") rc.PersistentFlags().StringP("config", "c", "", "Configuration file to read from.") - for _, subcomFn := range subcommandFns { - rc.AddCommand(subcomFn(stdin, stdout, stderr)) - } + + rc.AddCommand(newCheckCommand(stdin, stdout, stderr)) + rc.AddCommand(newConfigCommand(stdin, stdout, stderr)) + rc.AddCommand(newExportCommand(stdin, stdout, stderr)) + rc.AddCommand(newGenerateConfigCommand(stdin, stdout, stderr)) + rc.AddCommand(newImportCommand(stdin, stdout, stderr)) + rc.AddCommand(newInspectCommand(stdin, stdout, stderr)) + rc.AddCommand(newServeCmd(stdin, stdout, stderr)) + rc.SetOutput(stderr) return rc } diff --git a/cmd/server.go b/cmd/server.go index 83d18504c..d4834672e 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -50,7 +50,3 @@ on the configured port.`, ctl.BuildServerFlags(serveCmd, Server) return serveCmd } - -func init() { - subcommandFns["server"] = newServeCmd -} diff --git a/enterprise/b/btree.go b/enterprise/b/btree.go index c732e194d..3deb139b6 100644 --- a/enterprise/b/btree.go +++ b/enterprise/b/btree.go @@ -32,7 +32,6 @@ package b import ( - "fmt" "io" "sync" @@ -40,20 +39,12 @@ import ( ) const ( + // kx must be >= 2 kx = 128 //TODO benchmark tune this number if using custom key/value type(s). + // kd must be >= 1 kd = 128 //TODO benchmark tune this number if using custom key/value type(s). ) -func init() { - if kd < 1 { - panic(fmt.Errorf("kd %d: out of range", kd)) - } - - if kx < 2 { - panic(fmt.Errorf("kx %d: out of range", kx)) - } -} - var ( btDPool = sync.Pool{New: func() interface{} { return &d{} }} btEPool = btEpool{sync.Pool{New: func() interface{} { return &enumerator{} }}} diff --git a/enterprise/enterprise.go b/enterprise/enterprise.go index db9d6e2dd..f69ef1dd7 100644 --- a/enterprise/enterprise.go +++ b/enterprise/enterprise.go @@ -26,7 +26,7 @@ import ( "github.com/pilosa/pilosa/roaring" ) -func init() { +func init() { // nolint: gochecknoinits // Replace Bitmap constructor with B+Tree implementation roaring.NewFileBitmap = b.NewBTreeBitmap } diff --git a/gc.go b/gc.go index 23dd0f0d0..1456c22c2 100644 --- a/gc.go +++ b/gc.go @@ -23,12 +23,8 @@ type GCNotifier interface { AfterGC() <-chan struct{} } -func init() { - NopGCNotifier = &nopGCNotifier{} -} - // NopGCNotifier represents a GCNotifier that doesn't do anything. -var NopGCNotifier GCNotifier +var NopGCNotifier GCNotifier = &nopGCNotifier{} type nopGCNotifier struct{} diff --git a/http/client_test.go b/http/client_test.go index 2944a7586..0101bd2a6 100644 --- a/http/client_test.go +++ b/http/client_test.go @@ -29,13 +29,6 @@ import ( "github.com/pilosa/pilosa/test" ) -var defaultClient *gohttp.Client - -func init() { - defaultClient = http.GetHTTPClient(nil) - -} - // Test distributed TopN Row count across 3 nodes. func TestClient_MultiNode(t *testing.T) { c := test.MustRunCluster(t, 3, @@ -125,9 +118,9 @@ func TestClient_MultiNode(t *testing.T) { // Connect to each node to compare results. client := make([]*Client, 3) - client[0] = MustNewClient(c[0].URL(), defaultClient) - client[1] = MustNewClient(c[1].URL(), defaultClient) - client[2] = MustNewClient(c[2].URL(), defaultClient) + client[0] = MustNewClient(c[0].URL(), http.GetHTTPClient(nil)) + client[1] = MustNewClient(c[1].URL(), http.GetHTTPClient(nil)) + client[2] = MustNewClient(c[2].URL(), http.GetHTTPClient(nil)) topN := 4 queryRequest := &pilosa.QueryRequest{ @@ -191,7 +184,7 @@ func TestClient_Import(t *testing.T) { hldr.Row("i", "f", 0) // Send import request. - c := MustNewClient(host, defaultClient) + c := MustNewClient(host, http.GetHTTPClient(nil)) if err := c.Import(context.Background(), "i", "f", 0, []pilosa.Bit{ {RowID: 0, ColumnID: 1}, {RowID: 0, ColumnID: 5}, @@ -226,7 +219,7 @@ func TestClient_ImportValue(t *testing.T) { } // Send import request. - c := MustNewClient(host, defaultClient) + c := MustNewClient(host, http.GetHTTPClient(nil)) if err := c.ImportValue(context.Background(), "i", "f", 0, []pilosa.FieldValue{ {ColumnID: 1, Value: -10}, {ColumnID: 2, Value: 20}, @@ -287,7 +280,7 @@ func TestClient_FragmentBlocks(t *testing.T) { // Set a bit on a different shard. hldr.SetBit("i", "f", 0, 1) - c := MustNewClient(cmd.URL(), defaultClient) + c := MustNewClient(cmd.URL(), http.GetHTTPClient(nil)) blocks, err := c.FragmentBlocks(context.Background(), nil, "i", "f", 0) if err != nil { t.Fatal(err) diff --git a/logger.go b/logger.go index 28b35b999..074da8a36 100644 --- a/logger.go +++ b/logger.go @@ -28,12 +28,8 @@ type Logger interface { Debugf(format string, v ...interface{}) } -func init() { - NopLogger = &nopLogger{} -} - // NopLogger represents a Logger that doesn't do anything. -var NopLogger Logger +var NopLogger Logger = &nopLogger{} type nopLogger struct{} diff --git a/server/server.go b/server/server.go index 9060256af..2433def7a 100644 --- a/server/server.go +++ b/server/server.go @@ -44,10 +44,6 @@ import ( "github.com/pkg/errors" ) -func init() { - rand.Seed(time.Now().UTC().UnixNano()) -} - type loggerLogger interface { pilosa.Logger Logger() *log.Logger @@ -126,6 +122,9 @@ func NewCommand(stdin io.Reader, stdout, stderr io.Writer, opts ...CommandOption func (m *Command) Start() (err error) { defer close(m.Started) + // Seed random number generator + rand.Seed(time.Now().UTC().UnixNano()) + // SetupServer err = m.SetupServer() if err != nil { diff --git a/stats.go b/stats.go index 130a91a64..8f23c77aa 100644 --- a/stats.go +++ b/stats.go @@ -22,10 +22,6 @@ import ( "time" ) -func init() { - NopStatsClient = &nopStatsClient{} -} - // Expvar global expvar map. var Expvar = expvar.NewMap("index") @@ -66,7 +62,7 @@ type StatsClient interface { } // NopStatsClient represents a client that doesn't do anything. -var NopStatsClient StatsClient +var NopStatsClient StatsClient = &nopStatsClient{} type nopStatsClient struct{} diff --git a/version.go b/version.go index 04096f7cc..4164dee32 100644 --- a/version.go +++ b/version.go @@ -19,7 +19,9 @@ var EnterpriseEnabled = false var Version = "v0.0.0" var BuildTime = "not recorded" -func init() { +// init sets the EnterpriseEnabled bool, based on the Enterprise string. +// This is needed because bools cannot be set with ldflags. +func init() { // nolint: gochecknoinits if Enterprise == "1" { EnterpriseEnabled = true }