diff --git a/core/commands/routing.go b/core/commands/routing.go index 2d578fcb60f9..76672c6f2fab 100644 --- a/core/commands/routing.go +++ b/core/commands/routing.go @@ -10,6 +10,8 @@ import ( cmdenv "github.com/ipfs/kubo/core/commands/cmdenv" + iface "github.com/ipfs/boxo/coreiface" + "github.com/ipfs/boxo/coreiface/options" dag "github.com/ipfs/boxo/ipld/merkledag" path "github.com/ipfs/boxo/path" cid "github.com/ipfs/go-cid" @@ -19,6 +21,16 @@ import ( routing "github.com/libp2p/go-libp2p/core/routing" ) +var ( + errAllowOffline = errors.New("can't put while offline: pass `--allow-offline` to override") +) + +const ( + dhtVerboseOptionName = "verbose" + numProvidersOptionName = "num-providers" + allowOfflineOptionName = "allow-offline" +) + var RoutingCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Issue routing commands.", @@ -34,14 +46,6 @@ var RoutingCmd = &cmds.Command{ }, } -const ( - dhtVerboseOptionName = "verbose" -) - -const ( - numProvidersOptionName = "num-providers" -) - var findProvidersRoutingCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Find peers that can provide a specific value, given a key.", @@ -420,6 +424,9 @@ identified by QmFoo. cmds.StringArg("key", true, false, "The key to store the value at."), cmds.FileArg("value-file", true, false, "A path to a file containing the value to store.").EnableStdin(), }, + Options: []cmds.Option{ + cmds.BoolOption(allowOfflineOptionName, "When offline, save the IPNS record to the the local datastore without broadcasting to the network instead of simply failing."), + }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { api, err := cmdenv.GetApi(env, req) if err != nil { @@ -437,13 +444,22 @@ identified by QmFoo. return err } - err = api.Routing().Put(req.Context, req.Arguments[0], data) + allowOffline, _ := req.Options[allowOfflineOptionName].(bool) + + opts := []options.RoutingPutOption{ + options.Put.AllowOffline(allowOffline), + } + + err = api.Routing().Put(req.Context, req.Arguments[0], data, opts...) if err != nil { return err } id, err := api.Key().Self(req.Context) if err != nil { + if err == iface.ErrOffline { + err = errAllowOffline + } return err } diff --git a/core/coreapi/routing.go b/core/coreapi/routing.go index 76d969316796..95b50aa631d2 100644 --- a/core/coreapi/routing.go +++ b/core/coreapi/routing.go @@ -5,6 +5,7 @@ import ( "errors" coreiface "github.com/ipfs/boxo/coreiface" + caopts "github.com/ipfs/boxo/coreiface/options" "github.com/ipfs/boxo/path" peer "github.com/libp2p/go-libp2p/core/peer" ) @@ -24,9 +25,15 @@ func (r *RoutingAPI) Get(ctx context.Context, key string) ([]byte, error) { return r.routing.GetValue(ctx, dhtKey) } -func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte) error { - if !r.nd.IsOnline { - return coreiface.ErrOffline +func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte, opts ...caopts.RoutingPutOption) error { + options, err := caopts.RoutingPutOptions(opts...) + if err != nil { + return err + } + + err = r.checkOnline(options.AllowOffline) + if err != nil { + return err } dhtKey, err := normalizeKey(key)