From 559d2b3b97770858de741b1b13fb41a308768843 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Fri, 11 Feb 2022 09:09:27 +0200 Subject: [PATCH] aperture: add NewConfig function Add a NewConfig func so that all pointer variables in the config object can be initialised so that we can avoid needing to do nil checks everywhere. --- aperture.go | 11 ++++++----- config.go | 17 +++++++++++++---- hashmail_server_test.go | 2 ++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/aperture.go b/aperture.go index 12233d0..1afd01c 100644 --- a/aperture.go +++ b/aperture.go @@ -181,9 +181,10 @@ func (a *Aperture) Start(errChan chan error) error { var err error // Start the prometheus exporter. - if err := StartPrometheusExporter(a.cfg.Prometheus); err != nil { - return fmt.Errorf("unable to start the prometheus exporter: "+ - "%v", err) + err = StartPrometheusExporter(a.cfg.Prometheus) + if err != nil { + return fmt.Errorf("unable to start the prometheus "+ + "exporter: %v", err) } // Initialize our etcd client. @@ -281,7 +282,7 @@ func (a *Aperture) Start(errChan chan error) error { // will only be reached through the onion services, which already // provide encryption, so running this additional HTTP server should be // relatively safe. - if a.cfg.Tor != nil && (a.cfg.Tor.V2 || a.cfg.Tor.V3) { + if a.cfg.Tor.V2 || a.cfg.Tor.V3 { torController, err := initTorListener(a.cfg, a.etcdClient) if err != nil { return err @@ -363,7 +364,7 @@ func fileExists(name string) bool { func getConfig() (*Config, error) { // Pre-parse command line flags to determine whether we've been pointed // to a custom config file. - cfg := &Config{} + cfg := NewConfig() if _, err := flags.Parse(cfg); err != nil { return nil, err } diff --git a/config.go b/config.go index 3f0ddff..b1c0417 100644 --- a/config.go +++ b/config.go @@ -128,10 +128,8 @@ type Config struct { } func (c *Config) validate() error { - if c.Authenticator != nil { - if err := c.Authenticator.validate(); err != nil { - return err - } + if err := c.Authenticator.validate(); err != nil { + return err } if c.ListenAddr == "" { @@ -140,3 +138,14 @@ func (c *Config) validate() error { return nil } + +// NewConfig initializes a new Config variable. +func NewConfig() *Config { + return &Config{ + Etcd: &EtcdConfig{}, + Authenticator: &AuthConfig{}, + Tor: &TorConfig{}, + HashMail: &HashMailConfig{}, + Prometheus: &PrometheusConfig{}, + } +} diff --git a/hashmail_server_test.go b/hashmail_server_test.go index 6158996..0c99b0a 100644 --- a/hashmail_server_test.go +++ b/hashmail_server_test.go @@ -163,6 +163,8 @@ func setupAperture(t *testing.T) { MessageRate: time.Millisecond, MessageBurstAllowance: math.MaxUint32, }, + Prometheus: &PrometheusConfig{}, + Tor: &TorConfig{}, } aperture := NewAperture(apertureCfg) errChan := make(chan error)