From eb3c196ff91b88c2400cad70837be5e3e58e9409 Mon Sep 17 00:00:00 2001 From: Nick Tan Date: Thu, 14 Sep 2023 23:57:56 -0700 Subject: [PATCH] breaking change: #233 return error struct to avoid panic at runtime --- providers/appconfig/appconfig.go | 6 +++--- providers/consul/consul.go | 6 +++--- providers/etcd/etcd.go | 6 +++--- providers/parameterstore/parameterstore.go | 6 +++--- providers/vault/vault.go | 8 ++++---- 5 files changed, 16 insertions(+), 16 deletions(-) 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.