@cryptotaxi247 / kubo / commits / e5622f628

Stream results for "ipfs cid format".

Note reading input from Stdin is broken. Only the first result is accepted. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Aug 26, 2018 at 20:30 UTC e5622f628a8d5e92f66f81758e3800a766e9d297
1 file changed +36 -32
core/commands/cid.go
+36 -32
@@ -80,22 +80,25 @@ The optional format string is a printf style format string:
80 opts.newBase = mbase.Encoding(-1)
81 }
82
83 - res, err := formatCids(req.Arguments, opts)
84 - if err != nil {
85 - return err
86 - }
87 - cmds.EmitOnce(resp, res)
88 - return nil
83 + return emitCids(req, resp, opts)
84 },
90 - Encoders: cmds.EncoderMap{
91 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val interface{}) error {
92 - for _, v := range val.([]string) {
93 - fmt.Fprintf(w, "%s\n", v)
85 + PostRun: cmds.PostRunMap{
86 + cmds.CLI: streamRes(func(v interface{}, out io.Writer) nonFatalError {
87 + r := v.(*CidFormatRes)
88 + if r.ErrorMsg != "" {
89 + return nonFatalError(fmt.Sprintf("%s: %s", r.CidStr, r.ErrorMsg))
90 }
95 - return nil
91 + fmt.Fprintf(out, "%s\n", r.Formatted)
92 + return ""
93 }),
94 },
98 - Type: []string{},
95 + Type: CidFormatRes{},
96 +}
97 +
98 +type CidFormatRes struct {
99 + CidStr string // Original Cid String passed in
100 + Formatted string // Formated Result
101 + ErrorMsg string // Error
102 }
103
104 var base32Cmd = &cmds.Command{
@@ -103,7 +106,7 @@ var base32Cmd = &cmds.Command{
106 Tagline: "Convert CIDs to Base32 CID version 1.",
107 },
108 Arguments: []cmdkit.Argument{
106 - cmdkit.StringArg("cid", true, true, "Cids to convert."),
109 + cmdkit.StringArg("cid", true, true, "Cids to convert.").EnableStdin(),
110 },
111 Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
112 opts := cidFormatOpts{
@@ -111,16 +114,10 @@ var base32Cmd = &cmds.Command{
114 newBase: mbase.Encoding(mbase.Base32),
115 verConv: toCidV1,
116 }
114 - res, err := formatCids(req.Arguments, opts)
115 - if err != nil {
116 - return err
117 - }
118 -
119 - cmds.EmitOnce(resp, res)
120 - return nil
117 + return emitCids(req, resp, opts)
118 },
122 - Encoders: cidFmtCmd.Encoders,
123 - Type: cidFmtCmd.Type,
119 + PostRun: cidFmtCmd.PostRun,
120 + Type: cidFmtCmd.Type,
121 }
122
123 type cidFormatOpts struct {
@@ -129,12 +126,19 @@ type cidFormatOpts struct {
126 verConv func(cid cid.Cid) (cid.Cid, error)
127 }
128
132 -func formatCids(args []string, opts cidFormatOpts) ([]string, error) {
133 - var res []string
134 - for _, cidStr := range args {
129 +func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts) error {
130 + for _, cidStr := range req.Arguments {
131 + emit := func(fmtd string, err error) {
132 + res := &CidFormatRes{CidStr: cidStr, Formatted: fmtd}
133 + if err != nil {
134 + res.ErrorMsg = err.Error()
135 + }
136 + resp.Emit(res)
137 + }
138 c, err := cid.Decode(cidStr)
139 if err != nil {
137 - return nil, fmt.Errorf("%s: %v", cidStr, err)
140 + emit("", err)
141 + continue
142 }
143 base := opts.newBase
144 if base == -1 {
@@ -143,18 +147,18 @@ func formatCids(args []string, opts cidFormatOpts) ([]string, error) {
147 if opts.verConv != nil {
148 c, err = opts.verConv(c)
149 if err != nil {
146 - return nil, fmt.Errorf("%s: %v", cidStr, err)
150 + emit("", err)
151 + continue
152 }
153 }
154 str, err := cidutil.Format(opts.fmtStr, base, c)
155 if _, ok := err.(cidutil.FormatStringError); ok {
151 - return nil, err
152 - } else if err != nil {
153 - return nil, fmt.Errorf("%s: %v", cidStr, err)
156 + // no point in continuing if there is a problem with the format string
157 + return err
158 }
155 - res = append(res, str)
159 + emit(str, err)
160 }
157 - return res, nil
161 + return nil
162 }
163
164 func toCidV0(c cid.Cid) (cid.Cid, error) {