...
1
2
3
4 package storage
5
6 import (
7 "context"
8 "fmt"
9 "path/filepath"
10 "time"
11
12 "github.com/pkg/errors"
13 )
14
15
16 var filepathglob = filepath.Glob
17
18
19
20 func WaitForFileMatchingPattern(ctx context.Context, pattern string) (string, error) {
21 for {
22 files, err := filepathglob(pattern)
23 if err != nil {
24 return "", err
25 }
26 if len(files) == 0 {
27 select {
28 case <-ctx.Done():
29 return "", errors.Wrapf(ctx.Err(), "timed out waiting for file matching pattern %s to exist", pattern)
30 default:
31 time.Sleep(time.Millisecond * 10)
32 continue
33 }
34 } else if len(files) > 1 {
35 return "", fmt.Errorf("more than one file could exist for pattern \"%s\"", pattern)
36 }
37 return files[0], nil
38 }
39 }
40
View as plain text