pin: handle progress in post-run, everything else in the encoder
Otherwise, we break the HTTP API (slightly). License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Nov 7, 2018 at 12:59 UTC
5c15857e33bb279660161bcb4249cdde4124f27c
1 file changed
+22
-16
core/commands/pin.go
+22
-16
@@ -135,6 +135,23 @@ var addPinCmd = &cmds.Command{
135
}
136
}
137
},
138
+ Encoders: cmds.EncoderMap{
139
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *AddPinOutput) error {
140
+ rec, found := req.Options["recursive"].(bool)
141
+ var pintype string
142
+ if rec || !found {
143
+ pintype = "recursively"
144
+ } else {
145
+ pintype = "directly"
146
+ }
147
+
148
+ for _, k := range out.Pins {
149
+ fmt.Fprintf(w, "pinned %s %s\n", k, pintype)
150
+ }
151
+
152
+ return nil
153
+ }),
154
+ },
155
PostRun: cmds.PostRunMap{
156
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
157
for {
@@ -150,25 +167,14 @@ var addPinCmd = &cmds.Command{
167
if !ok {
168
return e.TypeErr(out, v)
169
}
153
- var added []string
154
-
155
- if out.Pins != nil {
156
- added = out.Pins
157
- } else {
170
+ if out.Pins == nil {
171
// this can only happen if the progress option is set
172
fmt.Fprintf(os.Stderr, "Fetched/Processed %d nodes\r", out.Progress)
160
- }
161
-
162
- var pintype string
163
- rec, found := res.Request().Options["recursive"].(bool)
164
- if rec || !found {
165
- pintype = "recursively"
173
} else {
167
- pintype = "directly"
168
- }
169
-
170
- for _, k := range added {
171
- fmt.Fprintf(os.Stdout, "pinned %s %s\n", k, pintype)
174
+ err = re.Emit(out)
175
+ if err != nil {
176
+ return err
177
+ }
178
}
179
}
180
},