|
| 1 | +package cmdenv |
| 2 | + |
| 3 | +import ( |
| 4 | + cidenc "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/cidenc" |
| 5 | + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds" |
| 6 | + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" |
| 7 | + mbase "gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase" |
| 8 | +) |
| 9 | + |
| 10 | +var OptionCidBase = cmdkit.StringOption("cid-base", "mbase", "Multi-base encoding used for version 1 CIDs in output.") |
| 11 | +var OptionOutputCidV1 = cmdkit.BoolOption("output-cidv1", "Upgrade CID version 0 to version 1 in output.") |
| 12 | + |
| 13 | +// ProcCidBase processes the `cid-base` and `output-cidv1` options. |
| 14 | +// In the future it may also be used to process relevant config |
| 15 | +// settings. |
| 16 | +func ProcCidBase(req *cmds.Request, opts ...ProcCidBaseOption) (*CidBaseInfo, error) { |
| 17 | + h := &CidBaseInfo{} |
| 18 | + h.base, _ = req.Options["cid-base"].(string) |
| 19 | + h.upgrade, h.upgradeDefined = req.Options["output-cidv1"].(bool) |
| 20 | + |
| 21 | + for _, opt := range opts { |
| 22 | + if err := opt(h); err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + e := cidenc.Default |
| 28 | + |
| 29 | + if h.base != "" { |
| 30 | + var err error |
| 31 | + e.Base, err = mbase.EncoderByName(h.base) |
| 32 | + if err != nil { |
| 33 | + return h, err |
| 34 | + } |
| 35 | + if !h.upgradeDefined { |
| 36 | + e.Upgrade = true |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if h.upgradeDefined { |
| 41 | + e.Upgrade = h.upgrade |
| 42 | + } |
| 43 | + |
| 44 | + if h.enc == nil { |
| 45 | + h.enc = &cidenc.Encoder{} |
| 46 | + } |
| 47 | + *h.enc = e |
| 48 | + return h, nil |
| 49 | +} |
| 50 | + |
| 51 | +func ProcCidBaseClientSide(req *cmds.Request, opts ...ProcCidBaseOption) (*CidBaseInfo, error) { |
| 52 | + return ProcCidBase(req, append([]ProcCidBaseOption{UseGlobal(true)}, opts...)...) |
| 53 | +} |
| 54 | + |
| 55 | +type ProcCidBaseOption func(*CidBaseInfo) error |
| 56 | + |
| 57 | +// UseGlobal enables the use of the global default. This is somewhat |
| 58 | +// of a hack and should be used with care. In particular it should |
| 59 | +// only be used on the client side and not the server side. |
| 60 | +func UseGlobal(yes bool) ProcCidBaseOption { |
| 61 | + return func(h *CidBaseInfo) error { |
| 62 | + if yes { |
| 63 | + h.enc = &cidenc.Default |
| 64 | + } else { |
| 65 | + h.enc = nil |
| 66 | + } |
| 67 | + return nil |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// CidBaseInfo is a returned by ProcCidBase |
| 72 | +type CidBaseInfo struct { |
| 73 | + base string |
| 74 | + upgrade bool |
| 75 | + upgradeDefined bool |
| 76 | + enc *cidenc.Encoder |
| 77 | +} |
| 78 | + |
| 79 | +// Encoder returns a copy of the underlying Encoder |
| 80 | +func (h *CidBaseInfo) Encoder() cidenc.Encoder { |
| 81 | + return *h.enc |
| 82 | +} |
0 commit comments