-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Provide new "cid" sub-command. #5385
Conversation
1dc7a95
to
b46e3d2
Compare
core/commands/cid.go
Outdated
` + cidutil.FormatRef, | ||
}, | ||
Arguments: []cmdkit.Argument{ | ||
cmdkit.StringArg("cid", true, true, "Cids to format."), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use WithDefault("%s")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You meant for the -f
option? If it is omitted that is the default (see below), I just didn't use WithDefault. The idea was the -f
is optional and if its not specified it will just return the CIDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's nice from a documentation standpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Problem.
core/commands/cid.go
Outdated
case "": | ||
opts.fmtStr = "%s" | ||
case "prefix": | ||
opts.fmtStr = "%P" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit magical. What's the motivation? We could also just add a ipfs cid prefix
command (alias of ipfs cid format -f '%P' ...
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was there in the original cid-fmt
utility and copied over here. For the cid-fmt
utility is was the original use case (to get information about cids). I am not tied to keeping it in this utility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove it for now. We can consider either a prefix
or a show
command later but we probably don't need them now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
core/commands/cid.go
Outdated
opts.newBase = mbase.Encoding(-1) | ||
} | ||
|
||
res, err := formatCids(req.Arguments, opts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd stream these results back. Users may want to feed quite a few CIDs into this command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that but didn't see the benefit. This is a client side utility that will never block and is should be very fast, so the only benefit will be memory usage. In addition the number of cid's passed in is limited by the length of command line so memory usage will never get out of control.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. Well, actually, arguments can be passed in on stdin (newline separated)... That's why I brought this up. Basically, a user may run something like ipfs refs -u -r QmThing | ipfs cid format -f '%c' | sort -u
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you have to do something special to make that work. Because when I tried it I get Error: argument "cid" is required
. I actually think this it is a bit of a misfeature to take the arguments from stdin without having to pass in a special flag, but that something to debate later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fwiw, I agree it's a misfeature to take arguments from stdin without a flag -- I think it's pretty widespread convention now to use a single dash "-
" argument to mean "read stdin instead of file". (And that would make just as much sense for "read stdin instead of literal".)
@alanshaw this adds a new Basically, it makes it easy to inspect CIDs and convert between various formats (bases, versions, etc.). |
core/commands/cid.go
Outdated
emit(str, err) | ||
} | ||
}() | ||
resp.Emit(out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just call Emit on each CID individually (no need for the goroutine or the channel).
EnableStdin isn't broken, it's just that:
Basically, you'll have to:
You can also just call |
That will prevent streaming. |
It will. The important part here is allowing ourselves to stream at some point in the future without breaking the API. |
Please see the new commits. You can now stream. Sorry I didn't follow up in the comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any way to simplify this? We're introducing multiple new abstractions.
core/commands/cid.go
Outdated
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error { | ||
prefixes, _ := req.Options["prefix"].(bool) | ||
numeric, _ := req.Options["numeric"].(bool) | ||
val := val0.([]CodeAndName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, you'll need to check if the type is correct. See: ipfs/go-ipfs-cmds#65. Basically, we end up feeding errors to this function as well.
(search the code for examples on how to do this).
core/commands/commands.go
Outdated
|
||
// streamRes is a helper function to stream results, that possibly | ||
// contain with non-fatal, the helper function is allowed to panic on | ||
// internal errors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs some proof-reading :).
core/commands/commands.go
Outdated
// streamRes is a helper function to stream results, that possibly | ||
// contain with non-fatal, the helper function is allowed to panic on | ||
// internal errors | ||
func streamRes(procVal func(interface{}, io.Writer) nonFatalError) func(*cmds.Request, cmds.ResponseEmitter) cmds.ResponseEmitter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we call this streamResults
? We tend to a bit too curt.
core/commands/cid.go
Outdated
if !ok { | ||
break | ||
} | ||
emit := func(fmtd string, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's lift this out of the loop (I don't trust go).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's in the loop as it is a closure. I do it that way to avoid having to pass is the cidStr variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just eliminated the need for the emit function.
core/commands/cid.go
Outdated
if err != nil { | ||
res.ErrorMsg = err.Error() | ||
} | ||
resp.Emit(res) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should handle this error (the client may have gone away).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what I should do. Should I log the error and then just return?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made at attempt at handling this, please check that it is correct.
Most (all) of the abstractions are lifted from the |
On second thought. I am not 100% sure what you mean. I thought you where talking about the handling of the |
Hey everyone! I've tried to replicate the functionalty here as closely as possible for the thank you ❤️ |
This PR adds a `jsipfs cid` command with tools for formatting, converting and discovering properties of CIDs. All the docs are here: https://github.com/ipfs-shipyard/js-cid-tool Will resolve this for JS: ipfs/kubo#5229 go-ipfs counterpart: ipfs/kubo#5385 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
@alanshaw I looked over the README and I think the tool captures the intentions about the same. It was never really my intention for these commands to become standardized except for perhaps the Note that some minor details of the behavior of these commands may change over time. |
@Stebalien if this is too complicated, I can separate out the part that implements proper streaming into a separate p.r. as I think that is where most of the complexity your seeing is coming from... |
Thanks @kevina
Understood, I haven't added this to any |
2973fdb
to
e21f234
Compare
@Stebalien I rebased this to fix the merge conflict and update to the new command libraries. It still seams that GitHub is broken when displaying the changes. Let me know if you made any progress with GitHub support or if you want me to try creating a new p.r. |
LGTM but I'm pulling in @warpfork for a second signoff (especially on the API, we can always fix the code later). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGWM.
It would be really nice if we could differentiate between API and until tool but we can. So I guess this is API now.
@kevina I was just waiting on some second signoff. Now that we have one, this should be good to go. Unfortunately, it'll need a rebase. I'd squash first as doing this commit-by-commit is painful (I tried, but there were significant changes to the commands lib). |
@Stebalien I rebase later today. |
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Note reading input from Stdin is broken. Only the first result is accepted. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
This also avoid using TabWriter as it doesn't support right aligning a single column and also because its an overkill for this case. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
@Stebalien actually I already rebased and fixed my code to use the new commands library as indicated in an earlier comment. I just rebased again and fixed a dep/import problem. This should be good to go. There is one failed test in Jenkins, but I don't think that is related. |
And it looks like rebasing caused GitHub to dismiss the earlier approving reviews... |
@Stebalien Jenkins is now green. |
Yeah, I just turned that off. Sorry. |
This PR adds a `jsipfs cid` command with tools for formatting, converting and discovering properties of CIDs. All the docs are here: https://github.com/ipfs-shipyard/js-cid-tool Will resolve this for JS: ipfs/kubo#5229 go-ipfs counterpart: ipfs/kubo#5385 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
This PR adds a `jsipfs cid` command with tools for formatting, converting and discovering properties of CIDs. All the docs are here: https://github.com/ipfs-shipyard/js-cid-tool Will resolve this for JS: ipfs/kubo#5229 go-ipfs counterpart: ipfs/kubo#5385 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
Implements #5357.
Still needs tests.Closes #5229. Closes #5357.