2019-10-23 22:31:36 +01:00
|
|
|
package cloudflare
|
|
|
|
|
|
|
|
import (
|
2019-12-16 14:38:51 +00:00
|
|
|
"context"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-10-23 22:31:36 +01:00
|
|
|
)
|
|
|
|
|
2019-12-16 14:38:51 +00: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")
|
|
|
|
}
|
|
|
|
|
2019-10-23 22:31:36 +01:00
|
|
|
// Token sets the cloudflare api token
|
2019-12-16 14:38:51 +00: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-23 22:31:36 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:12:45 +01:00
|
|
|
// Account sets the cloudflare account id
|
2019-12-16 14:38:51 +00: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-23 22:31:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace sets the KV namespace
|
2019-12-16 14:38:51 +00:00
|
|
|
func Namespace(ns string) store.Option {
|
|
|
|
return func(o *store.Options) {
|
2020-01-08 12:11:31 +00:00
|
|
|
o.Namespace = ns
|
2019-12-16 14:38:51 +00:00
|
|
|
}
|
2019-10-23 22:31:36 +01:00
|
|
|
}
|