diff --git a/examples/complex-etcd/main.go b/examples/complex-etcd/main.go index 795dfc6c..efdca4c9 100644 --- a/examples/complex-etcd/main.go +++ b/examples/complex-etcd/main.go @@ -70,7 +70,10 @@ func main() { Prefix: false, Key: "single_key", } - provider := etcd.Provider(providerCfg) + provider, err := etcd.Provider(providerCfg) + if err != nil { + log.Fatalf("Failed to instantiate etcd provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) @@ -109,7 +112,10 @@ func main() { Prefix: true, Key: "parent", } - provider = etcd.Provider(providerCfg) + provider, err = etcd.Provider(providerCfg) + if err != nil { + log.Fatalf("Failed to instantiate etcd provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) @@ -154,7 +160,10 @@ func main() { Key: "child", } - provider = etcd.Provider(providerCfg) + provider, err = etcd.Provider(providerCfg) + if err != nil { + log.Fatalf("Failed to instantiate etcd provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) @@ -201,7 +210,10 @@ func main() { Key: "child", } - provider = etcd.Provider(providerCfg) + provider, err = etcd.Provider(providerCfg) + if err != nil { + log.Fatalf("Failed to instantiate etcd provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) @@ -241,7 +253,10 @@ func main() { Key: "child", } - provider = etcd.Provider(providerCfg) + provider, err = etcd.Provider(providerCfg) + if err != nil { + log.Fatalf("Failed to instantiate etcd provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) diff --git a/examples/read-appconfig/main.go b/examples/read-appconfig/main.go index 4808e993..e2be5dc4 100644 --- a/examples/read-appconfig/main.go +++ b/examples/read-appconfig/main.go @@ -13,12 +13,16 @@ import ( var k = koanf.New(".") func main() { - provider := appconfig.Provider(appconfig.Config{ + provider, err := appconfig.Provider(appconfig.Config{ Application: os.Getenv("AWS_APPCONFIG_APPLICATION"), ClientID: os.Getenv("AWS_APPCONFIG_CLIENT_ID"), Configuration: os.Getenv("AWS_APPCONFIG_CONFIG_NAME"), Environment: os.Getenv("AWS_APPCONFIG_ENVIRONMENT"), }) + if err != nil { + log.Fatalf("Failed to instantiate appconfig provider: %v", err) + } + // Load the provider and parse configuration as JSON. if err := k.Load(provider, json.Parser()); err != nil { log.Fatalf("error loading config: %v", err) diff --git a/examples/read-consul/main.go b/examples/read-consul/main.go index d0acaa1a..5917ec04 100644 --- a/examples/read-consul/main.go +++ b/examples/read-consul/main.go @@ -54,12 +54,15 @@ func main() { log.Printf("Couldn't put key.") } - provider := consul.Provider(consul.Config{ + provider, err := consul.Provider(consul.Config{ Key: sKey, Recurse: false, Detailed: false, Cfg: api.DefaultConfig(), }) + if err != nil { + log.Fatalf("Failed to instantiate consul provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) @@ -91,12 +94,15 @@ func main() { log.Fatalf("error loading config: %v", err) } - provider = consul.Provider(consul.Config{ + provider, err = consul.Provider(consul.Config{ Key: "parent", Recurse: true, Detailed: false, Cfg: api.DefaultConfig(), }) + if err != nil { + log.Fatalf("Failed to instantiate consul provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) @@ -134,12 +140,15 @@ func main() { log.Fatalf("error loading config: %v", err) } - provider = consul.Provider(consul.Config{ + provider, err = consul.Provider(consul.Config{ Key: "child", Recurse: true, Detailed: false, Cfg: api.DefaultConfig(), }) + if err != nil { + log.Fatalf("Failed to instantiate consul provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) @@ -181,12 +190,15 @@ func main() { sFlags := uint64(42) sVal = "father" - provider = consul.Provider(consul.Config{ + provider, err = consul.Provider(consul.Config{ Key: sKey, Recurse: false, Detailed: true, Cfg: api.DefaultConfig(), }) + if err != nil { + log.Fatalf("Failed to instantiate consul provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { fmt.Printf("error loading config: %v", err) @@ -213,12 +225,15 @@ func main() { sKey = "parent" - provider = consul.Provider(consul.Config{ + provider, err = consul.Provider(consul.Config{ Key: sKey, Recurse: true, Detailed: true, Cfg: api.DefaultConfig(), }) + if err != nil { + log.Fatalf("Failed to instantiate consul provider: %v", err) + } if err := kCheck.Load(provider, nil); err != nil { fmt.Printf("error loading config: %v", err) @@ -255,12 +270,15 @@ func main() { // Watch test sKey = "parent" - provider = consul.Provider(consul.Config{ + provider, err = consul.Provider(consul.Config{ Key: sKey, Recurse: true, Detailed: false, Cfg: api.DefaultConfig(), }) + if err != nil { + log.Fatalf("Failed to instantiate consul provider: %v", err) + } // Getting the old value kCheck.Load(provider, nil) diff --git a/examples/read-vault/main.go b/examples/read-vault/main.go index c03442af..103abbe1 100644 --- a/examples/read-vault/main.go +++ b/examples/read-vault/main.go @@ -14,7 +14,7 @@ import ( var k = koanf.New(".") func main() { - provider := vault.Provider(vault.Config{ + provider, err := vault.Provider(vault.Config{ Address: os.Getenv("VAULT_ADDRESS"), Token: os.Getenv("VAULT_TOKEN"), Path: "secret/data/my-app", @@ -25,6 +25,9 @@ func main() { // k.String("data.YOUR_KEY") etc. instead of k.String("YOUR_KEY"). ExcludeMeta: true, }) + if err != nil { + log.Fatalf("Failed to instantiate vault provider: %v", err) + } // Load mapped config from Vault storage. if err := k.Load(provider, nil); err != nil { log.Fatalf("error loading config: %v", err) diff --git a/providers/appconfig/appconfig.go b/providers/appconfig/appconfig.go index 64359b1f..1664e748 100644 --- a/providers/appconfig/appconfig.go +++ b/providers/appconfig/appconfig.go @@ -66,10 +66,10 @@ type AppConfig struct { } // Provider returns an AWS AppConfig provider. -func Provider(cfg Config) *AppConfig { +func Provider(cfg Config) (*AppConfig, error) { c, err := config.LoadDefaultConfig(context.TODO()) if err != nil { - return nil + return nil, err } if cfg.AWSRegion != "" { @@ -89,7 +89,7 @@ func Provider(cfg Config) *AppConfig { } client := appconfig.NewFromConfig(c) - return &AppConfig{client: client, config: cfg} + return &AppConfig{client: client, config: cfg}, nil } // ProviderWithClient returns an AWS AppConfig provider diff --git a/providers/consul/consul.go b/providers/consul/consul.go index e6350df6..74838964 100644 --- a/providers/consul/consul.go +++ b/providers/consul/consul.go @@ -37,13 +37,13 @@ type Consul struct { } // Provider returns an instance of the Consul provider. -func Provider(cfg Config) *Consul { +func Provider(cfg Config) (*Consul, error) { c, err := api.NewClient(cfg.Cfg) if err != nil { - return nil + return nil, err } - return &Consul{client: c, cfg: cfg} + return &Consul{client: c, cfg: cfg}, nil } // ReadBytes is not supported by the Consul provider. diff --git a/providers/etcd/etcd.go b/providers/etcd/etcd.go index c75da160..bae27504 100644 --- a/providers/etcd/etcd.go +++ b/providers/etcd/etcd.go @@ -35,7 +35,7 @@ type Etcd struct { } // Provider returns a provider that takes etcd config. -func Provider(cfg Config) *Etcd { +func Provider(cfg Config) (*Etcd, error) { eCfg := clientv3.Config{ Endpoints: cfg.Endpoints, DialTimeout: cfg.DialTimeout, @@ -43,10 +43,10 @@ func Provider(cfg Config) *Etcd { c, err := clientv3.New(eCfg) if err != nil { - return nil + return nil, err } - return &Etcd{client: c, cfg: cfg} + return &Etcd{client: c, cfg: cfg}, nil } // ReadBytes is not supported by etcd provider. diff --git a/providers/parameterstore/parameterstore.go b/providers/parameterstore/parameterstore.go index 04df5f04..55bf24ca 100644 --- a/providers/parameterstore/parameterstore.go +++ b/providers/parameterstore/parameterstore.go @@ -48,12 +48,12 @@ type ParameterStore[T Input] struct { // - AWS_ACCESS_KEY_ID // - AWS_SECRET_ACCESS_KEY // - AWS_SESSION_TOKEN -func Provider[T Input](config Config[T]) *ParameterStore[T] { +func Provider[T Input](config Config[T]) (*ParameterStore[T], error) { c, err := awsconfig.LoadDefaultConfig(context.TODO()) if err != nil { - return nil + return nil, err } - return ProviderWithClient[T](config, ssm.NewFromConfig(c)) + return ProviderWithClient[T](config, ssm.NewFromConfig(c)), nil } // ProviderWithClient returns a ParameterStore provider diff --git a/providers/vault/vault.go b/providers/vault/vault.go index 3b5efed9..431abdf8 100644 --- a/providers/vault/vault.go +++ b/providers/vault/vault.go @@ -56,21 +56,21 @@ type Vault struct { } // Provider returns a provider that takes a Vault config. -func Provider(cfg Config) *Vault { +func Provider(cfg Config) (*Vault, error) { httpClient := &http.Client{Timeout: cfg.Timeout, Transport: cfg.Transport} client, err := api.NewClient(&api.Config{Address: cfg.Address, HttpClient: httpClient}) if err != nil { - return nil + return nil, err } if cfg.AuthMethod != nil { if _, err := client.Auth().Login(context.Background(), cfg.AuthMethod); err != nil { - return nil + return nil, err } } else { client.SetToken(cfg.Token) } - return &Vault{client: client, cfg: cfg} + return &Vault{client: client, cfg: cfg}, nil } // ReadBytes is not supported by the vault provider.