Skip to content

Commit 62ce03e

Browse files
overboolStebalien
authored andcommitted
cmds/pin: use PostRun in pin add
License: MIT Signed-off-by: Overbool <[email protected]>
1 parent 52cd246 commit 62ce03e

File tree

1 file changed

+65
-66
lines changed

1 file changed

+65
-66
lines changed

core/commands/pin.go

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"os"
78
"time"
89

910
core "github.com/ipfs/go-ipfs/core"
1011
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12+
e "github.com/ipfs/go-ipfs/core/commands/e"
1113
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1214
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1315
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
@@ -19,7 +21,7 @@ import (
1921
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
2022
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
2123
dag "gx/ipfs/QmaDBne4KeY3UepeqSVKYpSmQGa3q9zP6x3LfVF2UjF3Hc/go-merkledag"
22-
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
24+
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
2325
)
2426

2527
var PinCmd = &cmds.Command{
@@ -65,11 +67,6 @@ var addPinCmd = &cmds.Command{
6567
},
6668
Type: AddPinOutput{},
6769
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
68-
err := req.ParseBodyArgs()
69-
if err != nil {
70-
return err
71-
}
72-
7370
n, err := cmdenv.GetNode(env)
7471
if err != nil {
7572
return err
@@ -86,6 +83,10 @@ var addPinCmd = &cmds.Command{
8683
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
8784
showProgress, _ := req.Options[pinProgressOptionName].(bool)
8885

86+
if err := req.ParseBodyArgs(); err != nil {
87+
return err
88+
}
89+
8990
if !showProgress {
9091
added, err := corerepo.Pin(n, api, req.Context, req.Arguments, recursive)
9192
if err != nil {
@@ -94,84 +95,83 @@ var addPinCmd = &cmds.Command{
9495
return cmds.EmitOnce(res, &AddPinOutput{Pins: cidsToStrings(added)})
9596
}
9697

97-
out := make(chan interface{})
98-
9998
v := new(dag.ProgressTracker)
10099
ctx := v.DeriveContext(req.Context)
101100

102101
type pinResult struct {
103102
pins []cid.Cid
104103
err error
105104
}
105+
106106
ch := make(chan pinResult, 1)
107107
go func() {
108108
added, err := corerepo.Pin(n, api, ctx, req.Arguments, recursive)
109109
ch <- pinResult{pins: added, err: err}
110110
}()
111111

112-
errC := make(chan error)
113-
go func() {
114-
var err error
115-
ticker := time.NewTicker(500 * time.Millisecond)
116-
defer ticker.Stop()
117-
defer func() { errC <- err }()
118-
defer close(out)
112+
ticker := time.NewTicker(500 * time.Millisecond)
113+
defer ticker.Stop()
119114

120-
for {
121-
select {
122-
case val := <-ch:
123-
if val.err != nil {
124-
err = val.err
125-
return
126-
}
115+
for {
116+
select {
117+
case val := <-ch:
118+
if val.err != nil {
119+
return val.err
120+
}
127121

128-
if pv := v.Value(); pv != 0 {
129-
out <- &AddPinOutput{Progress: v.Value()}
122+
if pv := v.Value(); pv != 0 {
123+
if err := res.Emit(&AddPinOutput{Progress: v.Value()}); err != nil {
124+
return err
130125
}
131-
out <- &AddPinOutput{Pins: cidsToStrings(val.pins)}
132-
return
133-
case <-ticker.C:
134-
out <- &AddPinOutput{Progress: v.Value()}
135-
case <-ctx.Done():
136-
log.Error(ctx.Err())
137-
err = ctx.Err()
138-
return
139126
}
127+
return res.Emit(&AddPinOutput{Pins: cidsToStrings(val.pins)})
128+
case <-ticker.C:
129+
if err := res.Emit(&AddPinOutput{Progress: v.Value()}); err != nil {
130+
return err
131+
}
132+
case <-ctx.Done():
133+
log.Error(ctx.Err())
134+
return ctx.Err()
140135
}
141-
}()
142-
143-
err = res.Emit(out)
144-
if err != nil {
145-
return err
146136
}
147-
148-
return <-errC
149137
},
150-
Encoders: cmds.EncoderMap{
151-
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *AddPinOutput) error {
152-
var added []string
153-
154-
if out.Pins != nil {
155-
added = out.Pins
156-
} else {
157-
// this can only happen if the progress option is set
158-
return fmt.Errorf("Fetched/Processed %d nodes\r", out.Progress)
159-
}
138+
PostRun: cmds.PostRunMap{
139+
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
140+
for {
141+
v, err := res.Next()
142+
if err != nil {
143+
if err == io.EOF {
144+
return nil
145+
}
146+
return err
147+
}
160148

161-
var pintype string
162-
rec, found := req.Options["recursive"].(bool)
163-
if rec || !found {
164-
pintype = "recursively"
165-
} else {
166-
pintype = "directly"
167-
}
149+
out, ok := v.(*AddPinOutput)
150+
if !ok {
151+
return e.TypeErr(out, v)
152+
}
153+
var added []string
168154

169-
for _, k := range added {
170-
fmt.Fprintf(w, "pinned %s %s\n", k, pintype)
171-
}
155+
if out.Pins != nil {
156+
added = out.Pins
157+
} else {
158+
// this can only happen if the progress option is set
159+
fmt.Fprintf(os.Stderr, "Fetched/Processed %d nodes\r", out.Progress)
160+
}
172161

173-
return nil
174-
}),
162+
var pintype string
163+
rec, found := res.Request().Options["recursive"].(bool)
164+
if rec || !found {
165+
pintype = "recursively"
166+
} else {
167+
pintype = "directly"
168+
}
169+
170+
for _, k := range added {
171+
fmt.Fprintf(os.Stdout, "pinned %s %s\n", k, pintype)
172+
}
173+
}
174+
},
175175
},
176176
}
177177

@@ -192,11 +192,6 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
192192
},
193193
Type: PinOutput{},
194194
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
195-
err := req.ParseBodyArgs()
196-
if err != nil {
197-
return err
198-
}
199-
200195
n, err := cmdenv.GetNode(env)
201196
if err != nil {
202197
return err
@@ -210,6 +205,10 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
210205
// set recursive flag
211206
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
212207

208+
if err := req.ParseBodyArgs(); err != nil {
209+
return err
210+
}
211+
213212
removed, err := corerepo.Unpin(n, api, req.Context, req.Arguments, recursive)
214213
if err != nil {
215214
return err

0 commit comments

Comments
 (0)