-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add global option to specify the multibase encoding. #5464
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1f3ba9f
Add --cid-base & --upgrade-cidv0 global opt. and enable them on selec…
kevina 2407670
Add support for preserving the CID string in `ipfs refs`.
kevina 7c974b2
Add --cid-base support for `files ls -l`.
kevina 0b1ea38
Change --upgrade-cidv0 to --output-cidv1.
kevina 9380c83
Move `--cid-base` helpers to `cmdenc` package.
kevina dd7c66f
Use apicid.Hash for `ipfs file ls`.
kevina 0a4fb1f
Add documenation.
kevina bf79948
Fix "ipfs pin verify --verbose" test.
kevina 6c4033c
Fix `ipfs pin ls` to respect --cid-base add test cases.
kevina b508f6d
Use CID's directly in map in `ipfs pin verify`.
kevina 94bb70f
Fix `ipfs pin verify` so it respects the `--cid-base` option.
kevina bcc58f0
Documentation fixes.
kevina 6a6af85
Clean up logic in CidBaseHandler.Proc() method.
kevina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package cmdenv | ||
|
|
||
| import ( | ||
| oldcmds "github.com/ipfs/go-ipfs/commands" | ||
|
|
||
| cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" | ||
| cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds" | ||
| cidenc "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/cidenc" | ||
| mbase "gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase" | ||
| ) | ||
|
|
||
| var OptionCidBase = cmdkit.StringOption("cid-base", "mbase", "Multi-base encoding used for version 1 CIDs in output.") | ||
| var OptionOutputCidV1 = cmdkit.BoolOption("output-cidv1", "Upgrade CID version 0 to version 1 in output.") | ||
|
|
||
| // CidBaseHandler is a helper class to process the `--cid-base` and | ||
| // `--output-cidv1` options. In the future it may also be used to | ||
| // process relevant config settings. | ||
| // | ||
| // Several of its methods return the class itself in order to allow | ||
| // easy chaining, a typical usage would be | ||
| // `cmdenv.NewCidBaseHandler(req).UseGlobal().Proc()` or | ||
| // `cmdenv.NewCidBaseHandlerLegacy(req).Proc()`. | ||
| type CidBaseHandler struct { | ||
| base string | ||
| upgrade bool | ||
| upgradeDefined bool | ||
| args []string | ||
| enc *cidenc.Encoder | ||
| } | ||
|
|
||
| // NewCidBaseHandler created a CidBaseHandler from a request | ||
| func NewCidBaseHandler(req *cmds.Request) *CidBaseHandler { | ||
| h := &CidBaseHandler{} | ||
| h.base, _ = req.Options["cid-base"].(string) | ||
| h.upgrade, h.upgradeDefined = req.Options["output-cidv1"].(bool) | ||
| h.args = req.Arguments | ||
| return h | ||
| } | ||
|
|
||
| // NewCidBaseHandlerLegacy created a CidBaseHandler from a request | ||
| // using the old commands library | ||
| func NewCidBaseHandlerLegacy(req oldcmds.Request) *CidBaseHandler { | ||
| h := &CidBaseHandler{} | ||
| h.base, _, _ = req.Option("cid-base").String() | ||
| h.upgrade, h.upgradeDefined, _ = req.Option("output-cidv1").Bool() | ||
| h.args = req.Arguments() | ||
| return h | ||
| } | ||
|
|
||
| // UseGlobal enables the use of the global default. This is somewhat | ||
| // of a hack and should be used with care. In particular it should | ||
| // only be used on the client side and not the server side. | ||
| func (h *CidBaseHandler) UseGlobal() *CidBaseHandler { | ||
| h.enc = &cidenc.Default | ||
| return h | ||
| } | ||
|
|
||
| // Proc processes the `--cid-base` and `--output-cidv1` options. If | ||
| // UseGlobal was enabled, it will change the value of the global | ||
| // default. | ||
| func (h *CidBaseHandler) Proc() (*CidBaseHandler, error) { | ||
| e := cidenc.Default | ||
|
|
||
| if h.base != "" { | ||
| var err error | ||
| e.Base, err = mbase.EncoderByName(h.base) | ||
| if err != nil { | ||
| return h, err | ||
| } | ||
| if !h.upgradeDefined { | ||
| e.Upgrade = true | ||
| } | ||
| } | ||
|
|
||
| if h.upgradeDefined { | ||
| e.Upgrade = h.upgrade | ||
| } | ||
|
|
||
| if h.enc == nil { | ||
| h.enc = &cidenc.Encoder{} | ||
| } | ||
| *h.enc = e | ||
| return h, nil | ||
| } | ||
|
|
||
| // Encoder returns a copy of the underlying Encoder | ||
| func (h *CidBaseHandler) Encoder() cidenc.Encoder { | ||
| return *h.enc | ||
| } | ||
|
|
||
| // EncoderFromPath returns a new Encoder that will format CIDs like | ||
| // the one in the path if the `--cid-base` option is not used. (If | ||
| // the `--cid-base` is used then a copy of the base encoder will be | ||
| // returned.) In particular: if the path contains a version 1 CID | ||
| // then all CIDs will be outputting using the same multibase. if the | ||
| // path contains a version 0 CID then version 0 CIDs will be outputted | ||
| // as is and version 1 cids will use the multibase from the base | ||
| // encoder | ||
| func (h *CidBaseHandler) EncoderFromPath(p string) cidenc.Encoder { | ||
| if h.base == "" { | ||
| enc, _ := cidenc.FromPath(*h.enc, p) | ||
| return enc | ||
| } else { | ||
| return *h.enc | ||
| } | ||
| } | ||
|
|
||
| // EncoderWithOverride returns a new encoder that will use the setting | ||
| // from the base encoder unless it is a CID that was specified on the | ||
| // command line and the `--cid-base` option was not used. (If the | ||
| // `--cid-base` is used then a copy of the base encoder will be | ||
| // returned.) In that case the same CID string as specified on the | ||
| // command line will be used. | ||
| func (h *CidBaseHandler) EncoderWithOverride() cidenc.Interface { | ||
| if h.base == "" { | ||
| enc := cidenc.NewOverride(*h.enc) | ||
| enc.Add(h.args...) | ||
| return enc | ||
| } else { | ||
| return *h.enc | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.