micro/store/cloudflare/options.go

61 lines
1.2 KiB
Go
Raw Normal View History

2019-10-24 00:31:36 +03:00
package cloudflare
import (
2019-12-16 17:38:51 +03:00
"context"
"github.com/micro/go-micro/store"
2019-10-24 00:31:36 +03:00
)
2019-12-16 17:38:51 +03:00
func getOption(ctx context.Context, key string) string {
if ctx == nil {
return ""
}
val, ok := ctx.Value(key).(string)
if !ok {
return ""
}
return val
}
func getToken(ctx context.Context) string {
return getOption(ctx, "CF_API_TOKEN")
}
func getAccount(ctx context.Context) string {
return getOption(ctx, "CF_ACCOUNT_ID")
}
func getNamespace(ctx context.Context) string {
return getOption(ctx, "KV_NAMESPACE_ID")
}
2019-10-24 00:31:36 +03:00
// Token sets the cloudflare api token
2019-12-16 17:38:51 +03:00
func Token(t string) store.Option {
return func(o *store.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, "CF_API_TOKEN", t)
}
2019-10-24 00:31:36 +03:00
}
2019-10-24 01:12:45 +03:00
// Account sets the cloudflare account id
2019-12-16 17:38:51 +03:00
func Account(id string) store.Option {
return func(o *store.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, "CF_ACCOUNT_ID", id)
}
2019-10-24 00:31:36 +03:00
}
// Namespace sets the KV namespace
2019-12-16 17:38:51 +03:00
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)
}
2019-10-24 00:31:36 +03:00
}