mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-22 21:21:43 +08:00
coreapi name: add some missing options
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -13,6 +13,10 @@ const (
|
||||
type NamePublishSettings struct {
|
||||
ValidTime time.Duration
|
||||
Key string
|
||||
|
||||
TTL *time.Duration
|
||||
|
||||
AllowOffline bool
|
||||
}
|
||||
|
||||
type NameResolveSettings struct {
|
||||
@ -29,6 +33,8 @@ func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error)
|
||||
options := &NamePublishSettings{
|
||||
ValidTime: DefaultNameValidTime,
|
||||
Key: "self",
|
||||
|
||||
AllowOffline: false,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@ -82,6 +88,24 @@ func (nameOpts) Key(key string) NamePublishOption {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// Local is an option for Name.Resolve which specifies if the lookup should be
|
||||
// offline. Default value is false
|
||||
func (nameOpts) Local(local bool) NameResolveOption {
|
||||
|
@ -45,6 +45,9 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
|
||||
n := api.node
|
||||
|
||||
if !n.OnlineMode() {
|
||||
if !options.AllowOffline {
|
||||
return nil, coreiface.ErrOffline
|
||||
}
|
||||
err := n.SetupOfflineRouting()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -65,6 +68,10 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if options.TTL != nil {
|
||||
ctx = context.WithValue(ctx, "ipns-publish-ttl", *options.TTL)
|
||||
}
|
||||
|
||||
eol := time.Now().Add(options.ValidTime)
|
||||
err = n.Namesys.PublishWithEOL(ctx, k, pth, eol)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user