-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These changes enable to import codenotify as a go module and to use the now exported Subscribers function to compute subscribers from Go code rather than having to call the binary. Other than a slight code reorganization to allow that, behaviour remains unchanged.
- Loading branch information
Showing
6 changed files
with
109 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package codenotify | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Subscribers returns the list of subscribers defined in the topmost notifyFilename | ||
// found in the path. | ||
func Subscribers(fs FS, path string, notifyFilename string) ([]string, error) { | ||
subscribers := []string{} | ||
|
||
parts := strings.Split(path, string(os.PathSeparator)) | ||
for i := range parts { | ||
base := filepath.Join(parts[:i]...) | ||
rulefilepath := filepath.Join(base, notifyFilename) | ||
|
||
rulefile, err := fs.Open(rulefilepath) | ||
if err != nil { | ||
if err == os.ErrNotExist { | ||
continue | ||
} | ||
return nil, err | ||
} | ||
|
||
scanner := bufio.NewScanner(rulefile) | ||
for scanner.Scan() { | ||
rule := scanner.Text() | ||
if rule != "" && rule[0] == '#' { | ||
// skip comment | ||
continue | ||
} | ||
|
||
fields := strings.Fields(rule) | ||
switch len(fields) { | ||
case 0: | ||
// skip blank line | ||
continue | ||
case 1: | ||
return nil, fmt.Errorf("expected at least two fields for rule in %s: %s", rulefilepath, rule) | ||
} | ||
|
||
rel, err := filepath.Rel(base, path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
re, err := patternToRegexp(fields[0]) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid pattern in %s: %s: %w", rulefilepath, rule, err) | ||
} | ||
|
||
if re.MatchString(rel) { | ||
subscribers = append(subscribers, fields[1:]...) | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return subscribers, nil | ||
} | ||
|
||
func patternToRegexp(pattern string) (*regexp.Regexp, error) { | ||
if pattern[len(pattern)-1:] == "/" { | ||
pattern += "**" | ||
} | ||
pattern = regexp.QuoteMeta(pattern) | ||
pattern = strings.ReplaceAll(pattern, `/\*\*/`, "/([^/]*/)*") | ||
pattern = strings.ReplaceAll(pattern, `\*\*/`, "([^/]+/)*") | ||
pattern = strings.ReplaceAll(pattern, `/\*\*`, ".*") | ||
pattern = strings.ReplaceAll(pattern, `\*\*`, ".*") | ||
pattern = strings.ReplaceAll(pattern, `\*`, "[^/]*") | ||
pattern = "^" + pattern + "$" | ||
return regexp.Compile(pattern) | ||
} |