name.go 3.08 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
type NamePublishSettings struct {
	ValidTime time.Duration
	Key       string
16 17 18 19

	TTL *time.Duration

	AllowOffline bool
20 21 22
}

type NameResolveSettings struct {
Łukasz Magiera's avatar
Łukasz Magiera committed
23 24 25
	Local bool
	Cache bool

26
	ResolveOpts []ropts.ResolveOpt
27 28 29 30 31 32 33
}

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

func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) {
	options := &NamePublishSettings{
34
		ValidTime: DefaultNameValidTime,
35
		Key:       "self",
36 37

		AllowOffline: false,
38 39 40 41 42 43 44 45 46 47 48 49 50 51
	}

	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
52 53
		Local: false,
		Cache: true,
54 55 56 57 58 59 60 61 62 63 64 65
	}

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

	return options, nil
}

66
type nameOpts struct{}
67

68 69 70 71
var Name nameOpts

// ValidTime is an option for Name.Publish which specifies for how long the
// entry will remain valid. Default value is 24h
72
func (nameOpts) ValidTime(validTime time.Duration) NamePublishOption {
73 74 75 76 77 78
	return func(settings *NamePublishSettings) error {
		settings.ValidTime = validTime
		return nil
	}
}

79 80 81 82 83
// 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.
84
func (nameOpts) Key(key string) NamePublishOption {
85 86 87 88 89 90
	return func(settings *NamePublishSettings) error {
		settings.Key = key
		return nil
	}
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
// AllowOffline is an option for Name.Publish which specifies whether to allow
// publishing when the node is offline. Default value is false
func (nameOpts) AllowOffline(allow bool) NamePublishOption {
	return func(settings *NamePublishSettings) error {
		settings.AllowOffline = allow
		return nil
	}
}

// TTL is an option for Name.Publish which specifies the time duration the
// published record should be cached for (caution: experimental).
func (nameOpts) TTL(ttl time.Duration) NamePublishOption {
	return func(settings *NamePublishSettings) error {
		settings.TTL = &ttl
		return nil
	}
}

109 110
// Local is an option for Name.Resolve which specifies if the lookup should be
// offline. Default value is false
111
func (nameOpts) Local(local bool) NameResolveOption {
112 113 114 115 116 117
	return func(settings *NameResolveSettings) error {
		settings.Local = local
		return nil
	}
}

118 119
// Cache is an option for Name.Resolve which specifies if cache should be used.
// Default value is true
120
func (nameOpts) Cache(cache bool) NameResolveOption {
121
	return func(settings *NameResolveSettings) error {
122
		settings.Cache = cache
123 124 125
		return nil
	}
}
Łukasz Magiera's avatar
Łukasz Magiera committed
126

127 128
//
func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
129
	return func(settings *NameResolveSettings) error {
130
		settings.ResolveOpts = append(settings.ResolveOpts, opt)
Łukasz Magiera's avatar
Łukasz Magiera committed
131 132 133
		return nil
	}
}