@@ -90,9 +90,11 @@ func main() {
9090 if * workers < 0 {
9191 log .Fatalf ("-j must >= 0" )
9292 }
93+
9394 if * workers == 0 {
9495 * workers = runtime .NumCPU ()
9596 }
97+
9698 if * debugFlag {
9799 * workers = 1
98100 }
@@ -152,13 +154,16 @@ func main() {
152154 // Output logger
153155 //
154156 var cleanup func () error
157+
155158 output , cleanup = newLogger (* quietFlag , * outFlag )
159+
156160 defer func () { _ = cleanup () }()
157161
158162 //
159163 // Custom output format
160164 //
161165 var err error
166+
162167 defaultWrite , defaultRead , err = createTemplates (* format )
163168 if err != nil {
164169 log .Fatal (err )
@@ -176,14 +181,18 @@ func main() {
176181 r .Compile ()
177182
178183 args := flag .Args ()
184+
179185 debug .Printf ("initialization complete in %v" , time .Since (t ))
180186
181187 // stdin/stdout
182188 if len (args ) == 0 {
183189 // If we are working with pipes/stdin/stdout there is no concurrency,
184190 // so we can directly send data to the writers.
185- var fileOut io.Writer
186- var errOut io.Writer
191+ var (
192+ fileOut io.Writer
193+ errOut io.Writer
194+ )
195+
187196 switch * writeit {
188197 case true :
189198 // If we are writing the corrected stream,
@@ -250,12 +259,15 @@ func main() {
250259 if err == nil && ! info .IsDir () {
251260 c <- path
252261 }
262+
253263 return nil
254264 })
255265 }
266+
256267 close (c )
257268
258269 count := 0
270+
259271 for range * workers {
260272 changed := <- results
261273 count += changed
@@ -273,6 +285,7 @@ func main() {
273285
274286func worker (writeit bool , r * misspell.Replacer , mode string , files <- chan string , results chan <- int ) {
275287 count := 0
288+
276289 for filename := range files {
277290 orig , err := misspell .ReadTextFile (filename )
278291 if err != nil {
@@ -286,8 +299,10 @@ func worker(writeit bool, r *misspell.Replacer, mode string, files <-chan string
286299
287300 debug .Printf ("Processing %s" , filename )
288301
289- var updated string
290- var changes []misspell.Diff
302+ var (
303+ updated string
304+ changes []misspell.Diff
305+ )
291306
292307 if mode == "go" {
293308 updated , changes = r .ReplaceGo (orig )
@@ -324,6 +339,7 @@ func worker(writeit bool, r *misspell.Replacer, mode string, files <-chan string
324339 os .WriteFile (filename , []byte (updated ), 0 )
325340 }
326341 }
342+
327343 results <- count
328344}
329345
@@ -332,6 +348,7 @@ func readUserDict(userDictPath string) ([]string, error) {
332348 if err != nil {
333349 return nil , fmt .Errorf ("failed to load user defined corrections %q: %w" , userDictPath , err )
334350 }
351+
335352 defer func () { _ = file .Close () }()
336353
337354 reader := csv .NewReader (file )
@@ -365,11 +382,13 @@ func createTemplates(format string) (writeTmpl, readTmpl *template.Template, err
365382 if err != nil {
366383 return nil , nil , fmt .Errorf ("unable to compile log format: %w" , err )
367384 }
385+
368386 return tmpl , tmpl , nil
369387
370388 default : // format == ""
371389 writeTmpl = template .Must (template .New ("defaultWrite" ).Parse (defaultWriteTmpl ))
372390 readTmpl = template .Must (template .New ("defaultRead" ).Parse (defaultReadTmpl ))
391+
373392 return
374393 }
375394}
@@ -393,6 +412,7 @@ func newLogger(quiet bool, outputPath string) (logger *log.Logger, cleanup func(
393412 if err != nil {
394413 log .Fatalf ("unable to create outfile %q: %s" , outputPath , err )
395414 }
415+
396416 return log .New (fo , "" , 0 ), fo .Close
397417 }
398418
0 commit comments