@@ -39,6 +39,7 @@ func replaceString(conf *config.Config, originalName string) string {
3939// applyReplacements applies the configured replacement patterns to the source
4040// filename.
4141func applyReplacement (
42+ ctx context.Context ,
4243 conf * config.Config ,
4344 vars * variables.Variables ,
4445 change * file.Change ,
@@ -53,7 +54,7 @@ func applyReplacement(
5354 change .Target = replaceString (conf , originalName )
5455
5556 // Replace any variables present with their corresponding values
56- err := variables .Replace (conf , change , vars )
57+ err := variables .Replace (ctx , conf , change , vars )
5758 if err != nil {
5859 return err
5960 }
@@ -73,6 +74,7 @@ func applyReplacement(
7374// replaceMatches handles the replacement of matches in each file with the
7475// replacement string.
7576func replaceMatches (
77+ ctx context.Context ,
7678 conf * config.Config ,
7779 matches file.Changes ,
7880) (file.Changes , error ) {
@@ -116,7 +118,7 @@ func replaceMatches(
116118
117119 change .Position = i - pairs
118120
119- err := applyReplacement (conf , & vars , change )
121+ err := applyReplacement (ctx , conf , & vars , change )
120122 if err != nil {
121123 return nil , err
122124 }
@@ -135,6 +137,7 @@ func replaceMatches(
135137}
136138
137139func handleReplacementChain (
140+ ctx context.Context ,
138141 conf * config.Config ,
139142 matches file.Changes ,
140143) (file.Changes , error ) {
@@ -149,7 +152,7 @@ func handleReplacementChain(
149152
150153 var err error
151154
152- matches , err = replaceMatches (conf , matches )
155+ matches , err = replaceMatches (ctx , conf , matches )
153156 if err != nil {
154157 return nil , err
155158 }
@@ -163,7 +166,7 @@ func handleReplacementChain(
163166 return nil , err
164167 }
165168
166- err = prepNextChain (conf , matches )
169+ err = prepNextChain (ctx , conf , matches )
167170 if err != nil {
168171 return nil , err
169172 }
@@ -173,6 +176,7 @@ func handleReplacementChain(
173176}
174177
175178func prepNextChain (
179+ ctx context.Context ,
176180 conf * config.Config ,
177181 matches file.Changes ,
178182) (err error ) {
@@ -187,7 +191,7 @@ func prepNextChain(
187191 }
188192 }
189193
190- g , _ := errgroup .WithContext (context . Background () )
194+ g , _ := errgroup .WithContext (ctx )
191195 g .SetLimit (runtime .NumCPU ())
192196
193197 for j := range matches {
@@ -217,7 +221,7 @@ func prepNextChain(
217221 g .Go (func () error {
218222 change .Target = conf .Search .FindCond .String ()
219223
220- err := variables .Replace (conf , change , & findVars )
224+ err := variables .Replace (ctx , conf , change , & findVars )
221225 if err != nil {
222226 return err
223227 }
@@ -262,6 +266,7 @@ func prepNextChain(
262266// preExtractMetadata parallelizes the extraction of ID3 and Hashing metadata
263267// to speed up the replacement process.
264268func preExtractMetadata (
269+ ctx context.Context ,
265270 conf * config.Config ,
266271 matches file.Changes ,
267272) error {
@@ -298,7 +303,7 @@ func preExtractMetadata(
298303 return nil
299304 }
300305
301- g , _ := errgroup .WithContext (context . Background () )
306+ g , _ := errgroup .WithContext (ctx )
302307 g .SetLimit (runtime .NumCPU ())
303308
304309 for i := range matches {
@@ -313,14 +318,14 @@ func preExtractMetadata(
313318 rv := & rvars [i ]
314319
315320 if rv .id3Present && change .ID3Data == nil {
316- err := variables .Replace (conf , change , & rv .vars )
321+ err := variables .Replace (ctx , conf , change , & rv .vars )
317322 if err != nil {
318323 return err
319324 }
320325 }
321326
322327 if rv .hashPresent && change .HashData == nil {
323- err := variables .Replace (conf , change , & rv .vars )
328+ err := variables .Replace (ctx , conf , change , & rv .vars )
324329 if err != nil {
325330 return err
326331 }
@@ -337,10 +342,11 @@ func preExtractMetadata(
337342// Replace applies the file name replacements according to the --replace
338343// argument.
339344func Replace (
345+ ctx context.Context ,
340346 conf * config.Config ,
341347 matches file.Changes ,
342348) (file.Changes , error ) {
343- err := preExtractMetadata (conf , matches )
349+ err := preExtractMetadata (ctx , conf , matches )
344350 if err != nil {
345351 return matches , err
346352 }
@@ -385,14 +391,14 @@ func Replace(
385391 return nil , extractErr
386392 }
387393
388- err = applyReplacement (conf , & vars , ch )
394+ err = applyReplacement (ctx , conf , & vars , ch )
389395 if err != nil {
390396 return nil , err
391397 }
392398 }
393399 }
394400
395- matches , err = handleReplacementChain (conf , matches )
401+ matches , err = handleReplacementChain (ctx , conf , matches )
396402 if err != nil {
397403 return nil , err
398404 }
0 commit comments