mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 17:36:38 +08:00
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>
This commit is contained in:
@ -80,22 +80,25 @@ The optional format string is a printf style format string:
|
|||||||
opts.newBase = mbase.Encoding(-1)
|
opts.newBase = mbase.Encoding(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := formatCids(req.Arguments, opts)
|
return emitCids(req, resp, opts)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cmds.EmitOnce(resp, res)
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
Encoders: cmds.EncoderMap{
|
PostRun: cmds.PostRunMap{
|
||||||
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val interface{}) error {
|
cmds.CLI: streamRes(func(v interface{}, out io.Writer) nonFatalError {
|
||||||
for _, v := range val.([]string) {
|
r := v.(*CidFormatRes)
|
||||||
fmt.Fprintf(w, "%s\n", v)
|
if r.ErrorMsg != "" {
|
||||||
|
return nonFatalError(fmt.Sprintf("%s: %s", r.CidStr, r.ErrorMsg))
|
||||||
}
|
}
|
||||||
return nil
|
fmt.Fprintf(out, "%s\n", r.Formatted)
|
||||||
|
return ""
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
Type: []string{},
|
Type: CidFormatRes{},
|
||||||
|
}
|
||||||
|
|
||||||
|
type CidFormatRes struct {
|
||||||
|
CidStr string // Original Cid String passed in
|
||||||
|
Formatted string // Formated Result
|
||||||
|
ErrorMsg string // Error
|
||||||
}
|
}
|
||||||
|
|
||||||
var base32Cmd = &cmds.Command{
|
var base32Cmd = &cmds.Command{
|
||||||
@ -103,7 +106,7 @@ var base32Cmd = &cmds.Command{
|
|||||||
Tagline: "Convert CIDs to Base32 CID version 1.",
|
Tagline: "Convert CIDs to Base32 CID version 1.",
|
||||||
},
|
},
|
||||||
Arguments: []cmdkit.Argument{
|
Arguments: []cmdkit.Argument{
|
||||||
cmdkit.StringArg("cid", true, true, "Cids to convert."),
|
cmdkit.StringArg("cid", true, true, "Cids to convert.").EnableStdin(),
|
||||||
},
|
},
|
||||||
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
|
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
|
||||||
opts := cidFormatOpts{
|
opts := cidFormatOpts{
|
||||||
@ -111,16 +114,10 @@ var base32Cmd = &cmds.Command{
|
|||||||
newBase: mbase.Encoding(mbase.Base32),
|
newBase: mbase.Encoding(mbase.Base32),
|
||||||
verConv: toCidV1,
|
verConv: toCidV1,
|
||||||
}
|
}
|
||||||
res, err := formatCids(req.Arguments, opts)
|
return emitCids(req, resp, opts)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
cmds.EmitOnce(resp, res)
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
Encoders: cidFmtCmd.Encoders,
|
PostRun: cidFmtCmd.PostRun,
|
||||||
Type: cidFmtCmd.Type,
|
Type: cidFmtCmd.Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
type cidFormatOpts struct {
|
type cidFormatOpts struct {
|
||||||
@ -129,12 +126,19 @@ type cidFormatOpts struct {
|
|||||||
verConv func(cid cid.Cid) (cid.Cid, error)
|
verConv func(cid cid.Cid) (cid.Cid, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatCids(args []string, opts cidFormatOpts) ([]string, error) {
|
func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts) error {
|
||||||
var res []string
|
for _, cidStr := range req.Arguments {
|
||||||
for _, cidStr := range args {
|
emit := func(fmtd string, err error) {
|
||||||
|
res := &CidFormatRes{CidStr: cidStr, Formatted: fmtd}
|
||||||
|
if err != nil {
|
||||||
|
res.ErrorMsg = err.Error()
|
||||||
|
}
|
||||||
|
resp.Emit(res)
|
||||||
|
}
|
||||||
c, err := cid.Decode(cidStr)
|
c, err := cid.Decode(cidStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %v", cidStr, err)
|
emit("", err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
base := opts.newBase
|
base := opts.newBase
|
||||||
if base == -1 {
|
if base == -1 {
|
||||||
@ -143,18 +147,18 @@ func formatCids(args []string, opts cidFormatOpts) ([]string, error) {
|
|||||||
if opts.verConv != nil {
|
if opts.verConv != nil {
|
||||||
c, err = opts.verConv(c)
|
c, err = opts.verConv(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %v", cidStr, err)
|
emit("", err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
str, err := cidutil.Format(opts.fmtStr, base, c)
|
str, err := cidutil.Format(opts.fmtStr, base, c)
|
||||||
if _, ok := err.(cidutil.FormatStringError); ok {
|
if _, ok := err.(cidutil.FormatStringError); ok {
|
||||||
return nil, err
|
// no point in continuing if there is a problem with the format string
|
||||||
} else if err != nil {
|
return err
|
||||||
return nil, fmt.Errorf("%s: %v", cidStr, err)
|
|
||||||
}
|
}
|
||||||
res = append(res, str)
|
emit(str, err)
|
||||||
}
|
}
|
||||||
return res, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toCidV0(c cid.Cid) (cid.Cid, error) {
|
func toCidV0(c cid.Cid) (cid.Cid, error) {
|
||||||
|
Reference in New Issue
Block a user