From a4638db773fbb3886681b7e551a76120037f18a6 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Mon, 29 Mar 2021 17:21:12 -0500 Subject: [PATCH] Make configuration global, add helper functions --- cmd/config.go | 17 +++++++++++++++-- cmd/main.go | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 9e3eeff..d37b824 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -23,8 +23,21 @@ var ( DefaultConfig = Config{ userAgent: initUserAgent(), } + globalConfig *Config = NewConfig() ) +func NewConfig() *Config { + c := new(Config) + c.userAgent = initUserAgent() + c.Debug = false + c.OutputFormat = "simple" + return c +} + +func GetConfig() *Config { + return globalConfig +} + // Validate configuration struct func (c *Config) Validate() error { var err error @@ -69,7 +82,7 @@ func (c *Config) MergeWithEnv() { func LoadConfig(s string) (*Config, error) { var err error - var c *Config = &Config{} + var c *Config = GetConfig() *c = DefaultConfig @@ -83,7 +96,7 @@ func LoadConfig(s string) (*Config, error) { func LoadConfigFile(configPath string) (*Config, error) { var err error - var c *Config = &Config{} + var c *Config = GetConfig() *c = DefaultConfig diff --git a/cmd/main.go b/cmd/main.go index a188237..cfce3be 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,7 +27,7 @@ var BuildDate string var GitCommit string func Main(args []string) (int, error) { - var config *Config + var config *Config = GetConfig() var err error var i int