Skip to content

Commit

Permalink
Merge pull request #5825 from ipfs/feat/coreapi/opts
Browse files Browse the repository at this point in the history
CoreAPI: Global offline option
  • Loading branch information
Stebalien authored Dec 20, 2018
2 parents daa1202 + 3f0945a commit 28dabc7
Show file tree
Hide file tree
Showing 44 changed files with 453 additions and 242 deletions.
3 changes: 1 addition & 2 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
ipnsMountKwd = "mount-ipns"
migrateKwd = "migrate"
mountKwd = "mount"
offlineKwd = "offline"
offlineKwd = "offline" // global option
routingOptionKwd = "routing"
routingOptionSupernodeKwd = "supernode"
routingOptionDHTClientKwd = "dhtclient"
Expand Down Expand Up @@ -161,7 +161,6 @@ Headers.
cmdkit.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)"),
cmdkit.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection"),
cmdkit.BoolOption(adjustFDLimitKwd, "Check and raise file descriptor limits if needed").WithDefault(true),
cmdkit.BoolOption(offlineKwd, "Run offline. Do not connect to the rest of the network but provide local API."),
cmdkit.BoolOption(migrateKwd, "If true, assume yes at the migrate prompt. If false, assume no."),
cmdkit.BoolOption(enablePubSubKwd, "Instantiate the ipfs daemon with the experimental pubsub feature enabled."),
cmdkit.BoolOption(enableIPNSPubSubKwd, "Enable IPNS record distribution through pubsub; enables pubsub."),
Expand Down
5 changes: 4 additions & 1 deletion commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func (c *Context) GetAPI() (coreiface.CoreAPI, error) {
if err != nil {
return nil, err
}
c.api = coreapi.NewCoreAPI(n)
c.api, err = coreapi.NewCoreAPI(n)
if err != nil {
return nil, err
}
}
return c.api, nil
}
Expand Down
4 changes: 1 addition & 3 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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),

Expand Down
8 changes: 4 additions & 4 deletions core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var CatCmd = &cmds.Command{
cmdkit.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
},
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
}
Expand Down
24 changes: 22 additions & 2 deletions core/commands/cmdenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package cmdenv

import (
"fmt"
"strings"

"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"
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
)

var log = logging.Logger("core/commands/cmdenv")

// GetNode extracts the node from the environment.
func GetNode(env interface{}) (*core.IpfsNode, error) {
ctx, ok := env.(*commands.Context)
Expand All @@ -22,13 +27,28 @@ 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()
offline, _ := req.Options["offline"].(bool)
if !offline {
offline, _ = req.Options["local"].(bool)
if offline {
log.Errorf("Command '%s', --local is deprecated, use --offline instead", strings.Join(req.Path, " "))
}
}
api, err := ctx.GetAPI()
if err != nil {
return nil, err
}
if offline {
return api.WithOptions(options.Api.Offline(offline))
}

return api, nil
}

// GetConfig extracts the config from the environment.
Expand Down
4 changes: 2 additions & 2 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions core/commands/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions core/commands/name/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/name/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/object/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions core/commands/object/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 28dabc7

Please sign in to comment.