Skip to content

Commit 64147e0

Browse files
committed
refactor variables to be context-aware and provider based
1 parent d6e8626 commit 64147e0

19 files changed

Lines changed: 1687 additions & 1444 deletions

File tree

app/app_test/app_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func run(
5151

5252
var outBuf, errBuf bytes.Buffer
5353

54-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
54+
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
5555
defer cancel()
5656

5757
cmd := exec.CommandContext(ctx, bin, args...)

f2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func execute(ctx context.Context, cmd *cli.Command) error {
4747
slog.Any("config", appConfig),
4848
)
4949

50-
matches, err := find.Find(appConfig)
50+
matches, err := find.Find(ctx, appConfig)
5151
if err != nil {
5252
return err
5353
}
@@ -65,7 +65,7 @@ func execute(ctx context.Context, cmd *cli.Command) error {
6565
}
6666

6767
if !appConfig.Revert {
68-
matches, err = replace.Replace(appConfig, matches)
68+
matches, err = replace.Replace(ctx, appConfig, matches)
6969
if err != nil {
7070
return err
7171
}

find/find.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package find
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -114,14 +115,15 @@ func isMaxDepth(rootPath, currentPath string, maxDepth int) bool {
114115
}
115116

116117
func extractCustomSort(
118+
ctx context.Context,
117119
conf *config.Config,
118120
ch *file.Change,
119121
vars *variables.Variables,
120122
) error {
121123
// Temporarily set Target to SortVariable due to how variables.Replace() works
122124
ch.Target = conf.SortVariable
123125

124-
err := variables.Replace(conf, ch, vars)
126+
err := variables.Replace(ctx, conf, ch, vars)
125127
if err != nil {
126128
return err
127129
}
@@ -178,6 +180,7 @@ func createFileChange(
178180
}
179181

180182
func evaluateSearchCondition(
183+
ctx context.Context,
181184
conf *config.Config,
182185
match *file.Change,
183186
searchVars *variables.Variables,
@@ -187,7 +190,7 @@ func evaluateSearchCondition(
187190
return false, nil
188191
}
189192

190-
err = variables.Replace(conf, match, searchVars)
193+
err = variables.Replace(ctx, conf, match, searchVars)
191194
if err != nil {
192195
return true, err
193196
}
@@ -207,6 +210,7 @@ func evaluateSearchCondition(
207210
}
208211

209212
func checkIfMatch(
213+
ctx context.Context,
210214
conf *config.Config,
211215
path string,
212216
entry fs.DirEntry,
@@ -257,7 +261,7 @@ func checkIfMatch(
257261
}
258262

259263
if conf.SortVariable != "" {
260-
err = extractCustomSort(conf, match, sortVars)
264+
err = extractCustomSort(ctx, conf, match, sortVars)
261265
if err != nil {
262266
return nil, false, err
263267
}
@@ -267,6 +271,7 @@ func checkIfMatch(
267271
}
268272

269273
func walkDirectory(
274+
ctx context.Context,
270275
conf *config.Config,
271276
rootPath string,
272277
processedPaths map[string]bool,
@@ -330,6 +335,7 @@ func walkDirectory(
330335
}
331336

332337
match, isMatch, err := checkIfMatch(
338+
ctx,
333339
conf,
334340
currentPath,
335341
entry,
@@ -361,6 +367,7 @@ func shouldSkipExcludedDir(conf *config.Config, entry fs.DirEntry) bool {
361367
// searchPaths walks through the filesystem and finds matches for the provided
362368
// search pattern or variables comparison.
363369
func searchPaths(
370+
ctx context.Context,
364371
conf *config.Config,
365372
sortVars, searchVars *variables.Variables,
366373
) (file.Changes, error) {
@@ -382,6 +389,7 @@ func searchPaths(
382389
}
383390

384391
match, isMatch, matchErr := checkIfMatch(
392+
ctx,
385393
conf,
386394
rootPath,
387395
fs.FileInfoToDirEntry(fileInfo),
@@ -401,6 +409,7 @@ func searchPaths(
401409
}
402410

403411
dirMatches, err := walkDirectory(
412+
ctx,
404413
conf,
405414
rootPath,
406415
processedPaths,
@@ -416,7 +425,7 @@ func searchPaths(
416425
if conf.Search.FindCond != nil {
417426
var err error
418427

419-
matches, err = processFindExpression(conf, matches, searchVars)
428+
matches, err = processFindExpression(ctx, conf, matches, searchVars)
420429
if err != nil {
421430
return nil, err
422431
}
@@ -426,6 +435,7 @@ func searchPaths(
426435
}
427436

428437
func processFindExpression(
438+
ctx context.Context,
429439
conf *config.Config,
430440
matches file.Changes,
431441
searchVars *variables.Variables,
@@ -466,7 +476,12 @@ func processFindExpression(
466476
}
467477

468478
matches = slices.DeleteFunc(matches, func(match *file.Change) bool {
469-
removeMatch, err := evaluateSearchCondition(conf, match, searchVars)
479+
removeMatch, err := evaluateSearchCondition(
480+
ctx,
481+
conf,
482+
match,
483+
searchVars,
484+
)
470485
if err != nil {
471486
if conf.Verbose {
472487
report.SearchEvalFailed(
@@ -566,7 +581,10 @@ func loadFromBackup(conf *config.Config) (file.Changes, error) {
566581

567582
// Find returns a collection of files and directories that match the search
568583
// pattern or explicitly included as command-line arguments.
569-
func Find(conf *config.Config) (matches file.Changes, err error) {
584+
func Find(
585+
ctx context.Context,
586+
conf *config.Config,
587+
) (matches file.Changes, err error) {
570588
if conf.Revert {
571589
return loadFromBackup(conf)
572590
}
@@ -589,7 +607,7 @@ func Find(conf *config.Config) (matches file.Changes, err error) {
589607
if conf.CSVFilename != "" {
590608
matches, err = handleCSV(conf)
591609
} else {
592-
matches, err = searchPaths(conf, &sortVars, &searchVars)
610+
matches, err = searchPaths(ctx, conf, &sortVars, &searchVars)
593611
if err != nil {
594612
return nil, err
595613
}

find/find_test/find_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func findTest(t *testing.T, cases []testutil.TestCase, testDir string) {
264264
// directory argument
265265
config := testutil.GetConfig(t, &tc, testDir)
266266

267-
changes, err := find.Find(config)
267+
changes, err := find.Find(t.Context(), config)
268268
if err == nil {
269269
testutil.CompareSourcePath(t, tc.Want, changes)
270270
return

internal/testutil/testutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func GetConfig(tb testing.TB, tc *TestCase, testDir string) *config.Config {
296296
}
297297

298298
// Initialize the config
299-
err = f2App.Run(context.Background(), args)
299+
err = f2App.Run(tb.Context(), args)
300300
if err != nil {
301301
tb.Fatal(err)
302302
}

replace/replace.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func replaceString(conf *config.Config, originalName string) string {
3939
// applyReplacements applies the configured replacement patterns to the source
4040
// filename.
4141
func 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.
7576
func 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

137139
func 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

175178
func 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.
264268
func 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.
339344
func 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
}

replace/replace_test/replace_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func replaceTest(t *testing.T, cases []testutil.TestCase) {
3131

3232
conf := testutil.GetConfig(t, tc, ".")
3333

34-
changes, err := replace.Replace(conf, tc.Changes)
34+
changes, err := replace.Replace(t.Context(), conf, tc.Changes)
3535
if err == nil {
3636
testutil.CompareTargetPath(t, tc.Want, changes)
3737
return

0 commit comments

Comments
 (0)