core/commands: Fixed progress bar line clear race condition in 'add'
Matt Bell committed
Jan 22, 2015 at 12:58 UTC
121dfb10b4b8d3a2fd4268943e65b30e5a8301a9
1 file changed
+35
-65
core/commands/add.go
+35
-65
@@ -1,7 +1,6 @@
1
package commands
2
3
import (
4
- "bytes"
4
"errors"
5
"fmt"
6
"io"
@@ -108,9 +107,13 @@ remains to be implemented.
107
res.SetError(u.ErrCast(), cmds.ErrNormal)
108
return
109
}
110
+ res.SetOutput(nil)
111
112
- wrapperChan := make(chan interface{})
113
- res.SetOutput((<-chan interface{})(wrapperChan))
112
+ quiet, _, err := res.Request().Option("quiet").Bool()
113
+ if err != nil {
114
+ res.SetError(u.ErrCast(), cmds.ErrNormal)
115
+ return
116
+ }
117
118
size := int64(0)
119
s, found := res.Request().Values()["size"]
@@ -138,78 +141,45 @@ remains to be implemented.
141
bar.Update()
142
}
143
141
- go func() {
142
- lastFile := ""
143
- var totalProgress, prevFiles, lastBytes int64
144
-
145
- for out := range outChan {
146
- output := out.(*AddedObject)
147
- if len(output.Hash) > 0 {
148
- if showProgressBar {
149
- // clear progress bar line before we print "added x" output
150
- fmt.Fprintf(os.Stderr, "\r%s\r", strings.Repeat(" ", terminalWidth))
151
- }
152
- wrapperChan <- output
153
-
154
- } else {
155
- log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
156
-
157
- if !showProgressBar {
158
- continue
159
- }
160
-
161
- if len(lastFile) == 0 {
162
- lastFile = output.Name
163
- }
164
- if output.Name != lastFile || output.Bytes < lastBytes {
165
- prevFiles += lastBytes
166
- lastFile = output.Name
167
- }
168
- lastBytes = output.Bytes
169
- delta := prevFiles + lastBytes - totalProgress
170
- totalProgress = bar.Add64(delta)
171
- }
144
+ lastFile := ""
145
+ var totalProgress, prevFiles, lastBytes int64
146
147
+ for out := range outChan {
148
+ output := out.(*AddedObject)
149
+ if len(output.Hash) > 0 {
150
if showProgressBar {
174
- bar.Update()
151
+ // clear progress bar line before we print "added x" output
152
+ fmt.Fprintf(os.Stderr, "\r%s\r", strings.Repeat(" ", terminalWidth))
153
+ }
154
+ if quiet {
155
+ fmt.Printf("%s\n", output.Hash)
156
+ } else {
157
+ fmt.Printf("added %s %s\n", output.Hash, output.Name)
158
}
176
- }
177
-
178
- close(wrapperChan)
179
- }()
180
- },
181
- Marshalers: cmds.MarshalerMap{
182
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
183
- outChan, ok := res.Output().(<-chan interface{})
184
- if !ok {
185
- return nil, u.ErrCast()
186
- }
159
188
- quiet, _, err := res.Request().Option("quiet").Bool()
189
- if err != nil {
190
- return nil, err
191
- }
160
+ } else {
161
+ log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
162
193
- marshal := func(v interface{}) (io.Reader, error) {
194
- obj, ok := v.(*AddedObject)
195
- if !ok {
196
- return nil, u.ErrCast()
163
+ if !showProgressBar {
164
+ continue
165
}
166
199
- var buf bytes.Buffer
200
- if quiet {
201
- buf.WriteString(fmt.Sprintf("%s\n", obj.Hash))
202
- } else {
203
- buf.WriteString(fmt.Sprintf("added %s %s\n", obj.Hash, obj.Name))
167
+ if len(lastFile) == 0 {
168
+ lastFile = output.Name
169
+ }
170
+ if output.Name != lastFile || output.Bytes < lastBytes {
171
+ prevFiles += lastBytes
172
+ lastFile = output.Name
173
}
205
- return &buf, nil
174
+ lastBytes = output.Bytes
175
+ delta := prevFiles + lastBytes - totalProgress
176
+ totalProgress = bar.Add64(delta)
177
}
178
208
- return &cmds.ChannelMarshaler{
209
- Channel: outChan,
210
- Marshaler: marshal,
211
- }, nil
212
- },
179
+ if showProgressBar {
180
+ bar.Update()
181
+ }
182
+ }
183
},
184
Type: AddedObject{},
185
}