@cryptotaxi247 / kubo / commits / f3ea21c82

Reenable Stdin argument reading.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Aug 27, 2018 at 18:52 UTC f3ea21c821403c6b07bf594dc316163341adb630
1 file changed +35 -2
core/commands/cid.go
+35 -2
@@ -39,7 +39,7 @@ The optional format string is a printf style format string:
39 ` + cidutil.FormatRef,
40 },
41 Arguments: []cmdkit.Argument{
42 - cmdkit.StringArg("cid", true, true, "Cids to format."),
42 + cmdkit.StringArg("cid", true, true, "Cids to format.").EnableStdin(),
43 },
44 Options: []cmdkit.Option{
45 cmdkit.StringOption("f", "Printf style format string.").WithDefault("%s"),
@@ -125,8 +125,37 @@ type cidFormatOpts struct {
125 verConv func(cid cid.Cid) (cid.Cid, error)
126 }
127
128 +type argumentIterator struct {
129 + args []string
130 + body cmds.StdinArguments
131 +}
132 +
133 +func (i *argumentIterator) next() (string, bool) {
134 + if len(i.args) > 0 {
135 + arg := i.args[0]
136 + i.args = i.args[1:]
137 + return arg, true
138 + }
139 + if i.body == nil || !i.body.Scan() {
140 + return "", false
141 + }
142 + return strings.TrimSpace(i.body.Argument()), true
143 +}
144 +
145 +func (i *argumentIterator) err() error {
146 + if i.body == nil {
147 + return nil
148 + }
149 + return i.body.Err()
150 +}
151 +
152 func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts) error {
129 - for _, cidStr := range req.Arguments {
153 + itr := argumentIterator{req.Arguments, req.BodyArgs()}
154 + for {
155 + cidStr, ok := itr.next()
156 + if !ok {
157 + break
158 + }
159 emit := func(fmtd string, err error) {
160 res := &CidFormatRes{CidStr: cidStr, Formatted: fmtd}
161 if err != nil {
@@ -157,6 +186,10 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
186 }
187 emit(str, err)
188 }
189 + err := itr.err()
190 + if err != nil {
191 + return err
192 + }
193 return nil
194 }
195