support ability to set store, address and namespace via flags and env vars (#1092)
This commit is contained in:
@@ -96,6 +96,16 @@ func validateOptions(account, token, namespace string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (w *workersKV) Init(opts ...store.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&w.options)
|
||||
}
|
||||
if len(w.options.Namespace) > 0 {
|
||||
w.namespace = w.options.Namespace
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// In the cloudflare workers KV implemention, List() doesn't guarantee
|
||||
// anything as the workers API is eventually consistent.
|
||||
func (w *workersKV) List() ([]*store.Record, error) {
|
||||
@@ -308,7 +318,7 @@ func NewStore(opts ...store.Option) store.Store {
|
||||
}
|
||||
|
||||
if len(namespace) == 0 {
|
||||
namespace = getNamespace(options.Context)
|
||||
namespace = options.Namespace
|
||||
}
|
||||
|
||||
// validate options are not blank or log.Fatal
|
||||
|
@@ -25,10 +25,6 @@ func getAccount(ctx context.Context) string {
|
||||
return getOption(ctx, "CF_ACCOUNT_ID")
|
||||
}
|
||||
|
||||
func getNamespace(ctx context.Context) string {
|
||||
return getOption(ctx, "KV_NAMESPACE_ID")
|
||||
}
|
||||
|
||||
// Token sets the cloudflare api token
|
||||
func Token(t string) store.Option {
|
||||
return func(o *store.Options) {
|
||||
@@ -52,9 +48,6 @@ func Account(id string) store.Option {
|
||||
// Namespace sets the KV namespace
|
||||
func Namespace(ns string) store.Option {
|
||||
return func(o *store.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
}
|
||||
o.Context = context.WithValue(o.Context, "KV_NAMESPACE_ID", ns)
|
||||
o.Namespace = ns
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user