name.go 2.43 KB
Newer Older
1 2 3 4
package options

import (
	"time"
5 6

	ropts "github.com/ipfs/go-ipfs/namesys/opts"
7 8
)

9 10 11 12
const (
	DefaultNameValidTime = 24 * time.Hour
)

13 14 15 16 17 18
type NamePublishSettings struct {
	ValidTime time.Duration
	Key       string
}

type NameResolveSettings struct {
Łukasz Magiera's avatar
Łukasz Magiera committed
19 20 21
	Local bool
	Cache bool

22
	ResolveOpts []ropts.ResolveOpt
23 24 25 26 27 28 29
}

type NamePublishOption func(*NamePublishSettings) error
type NameResolveOption func(*NameResolveSettings) error

func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) {
	options := &NamePublishSettings{
30
		ValidTime: DefaultNameValidTime,
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
		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{
Łukasz Magiera's avatar
Łukasz Magiera committed
46 47
		Local: false,
		Cache: true,
48 49 50 51 52 53 54 55 56 57 58 59
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}

	return options, nil
}

60
type nameOpts struct{}
61

62 63 64 65
var Name nameOpts

// ValidTime is an option for Name.Publish which specifies for how long the
// entry will remain valid. Default value is 24h
66
func (nameOpts) ValidTime(validTime time.Duration) NamePublishOption {
67 68 69 70 71 72
	return func(settings *NamePublishSettings) error {
		settings.ValidTime = validTime
		return nil
	}
}

73 74 75 76 77
// Key is an option for Name.Publish which specifies the key to use for
// publishing. Default value is "self" which is the node's own PeerID.
// The key parameter must be either PeerID or keystore key alias.
//
// You can use KeyAPI to list and generate more names and their respective keys.
78
func (nameOpts) Key(key string) NamePublishOption {
79 80 81 82 83 84
	return func(settings *NamePublishSettings) error {
		settings.Key = key
		return nil
	}
}

85 86
// Local is an option for Name.Resolve which specifies if the lookup should be
// offline. Default value is false
87
func (nameOpts) Local(local bool) NameResolveOption {
88 89 90 91 92 93
	return func(settings *NameResolveSettings) error {
		settings.Local = local
		return nil
	}
}

94 95
// Cache is an option for Name.Resolve which specifies if cache should be used.
// Default value is true
96
func (nameOpts) Cache(cache bool) NameResolveOption {
97
	return func(settings *NameResolveSettings) error {
98
		settings.Cache = cache
99 100 101
		return nil
	}
}
Łukasz Magiera's avatar
Łukasz Magiera committed
102

103 104
//
func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
105
	return func(settings *NameResolveSettings) error {
106
		settings.ResolveOpts = append(settings.ResolveOpts, opt)
Łukasz Magiera's avatar
Łukasz Magiera committed
107 108 109
		return nil
	}
}