@cryptotaxi247 / kubo / commits / dfde39151

commands/filestore: use new cmds lib

License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>

Overbool committed Oct 27, 2018 at 11:57 UTC dfde39151f8c3645c2ed1dfa3217e9337b4cd5a9
1 file changed +44 -51
core/commands/filestore.go
+44 -51
@@ -5,12 +5,9 @@ import (
5 "fmt"
6 "io"
7
8 - oldCmds "github.com/ipfs/go-ipfs/commands"
9 - lgc "github.com/ipfs/go-ipfs/commands/legacy"
10 - "github.com/ipfs/go-ipfs/core"
8 + core "github.com/ipfs/go-ipfs/core"
9 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12 - e "github.com/ipfs/go-ipfs/core/commands/e"
13 - "github.com/ipfs/go-ipfs/filestore"
10 + filestore "github.com/ipfs/go-ipfs/filestore"
11
12 cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
13 cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
@@ -23,8 +20,8 @@ var FileStoreCmd = &cmds.Command{
20 },
21 Subcommands: map[string]*cmds.Command{
22 "ls": lsFileStore,
26 - "verify": lgc.NewCommand(verifyFileStore),
27 - "dups": lgc.NewCommand(dupsFileStore),
23 + "verify": verifyFileStore,
24 + "dups": dupsFileStore,
25 },
26 }
27
@@ -88,7 +85,7 @@ The output is:
85 Type: filestore.ListRes{},
86 }
87
91 -var verifyFileStore = &oldCmds.Command{
88 +var verifyFileStore = &cmds.Command{
89 Helptext: cmdkit.HelpText{
90 Tagline: "Verify objects in filestore.",
91 LongDescription: `
@@ -118,69 +115,54 @@ For ERROR entries the error will also be printed to stderr.
115 Options: []cmdkit.Option{
116 cmdkit.BoolOption(fileOrderOptionName, "verify the objects based on the order of the backing file"),
117 },
121 - Run: func(req oldCmds.Request, res oldCmds.Response) {
122 - _, fs, err := getFilestore(req.InvocContext())
118 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
119 + _, fs, err := getFilestore(env)
120 if err != nil {
124 - res.SetError(err, cmdkit.ErrNormal)
125 - return
121 + return err
122 }
127 - args := req.Arguments()
123 + args := req.Arguments
124 if len(args) > 0 {
129 - out := perKeyActionToChan(req.Context(), args, func(c cid.Cid) *filestore.ListRes {
125 + out := perKeyActionToChan(req.Context, args, func(c cid.Cid) *filestore.ListRes {
126 return filestore.Verify(fs, c)
127 })
132 - res.SetOutput(out)
128 + return res.Emit(out)
129 } else {
134 - fileOrder, _, _ := req.Option(fileOrderOptionName).Bool()
130 + fileOrder, _ := req.Options[fileOrderOptionName].(bool)
131 next, err := filestore.VerifyAll(fs, fileOrder)
132 if err != nil {
137 - res.SetError(err, cmdkit.ErrNormal)
138 - return
133 + return err
134 }
140 - out := listResToChan(req.Context(), next)
141 - res.SetOutput(out)
135 + out := listResToChan(req.Context, next)
136 + return res.Emit(out)
137 }
138 },
144 - Marshalers: oldCmds.MarshalerMap{
145 - oldCmds.Text: func(res oldCmds.Response) (io.Reader, error) {
146 - v, err := unwrapOutput(res.Output())
147 - if err != nil {
148 - return nil, err
149 - }
150 -
151 - r, ok := v.(*filestore.ListRes)
152 - if !ok {
153 - return nil, e.TypeErr(r, v)
154 - }
155 -
156 - if r.Status == filestore.StatusOtherError {
157 - fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg)
139 + Encoders: cmds.EncoderMap{
140 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *filestore.ListRes) error {
141 + if out.Status == filestore.StatusOtherError {
142 + return fmt.Errorf(out.ErrorMsg)
143 }
159 - fmt.Fprintf(res.Stdout(), "%s %s\n", r.Status.Format(), r.FormatLong())
160 - return nil, nil
161 - },
144 + fmt.Fprintf(w, "%s %s\n", out.Status.Format(), out.FormatLong())
145 + return nil
146 + }),
147 },
148 Type: filestore.ListRes{},
149 }
150
166 -var dupsFileStore = &oldCmds.Command{
151 +var dupsFileStore = &cmds.Command{
152 Helptext: cmdkit.HelpText{
153 Tagline: "List blocks that are both in the filestore and standard block storage.",
154 },
170 - Run: func(req oldCmds.Request, res oldCmds.Response) {
171 - _, fs, err := getFilestore(req.InvocContext())
155 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
156 + _, fs, err := getFilestore(env)
157 if err != nil {
173 - res.SetError(err, cmdkit.ErrNormal)
174 - return
158 + return err
159 }
176 - ch, err := fs.FileManager().AllKeysChan(req.Context())
160 + ch, err := fs.FileManager().AllKeysChan(req.Context)
161 if err != nil {
178 - res.SetError(err, cmdkit.ErrNormal)
179 - return
162 + return err
163 }
164
165 out := make(chan interface{}, 128)
183 - res.SetOutput((<-chan interface{})(out))
166
167 go func() {
168 defer close(out)
@@ -189,25 +171,36 @@ var dupsFileStore = &oldCmds.Command{
171 if err != nil {
172 select {
173 case out <- &RefWrapper{Err: err.Error()}:
192 - case <-req.Context().Done():
174 + case <-req.Context.Done():
175 }
176 return
177 }
178 if have {
179 select {
180 case out <- &RefWrapper{Ref: cid.String()}:
199 - case <-req.Context().Done():
181 + case <-req.Context.Done():
182 return
183 }
184 }
185 }
186 }()
187 + return res.Emit(out)
188 + },
189 + Encoders: cmds.EncoderMap{
190 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefWrapper) error {
191 + if out.Err != "" {
192 + return fmt.Errorf(out.Err)
193 + }
194 +
195 + fmt.Fprintln(w, out.Ref)
196 +
197 + return nil
198 + }),
199 },
206 - Marshalers: refsMarshallerMap,
207 - Type: RefWrapper{},
200 + Type: RefWrapper{},
201 }
202
210 -func getFilestore(env interface{}) (*core.IpfsNode, *filestore.Filestore, error) {
203 +func getFilestore(env cmds.Environment) (*core.IpfsNode, *filestore.Filestore, error) {
204 n, err := cmdenv.GetNode(env)
205 if err != nil {
206 return nil, nil, err