coreapi: implement --local with Offline option

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera 2018-12-10 15:26:27 +01:00
parent 6e18a6b857
commit 2c2f9f2bed
19 changed files with 70 additions and 70 deletions

View File

@ -139,7 +139,7 @@ You can now check what blocks have been created by:
return nil
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -160,7 +160,6 @@ You can now check what blocks have been created by:
inline, _ := req.Options[inlineOptionName].(bool)
inlineLimit, _ := req.Options[inlineLimitOptionName].(int)
pathName, _ := req.Options[stdinPathName].(string)
local, _ := req.Options["local"].(bool)
hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)]
if !ok {
@ -179,7 +178,6 @@ You can now check what blocks have been created by:
options.Unixfs.Pin(dopin),
options.Unixfs.HashOnly(hash),
options.Unixfs.Local(local),
options.Unixfs.FsCache(fscache),
options.Unixfs.Nocopy(nocopy),

View File

@ -60,7 +60,7 @@ on raw IPFS blocks. It outputs the following to stdout:
cmdkit.StringArg("key", true, false, "The base58 multihash of an existing block to stat.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -102,7 +102,7 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
cmdkit.StringArg("key", true, false, "The base58 multihash of an existing block to get.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -148,7 +148,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
cmdkit.IntOption(mhlenOptionName, "multihash hash length").WithDefault(-1),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -218,7 +218,7 @@ It takes a list of base58 encoded multihashes to remove.
cmdkit.BoolOption(blockQuietOptionName, "q", "Write minimal output."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -6,6 +6,7 @@ import (
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
config "gx/ipfs/QmYyzmMnhNTtoXx5ttgUaRdHHckYnQWjPL98hgLAR2QLDD/go-ipfs-config"
cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
@ -22,13 +23,22 @@ func GetNode(env interface{}) (*core.IpfsNode, error) {
}
// GetApi extracts CoreAPI instance from the environment.
func GetApi(env cmds.Environment) (coreiface.CoreAPI, error) {
func GetApi(env cmds.Environment, req *cmds.Request) (coreiface.CoreAPI, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}
return ctx.GetAPI()
local, _ := req.Options["local"].(bool)
api, err := ctx.GetAPI()
if err != nil {
return nil, err
}
if local {
return api.WithOptions(options.Api.Offline(local))
}
return api, nil
}
// GetConfig extracts the config from the environment.

View File

@ -124,7 +124,7 @@ var filesStatCmd = &cmds.Command{
return err
}
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -308,7 +308,7 @@ var filesCpCmd = &cmds.Command{
return err
}
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -71,7 +71,7 @@ var keyGenCmd = &cmds.Command{
cmdkit.StringArg("name", true, false, "name of key to create"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -121,7 +121,7 @@ var keyListCmd = &cmds.Command{
cmdkit.BoolOption("l", "Show extra information about keys."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -161,7 +161,7 @@ var keyRenameCmd = &cmds.Command{
cmdkit.BoolOption(keyStoreForceOptionName, "f", "Allow to overwrite an existing key."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -206,7 +206,7 @@ var keyRmCmd = &cmds.Command{
cmdkit.BoolOption("l", "Show extra information about keys."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -74,7 +74,7 @@ The JSON output contains type information.
return err
}
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -80,13 +80,12 @@ Resolve the value of a dnslink:
cmdkit.BoolOption(streamOptionName, "s", "Stream entries as they are found."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
nocache, _ := req.Options["nocache"].(bool)
local, _ := req.Options["local"].(bool)
var name string
if len(req.Arguments) == 0 {
@ -105,7 +104,6 @@ Resolve the value of a dnslink:
stream, _ := req.Options[streamOptionName].(bool)
opts := []options.NameResolveOption{
options.Name.Local(local),
options.Name.Cache(!nocache),
}

View File

@ -83,7 +83,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
cmdkit.BoolOption(quieterOptionName, "Q", "Write only final hash."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -55,7 +55,7 @@ Example:
cmdkit.BoolOption(verboseOptionName, "v", "Print extra information."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -77,7 +77,7 @@ is the raw data of the object.
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -114,7 +114,7 @@ multihash.
cmdkit.BoolOption("headers", "v", "Print table headers (Hash, Size, Name)."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -204,7 +204,7 @@ Supported values are:
cmdkit.StringOption("data-encoding", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -294,7 +294,7 @@ var ObjectStatCmd = &cmds.Command{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -386,7 +386,7 @@ And then run:
cmdkit.BoolOption("quiet", "q", "Write minimal output."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -459,7 +459,7 @@ Available templates:
cmdkit.StringArg("template", false, false, "Template to use. Optional."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -50,7 +50,7 @@ the limit will not be respected by the network.
cmdkit.FileArg("data", true, false, "Data to append.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -97,7 +97,7 @@ Example:
cmdkit.FileArg("data", true, false, "The data to set the object to.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -140,7 +140,7 @@ Remove a Merkle-link from the given object and return the hash of the result.
cmdkit.StringArg("name", true, false, "Name of the link to remove."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -192,7 +192,7 @@ to a file containing 'bar', and returns the hash of the new object.
cmdkit.BoolOption("create", "p", "Create intermediary nodes."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -72,7 +72,7 @@ var addPinCmd = &cmds.Command{
return err
}
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -203,7 +203,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return err
}
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -294,7 +294,7 @@ Example:
return err
}
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -366,7 +366,7 @@ new pin and removing the old one.
},
Type: PinOutput{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -78,7 +78,7 @@ This command outputs data in the following encodings:
cmdkit.BoolOption(pubsubDiscoverOptionName, "try to discover other peers subscribed to the same topic"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -153,7 +153,7 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
cmdkit.StringArg("data", true, true, "Payload of message to publish.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -188,7 +188,7 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -234,7 +234,7 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
cmdkit.StringArg("topic", false, false, "topic to list connected peers of"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -71,7 +71,7 @@ var swarmPeersCmd = &cmds.Command{
cmdkit.BoolOption(swarmDirectionOptionName, "Also list information about the direction of connection"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -221,7 +221,7 @@ var swarmAddrsCmd = &cmds.Command{
"listen": swarmAddrsListenCmd,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -275,7 +275,7 @@ var swarmAddrsLocalCmd = &cmds.Command{
cmdkit.BoolOption("id", "Show peer ID in addresses."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -316,7 +316,7 @@ var swarmAddrsListenCmd = &cmds.Command{
`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -355,7 +355,7 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3
cmdkit.StringArg("address", true, true, "Address of peer to connect to.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
@ -403,7 +403,7 @@ it will reconnect.
cmdkit.StringArg("address", true, true, "Address of peer to disconnect from.").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -77,7 +77,7 @@ possible, please use 'ipfs ls' instead.
return err
}
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

View File

@ -31,6 +31,7 @@ import (
routing "gx/ipfs/QmRASJXJUFygM5qU4YrH7k7jD6S4Hg8nJmgqJ4bYJvLatd/go-libp2p-routing"
blockstore "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
offlinexch "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
pubsub "gx/ipfs/QmaqGyUhWLsJbVo1QAujSu13mxNjFJ98Kt2VWGSnShGE1Q/go-libp2p-pubsub"
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
@ -218,7 +219,10 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
subApi.peerHost = nil
subApi.namesys = nil
subApi.recordValidator = nil
subApi.exchange = nil
subApi.exchange = offlinexch.Exchange(subApi.blockstore)
subApi.blocks = bserv.New(api.blockstore, subApi.exchange)
subApi.dag = dag.NewDAGService(subApi.blocks)
}
return subApi, nil

View File

@ -30,7 +30,6 @@ type UnixfsAddSettings struct {
Pin bool
OnlyHash bool
Local bool
FsCache bool
NoCopy bool
@ -60,7 +59,6 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
Pin: false,
OnlyHash: false,
Local: false,
FsCache: false,
NoCopy: false,
@ -220,16 +218,6 @@ func (unixfsOpts) HashOnly(hashOnly bool) UnixfsAddOption {
}
}
// Local will add the data to blockstore without announcing it to the network
//
// Note that this doesn't prevent other nodes from getting this data
func (unixfsOpts) Local(local bool) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.Local = local
return nil
}
}
// Wrap tells the adder to wrap the added file structure with an additional
// directory.
func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption {

View File

@ -15,7 +15,6 @@ import (
bstore "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
mfs "gx/ipfs/QmU3iDRUrxyTYdV2j5MuWLFvP1k7w98vD66PLnNChgvUmZ/go-mfs"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
cidutil "gx/ipfs/QmbfKu17LbMWyGUxHEUns9Wf5Dkm8PT6be4uPhTkk4YvaV/go-cidutil"
ft "gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs"
uio "gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs/io"
@ -72,10 +71,6 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
pinning = nilnode.Pinning
}
if settings.Local {
exch = offline.Exchange(addblockstore)
}
bserv := blockservice.New(addblockstore, exch) // hash security 001
dserv := dag.NewDAGService(bserv)

View File

@ -183,6 +183,8 @@ func TestAdd(t *testing.T) {
data func() files.Node
expect func(files.Node) files.Node
apiOpts []options.ApiOption
path string
err string
@ -270,10 +272,10 @@ func TestAdd(t *testing.T) {
},
// Local
{
name: "addLocal", // better cases in sharness
data: strFile(helloStr),
path: hello,
opts: []options.UnixfsAddOption{options.Unixfs.Local(true)},
name: "addLocal", // better cases in sharness
data: strFile(helloStr),
path: hello,
apiOpts: []options.ApiOption{options.Api.Offline(true)},
},
{
name: "hashOnly", // test (non)fetchability
@ -511,9 +513,14 @@ func TestAdd(t *testing.T) {
}()
}
tapi, err := api.WithOptions(testCase.apiOpts...)
if err != nil {
t.Fatal(err)
}
// Add!
p, err := api.Unixfs().Add(ctx, data, opts...)
p, err := tapi.Unixfs().Add(ctx, data, opts...)
close(eventOut)
evtWg.Wait()
if testCase.err != "" {
@ -594,7 +601,7 @@ func TestAdd(t *testing.T) {
}
}
f, err := api.Unixfs().Get(ctx, p)
f, err := tapi.Unixfs().Get(ctx, p)
if err != nil {
t.Fatal(err)
}