this method prepares the pattern struct (aka /path/*pattern)
()
| 33 | |
| 34 | // this method prepares the pattern struct (aka /path/*pattern) |
| 35 | func (p *pattern) parse() (err error) { |
| 36 | // first we clean the value |
| 37 | absPattern, err := fastabs.FastAbs(p.value) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | p.value = absPattern |
| 43 | |
| 44 | volumeName := filepath.VolumeName(p.value) |
| 45 | p.value = strings.TrimPrefix(p.value, volumeName) |
| 46 | |
| 47 | // then we split the pattern to determine where the directory ends and the pattern starts |
| 48 | splitPattern := strings.Split(p.value, sep) |
| 49 | patternWithoutDir := "" |
| 50 | for i, part := range splitPattern { |
| 51 | isFilename := i == len(splitPattern)-1 && strings.Contains(part, ".") |
| 52 | isGlobCharacter := strings.ContainsAny(part, "[*?{") |
| 53 | |
| 54 | if isFilename || isGlobCharacter { |
| 55 | patternWithoutDir = filepath.Join(splitPattern[i:]...) |
| 56 | p.value = filepath.Join(splitPattern[:i]...) |
| 57 | |
| 58 | break |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // now we split the pattern according to the recursive '**' syntax |
| 63 | p.parsedValues = strings.Split(patternWithoutDir, "**") |
| 64 | for i, pp := range p.parsedValues { |
| 65 | p.parsedValues[i] = strings.Trim(pp, sep) |
| 66 | } |
| 67 | |
| 68 | // remove the trailing separator and add leading separator (except on Windows) |
| 69 | if volumeName == "" { |
| 70 | p.value = sep + strings.Trim(p.value, sep) |
| 71 | } else { |
| 72 | p.value = volumeName + sep + strings.Trim(p.value, sep) |
| 73 | } |
| 74 | |
| 75 | // try to canonicalize the path |
| 76 | canonicalPattern, err := filepath.EvalSymlinks(p.value) |
| 77 | if err == nil { |
| 78 | p.value = canonicalPattern |
| 79 | } |
| 80 | |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | func (p *pattern) allowReload(event *watcher.Event) bool { |
| 85 | if !isValidEventType(event.EffectType) || !isValidPathType(event) { |