Commit 7a786c55 authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi: name/key functional options

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 4fbdf56d
......@@ -99,14 +99,25 @@ type DagAPI interface {
}
type NameAPI interface {
Publish(ctx context.Context, path Path, validTime time.Duration, key string) (*IpnsEntry, error)
Resolve(ctx context.Context, name string, recursive bool, local bool, nocache bool) (Path, error)
Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (*IpnsEntry, error)
WithValidTime(validTime time.Duration) options.NamePublishOption
WithKey(key string) options.NamePublishOption
Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
WithRecursive(recursive bool) options.NameResolveOption
WithLocal(local bool) options.NameResolveOption
WithNoCache(nocache bool) options.NameResolveOption
}
type KeyAPI interface {
Generate(ctx context.Context, name string, algorithm string, size int) (string, error)
Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (string, error)
WithAlgorithm(algorithm string) options.KeyGenerateOption
WithSize(size int) options.KeyGenerateOption
Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (string, bool, error)
WithForce(force bool) options.KeyRenameOption
List(ctx context.Context) (map[string]string, error) //TODO: better key type?
Rename(ctx context.Context, oldName string, newName string, force bool) (string, bool, error)
Remove(ctx context.Context, name string) (string, error)
}
......
package options
type KeyGenerateSettings struct {
Algorithm string
Size int
}
type KeyRenameSettings struct {
Force bool
}
type KeyGenerateOption func(*KeyGenerateSettings) error
type KeyRenameOption func(*KeyRenameSettings) error
func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) {
options := &KeyGenerateSettings{
Algorithm: "rsa",
Size: 0,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
func KeyRenameOptions(opts ...KeyRenameOption) (*KeyRenameSettings, error) {
options := &KeyRenameSettings{
Force: false,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type KeyOptions struct{}
func (api *KeyOptions) WithAlgorithm(algorithm string) KeyGenerateOption {
return func(settings *KeyGenerateSettings) error {
settings.Algorithm = algorithm
return nil
}
}
func (api *KeyOptions) WithSize(size int) KeyGenerateOption {
return func(settings *KeyGenerateSettings) error {
settings.Size = size
return nil
}
}
func (api *KeyOptions) WithForce(force bool) KeyRenameOption {
return func(settings *KeyRenameSettings) error {
settings.Force = force
return nil
}
}
package options
import (
"time"
)
type NamePublishSettings struct {
ValidTime time.Duration
Key string
}
type NameResolveSettings struct {
Recursive bool
Local bool
Nocache bool
}
type NamePublishOption func(*NamePublishSettings) error
type NameResolveOption func(*NameResolveSettings) error
func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) {
options := &NamePublishSettings{
ValidTime: 24 * time.Hour,
Key: "self",
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) {
options := &NameResolveSettings{
Recursive: false,
Local: false,
Nocache: false,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type NameOptions struct{}
func (api *NameOptions) WithValidTime(validTime time.Duration) NamePublishOption {
return func(settings *NamePublishSettings) error {
settings.ValidTime = validTime
return nil
}
}
func (api *NameOptions) WithKey(key string) NamePublishOption {
return func(settings *NamePublishSettings) error {
settings.Key = key
return nil
}
}
func (api *NameOptions) WithRecursive(recursive bool) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.Recursive = recursive
return nil
}
}
func (api *NameOptions) WithLocal(local bool) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.Local = local
return nil
}
}
func (api *NameOptions) WithNoCache(nocache bool) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.Nocache = nocache
return nil
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment