-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global --cid-base option and enable it for ipfs add and tar comma…
…nds. This also adds a global --output-cidv1 option. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
- Loading branch information
Showing
10 changed files
with
197 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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,82 @@ | ||
package cmdenv | ||
|
||
import ( | ||
cidenc "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/cidenc" | ||
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds" | ||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" | ||
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.") | ||
|
||
// ProcCidBase processes the `cid-base` and `output-cidv1` options. | ||
// In the future it may also be used to process relevant config | ||
// settings. | ||
func ProcCidBase(req *cmds.Request, opts ...ProcCidBaseOption) (*CidBaseInfo, error) { | ||
h := &CidBaseInfo{} | ||
h.base, _ = req.Options["cid-base"].(string) | ||
h.upgrade, h.upgradeDefined = req.Options["output-cidv1"].(bool) | ||
|
||
for _, opt := range opts { | ||
if err := opt(h); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
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 | ||
} | ||
|
||
func ProcCidBaseClientSide(req *cmds.Request, opts ...ProcCidBaseOption) (*CidBaseInfo, error) { | ||
return ProcCidBase(req, append([]ProcCidBaseOption{UseGlobal(true)}, opts...)...) | ||
} | ||
|
||
type ProcCidBaseOption func(*CidBaseInfo) error | ||
|
||
// 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 UseGlobal(yes bool) ProcCidBaseOption { | ||
return func(h *CidBaseInfo) error { | ||
if yes { | ||
h.enc = &cidenc.Default | ||
} else { | ||
h.enc = nil | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// CidBaseInfo is a returned by ProcCidBase | ||
type CidBaseInfo struct { | ||
base string | ||
upgrade bool | ||
upgradeDefined bool | ||
enc *cidenc.Encoder | ||
} | ||
|
||
// Encoder returns a copy of the underlying Encoder | ||
func (h *CidBaseInfo) Encoder() cidenc.Encoder { | ||
return *h.enc | ||
} |
This file contains 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 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 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 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 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 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 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.