Skip to content

Commit a9e5058

Browse files
committed
Fix critical error in concurreny handling
1 parent 8d9818b commit a9e5058

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

main.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ func buildSearchTerm(term string) (*regexp.Regexp) {
235235
// --version
236236
// --path
237237
// --mode=...
238-
func handleSpecialCliOptions(args []string) ([]string) {
239-
238+
func handleSpecialCliOptions(args []string) {
240239
// --dumpversion
241240
if (opts.ShowOnlyVersion) {
242241
fmt.Println(Version)
@@ -280,13 +279,6 @@ func handleSpecialCliOptions(args []string) ([]string) {
280279
opts.ModeIsTemplate = true
281280
}
282281

283-
// --path
284-
if (opts.Path != "") {
285-
searchFilesInPath(opts.Path, func(f os.FileInfo, path string) {
286-
args = append(args, path)
287-
})
288-
}
289-
290282
// --output
291283
if (opts.Output != "" && len(args) > 1) {
292284
logFatalErrorAndExit(errors.New("Only one file is allowed when using --output"), 1)
@@ -301,8 +293,19 @@ func handleSpecialCliOptions(args []string) ([]string) {
301293
logFatalErrorAndExit(errors.New("Only --lineinfile-after or --lineinfile-before is allowed in --mode=lineinfile"), 1)
302294
}
303295
}
296+
}
297+
298+
func getFilelistByPath() []string {
299+
var ret []string
304300

305-
return args
301+
// --path
302+
if (opts.Path != "") {
303+
searchFilesInPath(opts.Path, func(f os.FileInfo, path string) {
304+
ret = append(ret, path)
305+
})
306+
}
307+
308+
return ret
306309
}
307310

308311
func actionProcessStdinReplace(changesets []changeset) (int) {
@@ -347,13 +350,13 @@ func actionProcessFiles(changesets []changeset, fileitems []fileitem) (int) {
347350
}
348351
}
349352

350-
results := make(chan changeresult)
351-
352-
wg := sizedwaitgroup.New(opts.ThreadCount)
353+
swg := sizedwaitgroup.New(8)
354+
results := make(chan changeresult, len(fileitems))
353355

354356
// process file list
355357
for _, file := range fileitems {
356-
wg.Add()
358+
fmt.Println(file.Path)
359+
swg.Add()
357360
go func(file fileitem, changesets []changeset) {
358361
var (
359362
err error = nil
@@ -366,16 +369,15 @@ func actionProcessFiles(changesets []changeset, fileitems []fileitem) (int) {
366369
} else {
367370
output, status, err = applyChangesetsToFile(file, changesets)
368371
}
372+
369373
results <- changeresult{file, output, status, err}
370-
wg.Done()
374+
swg.Done()
371375
} (file, changesets);
372376
}
373377

374378
// wait for all changes to be processed
375-
go func() {
376-
wg.Wait()
377-
close(results)
378-
}()
379+
swg.Wait()
380+
close(results)
379381

380382
// show results
381383
errorCount := 0
@@ -395,6 +397,7 @@ func actionProcessFiles(changesets []changeset, fileitems []fileitem) (int) {
395397
}
396398
}
397399

400+
398401
if errorCount >= 1 {
399402
fmt.Fprintln(os.Stderr, fmt.Sprintf("[ERROR] %s failed with %d error(s)", argparser.Command.Name, errorCount))
400403
return 1
@@ -432,10 +435,13 @@ func buildChangesets() ([]changeset){
432435
}
433436

434437
func buildFileitems(args []string) ([]fileitem) {
435-
var fileitems []fileitem
438+
var (
439+
fileitems []fileitem
440+
file fileitem
441+
)
436442

437443
for _, filepath := range args {
438-
file := fileitem{filepath, filepath}
444+
file = fileitem{filepath, filepath}
439445

440446
if opts.Output != "" {
441447
// use specific output
@@ -454,6 +460,25 @@ func buildFileitems(args []string) ([]fileitem) {
454460
fileitems = append(fileitems, file)
455461
}
456462

463+
// --path parsing
464+
if opts.Path != "" {
465+
for _, filepath := range getFilelistByPath() {
466+
file := fileitem{filepath, filepath}
467+
468+
if opts.Output != "" {
469+
// use specific output
470+
file.Output = opts.Output
471+
} else if opts.OutputStripFileExt != "" {
472+
// remove file ext from saving destination
473+
file.Output = strings.TrimSuffix(file.Output, opts.OutputStripFileExt)
474+
}
475+
476+
// no colon parsing here
477+
478+
fileitems = append(fileitems, file)
479+
}
480+
}
481+
457482
return fileitems
458483
}
459484

@@ -462,7 +487,7 @@ func main() {
462487
argparser = flags.NewParser(&opts, flags.PassDoubleDash)
463488
args, err := argparser.Parse()
464489

465-
args = handleSpecialCliOptions(args)
490+
handleSpecialCliOptions(args)
466491

467492
// check if there is an parse error
468493
if err != nil {

0 commit comments

Comments
 (0)