@@ -12,6 +12,14 @@ import (
12
12
var OptionCidBase = cmdkit .StringOption ("cid-base" , "mbase" , "Multi-base to use to encode version 1 CIDs in output." )
13
13
var OptionOutputCidV1 = cmdkit .BoolOption ("output-cidv1" , "Upgrade CID version 0 to version 1 in output." )
14
14
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()`.
15
23
type CidBaseHandler struct {
16
24
base string
17
25
upgrade bool
@@ -20,6 +28,7 @@ type CidBaseHandler struct {
20
28
enc * cidenc.Encoder
21
29
}
22
30
31
+ // NewCidBaseHandler created a a CidBaseHandler from a request
23
32
func NewCidBaseHandler (req * cmds.Request ) * CidBaseHandler {
24
33
h := & CidBaseHandler {}
25
34
h .base , _ = req .Options ["cid-base" ].(string )
@@ -28,6 +37,8 @@ func NewCidBaseHandler(req *cmds.Request) *CidBaseHandler {
28
37
return h
29
38
}
30
39
40
+ // NewCidBaseHandlerLegacy created a a CidBaseHandler from a request
41
+ // using the old commands library
31
42
func NewCidBaseHandlerLegacy (req oldcmds.Request ) * CidBaseHandler {
32
43
h := & CidBaseHandler {}
33
44
h .base , _ , _ = req .Option ("cid-base" ).String ()
@@ -36,11 +47,17 @@ func NewCidBaseHandlerLegacy(req oldcmds.Request) *CidBaseHandler {
36
47
return h
37
48
}
38
49
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.
39
53
func (h * CidBaseHandler ) UseGlobal () * CidBaseHandler {
40
54
h .enc = & cidenc .Default
41
55
return h
42
56
}
43
57
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.
44
61
func (h * CidBaseHandler ) Proc () (* CidBaseHandler , error ) {
45
62
var e cidenc.Encoder = cidenc .Default
46
63
if h .base != "" {
@@ -63,10 +80,19 @@ func (h *CidBaseHandler) Proc() (*CidBaseHandler, error) {
63
80
return h , nil
64
81
}
65
82
83
+ // Encoder returns a copy of the underlying Encoder
66
84
func (h * CidBaseHandler ) Encoder () cidenc.Encoder {
67
85
return * h .enc
68
86
}
69
87
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
70
96
func (h * CidBaseHandler ) EncoderFromPath (p string ) cidenc.Encoder {
71
97
if h .base == "" {
72
98
enc , _ := cidenc .FromPath (* h .enc , p )
@@ -76,6 +102,12 @@ func (h *CidBaseHandler) EncoderFromPath(p string) cidenc.Encoder {
76
102
}
77
103
}
78
104
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.
79
111
func (h * CidBaseHandler ) EncoderWithOverride () cidenc.Interface {
80
112
if h .base == "" {
81
113
enc := cidenc .NewOverride (* h .enc )
0 commit comments