clean up previous work a bit
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Jul 8, 2016 at 11:09 UTC
a6af6c504bbba71a1c3b3c3cd9df1784a456157d
2 files changed
+96
-54
commands/request.go
+37
@@ -1,6 +1,7 @@
1
package commands
2
3
import (
4
+ "bufio"
5
"errors"
6
"fmt"
7
"io"
@@ -80,6 +81,7 @@ type Request interface {
81
Command() *Command
82
Values() map[string]interface{}
83
Stdin() io.Reader
84
+ VarArgs(func(string) error) error
85
86
ConvertOptions() error
87
}
@@ -187,6 +189,41 @@ func (r *request) Context() context.Context {
189
return r.rctx
190
}
191
192
+func (r *request) VarArgs(f func(string) error) error {
193
+ var i int
194
+ for i = 0; i < len(r.cmd.Arguments); i++ {
195
+ if r.cmd.Arguments[i].Variadic {
196
+ break
197
+ }
198
+ }
199
+
200
+ args := r.arguments[i:]
201
+ if len(args) > 0 {
202
+ for _, arg := range args {
203
+ err := f(arg)
204
+ if err != nil {
205
+ return err
206
+ }
207
+ }
208
+
209
+ return nil
210
+ } else {
211
+ fi, err := r.files.NextFile()
212
+ if err != nil {
213
+ return err
214
+ }
215
+
216
+ scan := bufio.NewScanner(fi)
217
+ for scan.Scan() {
218
+ err := f(scan.Text())
219
+ if err != nil {
220
+ return err
221
+ }
222
+ }
223
+ return nil
224
+ }
225
+}
226
+
227
func getContext(base context.Context, req Request) (context.Context, error) {
228
tout, found, err := req.Option("timeout").String()
229
if err != nil {
core/commands/pin.go
+59
-54
@@ -1,7 +1,6 @@
1
package commands
2
3
import (
4
- "bufio"
4
"bytes"
5
"fmt"
6
"io"
@@ -62,37 +61,25 @@ var addPinCmd = &cmds.Command{
61
return
62
}
63
65
- if len(req.Arguments()) > 0 {
66
- added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive)
67
- if err != nil {
68
- res.SetError(err, cmds.ErrNormal)
69
- return
70
- }
64
+ out := make(chan interface{})
65
+ go func(ctx context.Context) {
66
+ defer close(out)
67
+ err := req.VarArgs(func(arg string) error {
68
+ added, err := corerepo.Pin(n, ctx, []string{arg}, recursive)
69
+ if err != nil {
70
+ return err
71
+ }
72
+
73
+ out <- &PinOutput{added}
74
+ return nil
75
+ })
76
72
- res.SetOutput(&PinOutput{added})
73
- } else {
74
- fi, err := req.Files().NextFile()
77
if err != nil {
78
res.SetError(err, cmds.ErrNormal)
79
return
80
}
79
-
80
- out := make(chan interface{})
81
- go func(ctx context.Context) {
82
- defer close(out)
83
- scan := bufio.NewScanner(fi)
84
- for scan.Scan() {
85
- added, err := corerepo.Pin(n, ctx, []string{scan.Text()}, recursive)
86
- if err != nil {
87
- res.SetError(err, cmds.ErrNormal)
88
- return
89
- }
90
-
91
- out <- &PinOutput{added}
92
- }
93
- }(req.Context())
94
- res.SetOutput((<-chan interface{})(out))
95
- }
81
+ }(req.Context())
82
+ res.SetOutput((<-chan interface{})(out))
83
},
84
Marshalers: cmds.MarshalerMap{
85
cmds.Text: func(res cmds.Response) (io.Reader, error) {
@@ -112,23 +99,20 @@ var addPinCmd = &cmds.Command{
99
return buf
100
}
101
115
- switch out := res.Output().(type) {
116
- case *PinOutput:
117
- return marshalPinOutput(out), nil
118
- case <-chan interface{}:
119
-
120
- marshal := func(i interface{}) (io.Reader, error) {
121
- return marshalPinOutput(i.(*PinOutput)), nil
122
- }
123
-
124
- return &cmds.ChannelMarshaler{
125
- Res: res,
126
- Marshaler: marshal,
127
- Channel: out,
128
- }, nil
129
- default:
102
+ out, ok := res.Output().(<-chan interface{})
103
+ if !ok {
104
return nil, u.ErrCast()
105
}
106
+
107
+ marshal := func(i interface{}) (io.Reader, error) {
108
+ return marshalPinOutput(i.(*PinOutput)), nil
109
+ }
110
+
111
+ return &cmds.ChannelMarshaler{
112
+ Res: res,
113
+ Marshaler: marshal,
114
+ Channel: out,
115
+ }, nil
116
},
117
},
118
}
@@ -143,7 +127,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
127
},
128
129
Arguments: []cmds.Argument{
146
- cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned."),
130
+ cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned.").EnableStdin(),
131
},
132
Options: []cmds.Option{
133
cmds.BoolOption("recursive", "r", "Recursively unpin the object linked to by the specified object(s).").Default(true),
@@ -163,26 +147,47 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
147
return
148
}
149
166
- removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
167
- if err != nil {
168
- res.SetError(err, cmds.ErrNormal)
169
- return
170
- }
150
+ out := make(chan interface{})
151
+ go func() {
152
+ defer close(out)
153
+ err = req.VarArgs(func(arg string) error {
154
+ removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
155
+ if err != nil {
156
+ return err
157
+ }
158
172
- res.SetOutput(&PinOutput{removed})
159
+ out <- &PinOutput{removed}
160
+ return nil
161
+ })
162
+ if err != nil {
163
+ res.SetError(err, cmds.ErrNormal)
164
+ return
165
+ }
166
+ }()
167
+
168
+ res.SetOutput((<-chan interface{})(out))
169
},
170
Marshalers: cmds.MarshalerMap{
171
cmds.Text: func(res cmds.Response) (io.Reader, error) {
176
- added, ok := res.Output().(*PinOutput)
172
+ outch, ok := res.Output().(<-chan interface{})
173
if !ok {
174
return nil, u.ErrCast()
175
}
176
181
- buf := new(bytes.Buffer)
182
- for _, k := range added.Pins {
183
- fmt.Fprintf(buf, "unpinned %s\n", k)
177
+ marshal := func(i interface{}) (io.Reader, error) {
178
+ added := i.(*PinOutput)
179
+ buf := new(bytes.Buffer)
180
+ for _, k := range added.Pins {
181
+ fmt.Fprintf(buf, "unpinned %s\n", k)
182
+ }
183
+ return buf, nil
184
}
185
- return buf, nil
185
+
186
+ return &cmds.ChannelMarshaler{
187
+ Res: res,
188
+ Marshaler: marshal,
189
+ Channel: outch,
190
+ }, nil
191
},
192
},
193
}