Make configuration global, add helper functions

This commit is contained in:
Ryan Cavicchioni 2021-03-29 17:21:12 -05:00
parent 3c0254c9a1
commit a4638db773
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 16 additions and 3 deletions

View File

@ -23,8 +23,21 @@ var (
DefaultConfig = Config{ DefaultConfig = Config{
userAgent: initUserAgent(), 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 // Validate configuration struct
func (c *Config) Validate() error { func (c *Config) Validate() error {
var err error var err error
@ -69,7 +82,7 @@ func (c *Config) MergeWithEnv() {
func LoadConfig(s string) (*Config, error) { func LoadConfig(s string) (*Config, error) {
var err error var err error
var c *Config = &Config{} var c *Config = GetConfig()
*c = DefaultConfig *c = DefaultConfig
@ -83,7 +96,7 @@ func LoadConfig(s string) (*Config, error) {
func LoadConfigFile(configPath string) (*Config, error) { func LoadConfigFile(configPath string) (*Config, error) {
var err error var err error
var c *Config = &Config{} var c *Config = GetConfig()
*c = DefaultConfig *c = DefaultConfig

View File

@ -27,7 +27,7 @@ var BuildDate string
var GitCommit string var GitCommit string
func Main(args []string) (int, error) { func Main(args []string) (int, error) {
var config *Config var config *Config = GetConfig()
var err error var err error
var i int var i int