Skip to content

Commit ead12ce

Browse files
committed
Add documenation.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent f25f4eb commit ead12ce

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

core/commands/cmdenv/cidbase.go

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
var OptionCidBase = cmdkit.StringOption("cid-base", "mbase", "Multi-base to use to encode version 1 CIDs in output.")
1313
var OptionOutputCidV1 = cmdkit.BoolOption("output-cidv1", "Upgrade CID version 0 to version 1 in output.")
1414

15+
// CidBaseHandler is a helper class to process the `--cid-base` and
16+
// `--output-cidv1` options. In the future it may also be used to
17+
// process relevant config settings.
18+
//
19+
// Several of its methods return the class itself in order to allow
20+
// easy chaining, a typical usage would be
21+
// `cmdenv.NewCidBaseHandler(req).UseGlobal().Proc()` or
22+
// `cmdenv.NewCidBaseHandlerLegacy(req).Proc()`.
1523
type CidBaseHandler struct {
1624
base string
1725
upgrade bool
@@ -20,6 +28,7 @@ type CidBaseHandler struct {
2028
enc *cidenc.Encoder
2129
}
2230

31+
// NewCidBaseHandler created a a CidBaseHandler from a request
2332
func NewCidBaseHandler(req *cmds.Request) *CidBaseHandler {
2433
h := &CidBaseHandler{}
2534
h.base, _ = req.Options["cid-base"].(string)
@@ -28,6 +37,8 @@ func NewCidBaseHandler(req *cmds.Request) *CidBaseHandler {
2837
return h
2938
}
3039

40+
// NewCidBaseHandlerLegacy created a a CidBaseHandler from a request
41+
// using the old commands library
3142
func NewCidBaseHandlerLegacy(req oldcmds.Request) *CidBaseHandler {
3243
h := &CidBaseHandler{}
3344
h.base, _, _ = req.Option("cid-base").String()
@@ -36,11 +47,17 @@ func NewCidBaseHandlerLegacy(req oldcmds.Request) *CidBaseHandler {
3647
return h
3748
}
3849

50+
// UseGlobal enables the use of the global default. This is somewhat
51+
// of a hack and should be used with care. In particular it should
52+
// only be used on the client side and not the server side.
3953
func (h *CidBaseHandler) UseGlobal() *CidBaseHandler {
4054
h.enc = &cidenc.Default
4155
return h
4256
}
4357

58+
// Proc processes the `--cid-base` and `--output-cidv1` options. If
59+
// UseGlobal was enabled it will change the value of the global
60+
// default.
4461
func (h *CidBaseHandler) Proc() (*CidBaseHandler, error) {
4562
var e cidenc.Encoder = cidenc.Default
4663
if h.base != "" {
@@ -63,10 +80,19 @@ func (h *CidBaseHandler) Proc() (*CidBaseHandler, error) {
6380
return h, nil
6481
}
6582

83+
// Encoder returns a copy of the underlying Encoder
6684
func (h *CidBaseHandler) Encoder() cidenc.Encoder {
6785
return *h.enc
6886
}
6987

88+
// EncoderFromPath returns a a new Encoder that will format Cid like
89+
// the one in the path if the `--cid-base` option is not used. (If
90+
// the `--cid-base` is used then a copy of the base encoder will be
91+
// returned.) In particular: if the path contains a version 1 CID
92+
// then all CIDs will be outputting using the same multibase. if the
93+
// path contains a version 0 CID then version 0 CIDs will be outputted
94+
// as is and version1 cids will use the multibase from the base
95+
// encoder
7096
func (h *CidBaseHandler) EncoderFromPath(p string) cidenc.Encoder {
7197
if h.base == "" {
7298
enc, _ := cidenc.FromPath(*h.enc, p)
@@ -76,6 +102,12 @@ func (h *CidBaseHandler) EncoderFromPath(p string) cidenc.Encoder {
76102
}
77103
}
78104

105+
// EncoderWithOverride returns a new encoder that will use the setting
106+
// from the base encoder unless it is a CID that was specified on the
107+
// command line and the `--cid-base` option was not used. (If the
108+
// `--cid-base` is used then a copy of the base encoder will be
109+
// returned.) In that case the same CID string as specified on the
110+
// command line will be used.
79111
func (h *CidBaseHandler) EncoderWithOverride() cidenc.Interface {
80112
if h.base == "" {
81113
enc := cidenc.NewOverride(*h.enc)

0 commit comments

Comments
 (0)