Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add offline id test #4978 and refactor command code #5562

Merged
merged 3 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/commands/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
)

const (
verboseOptionName = "v"
)

var ActiveReqsCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List commands run on this IPFS node.",
Expand All @@ -25,7 +29,7 @@ Lists running and recently run commands.
res.SetOutput(req.InvocContext().ReqLog.Report())
},
Options: []cmdkit.Option{
cmdkit.BoolOption("verbose", "v", "Print extra information."),
cmdkit.BoolOption("verbose", verboseOptionName, "Print extra information."),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to switch this to constantize (and later use in req.Option()) the long name for this command so that it is consistent with all the others.

},
Subcommands: map[string]*cmds.Command{
"clear": clearInactiveCmd,
Expand All @@ -44,7 +48,7 @@ Lists running and recently run commands.
}
buf := new(bytes.Buffer)

verbose, _, _ := res.Request().Option("v").Bool()
verbose, _, _ := res.Request().Option(verboseOptionName).Bool()

w := tabwriter.NewWriter(buf, 4, 4, 2, ' ', 0)
if verbose {
Expand Down
8 changes: 6 additions & 2 deletions core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ var BitswapCmd = &cmds.Command{
},
}

const (
peerOptionName = "peer"
)

var showWantlistCmd = &oldcmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Show blocks currently on the wantlist.",
ShortDescription: `
Print out all blocks currently on the bitswap wantlist for the local peer.`,
},
Options: []cmdkit.Option{
cmdkit.StringOption("peer", "p", "Specify which peer to show wantlist for. Default: self."),
cmdkit.StringOption(peerOptionName, "p", "Specify which peer to show wantlist for. Default: self."),
},
Type: KeyList{},
Run: func(req oldcmds.Request, res oldcmds.Response) {
Expand All @@ -60,7 +64,7 @@ Print out all blocks currently on the bitswap wantlist for the local peer.`,
return
}

pstr, found, err := req.Option("peer").String()
pstr, found, err := req.Option(peerOptionName).String()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down
31 changes: 21 additions & 10 deletions core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
},
}

const (
blockFormatOptionName = "format"
mhtypeOptionName = "mhtype"
mhlenOptionName = "mhlen"
)

var blockPutCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Store input as an IPFS block.",
Expand All @@ -142,9 +148,9 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.StringOption("format", "f", "cid format for blocks to be created with."),
cmdkit.StringOption("mhtype", "multihash hash function").WithDefault("sha2-256"),
cmdkit.IntOption("mhlen", "multihash hash length").WithDefault(-1),
cmdkit.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."),
cmdkit.StringOption(mhtypeOptionName, "multihash hash function").WithDefault("sha2-256"),
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)
Expand All @@ -157,18 +163,18 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
return err
}

mhtype, _ := req.Options["mhtype"].(string)
mhtype, _ := req.Options[mhtypeOptionName].(string)
mhtval, ok := mh.Names[mhtype]
if !ok {
return fmt.Errorf("unrecognized multihash function: %s", mhtype)
}

mhlen, ok := req.Options["mhlen"].(int)
mhlen, ok := req.Options[mhlenOptionName].(int)
if !ok {
return errors.New("missing option \"mhlen\"")
}

format, formatSet := req.Options["format"].(string)
format, formatSet := req.Options[blockFormatOptionName].(string)
if !formatSet {
if mhtval != mh.SHA2_256 || (mhlen != -1 && mhlen != 32) {
format = "protobuf"
Expand Down Expand Up @@ -200,6 +206,11 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
Type: BlockStat{},
}

const (
forceOptionName = "force"
blockQuietOptionName = "quiet"
)

var blockRmCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Remove IPFS block(s).",
Expand All @@ -212,17 +223,17 @@ It takes a list of base58 encoded multihashes to remove.
cmdkit.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("force", "f", "Ignore nonexistent blocks."),
cmdkit.BoolOption("quiet", "q", "Write minimal output."),
cmdkit.BoolOption(forceOptionName, "f", "Ignore nonexistent blocks."),
cmdkit.BoolOption(blockQuietOptionName, "q", "Write minimal output."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
if err != nil {
return err
}

force, _ := req.Options["force"].(bool)
quiet, _ := req.Options["quiet"].(bool)
force, _ := req.Options[forceOptionName].(bool)
quiet, _ := req.Options[blockQuietOptionName].(bool)

// TODO: use batching coreapi when done
for _, b := range req.Arguments {
Expand Down
16 changes: 12 additions & 4 deletions core/commands/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
},
}

const (
defaultOptionName = "default"
)

var bootstrapAddCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Add peers to the bootstrap list.",
Expand All @@ -53,14 +57,14 @@ in the bootstrap list).
},

Options: []cmdkit.Option{
cmdkit.BoolOption("default", "Add default bootstrap nodes. (Deprecated, use 'default' subcommand instead)"),
cmdkit.BoolOption(defaultOptionName, "Add default bootstrap nodes. (Deprecated, use 'default' subcommand instead)"),
},
Subcommands: map[string]*cmds.Command{
"default": bootstrapAddDefaultCmd,
},

Run: func(req cmds.Request, res cmds.Response) {
deflt, _, err := req.Option("default").Bool()
deflt, _, err := req.Option(defaultOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -191,6 +195,10 @@ in the bootstrap list).`,
},
}

const (
bootstrapAllOptionName = "all"
)

var bootstrapRemoveCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Remove peers from the bootstrap list.",
Expand All @@ -202,13 +210,13 @@ var bootstrapRemoveCmd = &cmds.Command{
cmdkit.StringArg("peer", false, true, peerOptionDesc).EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("all", "Remove all bootstrap peers. (Deprecated, use 'all' subcommand)"),
cmdkit.BoolOption(bootstrapAllOptionName, "Remove all bootstrap peers. (Deprecated, use 'all' subcommand)"),
},
Subcommands: map[string]*cmds.Command{
"all": bootstrapRemoveAllCmd,
},
Run: func(req cmds.Request, res cmds.Response) {
all, _, err := req.Option("all").Bool()
all, _, err := req.Option(bootstrapAllOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down
14 changes: 9 additions & 5 deletions core/commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
)

const progressBarMinSize = 1024 * 1024 * 8 // show progress bar for outputs > 8MiB
const (
progressBarMinSize = 1024 * 1024 * 8 // show progress bar for outputs > 8MiB
offsetOptionName = "offset"
lengthOptionName = "length"
)

var CatCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Expand All @@ -25,8 +29,8 @@ var CatCmd = &cmds.Command{
cmdkit.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to be outputted.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.IntOption("offset", "o", "Byte offset to begin reading from."),
cmdkit.IntOption("length", "l", "Maximum number of bytes to read."),
cmdkit.IntOption(offsetOptionName, "o", "Byte offset to begin reading from."),
cmdkit.IntOption(lengthOptionName, "l", "Maximum number of bytes to read."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
Expand All @@ -45,12 +49,12 @@ var CatCmd = &cmds.Command{
}
}

offset, _ := req.Options["offset"].(int)
offset, _ := req.Options[offsetOptionName].(int)
if offset < 0 {
return fmt.Errorf("cannot specify negative offset")
}

max, found := req.Options["length"].(int)
max, found := req.Options[lengthOptionName].(int)
if err != nil {
return err
}
Expand Down
39 changes: 27 additions & 12 deletions core/commands/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ var CidCmd = &cmds.Command{
},
}

const (
cidFormatOptionName = "f"
cidVerisonOptionName = "v"
cidMultibaseOptionName = "b"
)

var cidFmtCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Format and convert a CID in various useful ways.",
Expand All @@ -44,14 +50,14 @@ The optional format string is a printf style format string:
cmdkit.StringArg("cid", true, true, "Cids to format.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.StringOption("f", "Printf style format string.").WithDefault("%s"),
cmdkit.StringOption("v", "CID version to convert to."),
cmdkit.StringOption("b", "Multibase to display CID in."),
cmdkit.StringOption(cidFormatOptionName, "Printf style format string.").WithDefault("%s"),
cmdkit.StringOption(cidVerisonOptionName, "CID version to convert to."),
cmdkit.StringOption(cidMultibaseOptionName, "Multibase to display CID in."),
},
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
fmtStr, _ := req.Options["f"].(string)
verStr, _ := req.Options["v"].(string)
baseStr, _ := req.Options["b"].(string)
fmtStr, _ := req.Options[cidFormatOptionName].(string)
verStr, _ := req.Options[cidVerisonOptionName].(string)
baseStr, _ := req.Options[cidMultibaseOptionName].(string)

opts := cidFormatOpts{}

Expand Down Expand Up @@ -216,13 +222,18 @@ type CodeAndName struct {
Name string
}

const (
prefixOptionName = "prefix"
numericOptionName = "numeric"
)

var basesCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List available multibase encodings.",
},
Options: []cmdkit.Option{
cmdkit.BoolOption("prefix", "also include the single leter prefixes in addition to the code"),
cmdkit.BoolOption("numeric", "also include numeric codes"),
cmdkit.BoolOption(prefixOptionName, "also include the single leter prefixes in addition to the code"),
cmdkit.BoolOption(numericOptionName, "also include numeric codes"),
},
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
var res []CodeAndName
Expand All @@ -235,8 +246,8 @@ var basesCmd = &cmds.Command{
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
prefixes, _ := req.Options["prefix"].(bool)
numeric, _ := req.Options["numeric"].(bool)
prefixes, _ := req.Options[prefixOptionName].(bool)
numeric, _ := req.Options[numericOptionName].(bool)
val, ok := val0.([]CodeAndName)
if !ok {
return e.TypeErr(val, val0)
Expand Down Expand Up @@ -265,12 +276,16 @@ var basesCmd = &cmds.Command{
Type: []CodeAndName{},
}

const (
codecsNumericOptionName = "numeric"
)

var codecsCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List available CID codecs.",
},
Options: []cmdkit.Option{
cmdkit.BoolOption("numeric", "also include numeric codes"),
cmdkit.BoolOption(codecsNumericOptionName, "also include numeric codes"),
},
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
var res []CodeAndName
Expand All @@ -283,7 +298,7 @@ var codecsCmd = &cmds.Command{
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
numeric, _ := req.Options["numeric"].(bool)
numeric, _ := req.Options[codecsNumericOptionName].(bool)
val, ok := val0.([]CodeAndName)
if !ok {
return e.TypeErr(val, val0)
Expand Down
13 changes: 9 additions & 4 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type ConfigField struct {
Value interface{}
}

const (
configBoolOptionName = "bool"
configJSONOptionName = "json"
)

var ConfigCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Get and set ipfs config values.",
Expand Down Expand Up @@ -54,8 +59,8 @@ Set the value of the 'Datastore.Path' key:
cmdkit.StringArg("value", false, false, "The value to set the config entry to."),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("bool", "Set a boolean value."),
cmdkit.BoolOption("json", "Parse stringified JSON."),
cmdkit.BoolOption(configBoolOptionName, "Set a boolean value."),
cmdkit.BoolOption(configJSONOptionName, "Parse stringified JSON."),
},
Run: func(req cmds.Request, res cmds.Response) {
args := req.Arguments()
Expand Down Expand Up @@ -87,7 +92,7 @@ Set the value of the 'Datastore.Path' key:
if len(args) == 2 {
value := args[1]

if parseJson, _, _ := req.Option("json").Bool(); parseJson {
if parseJSON, _, _ := req.Option(configJSONOptionName).Bool(); parseJSON {
var jsonVal interface{}
if err := json.Unmarshal([]byte(value), &jsonVal); err != nil {
err = fmt.Errorf("failed to unmarshal json. %s", err)
Expand All @@ -96,7 +101,7 @@ Set the value of the 'Datastore.Path' key:
}

output, err = setConfig(r, key, jsonVal)
} else if isbool, _, _ := req.Option("bool").Bool(); isbool {
} else if isbool, _, _ := req.Option(configBoolOptionName).Bool(); isbool {
output, err = setConfig(r, key, value == "true")
} else {
output, err = setConfig(r, key, value)
Expand Down
Loading