mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
47 lines
1,003 B
Go
47 lines
1,003 B
Go
package bird
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
)
|
|
|
|
// Get last reconfig timestamp from file modification date
|
|
func lastReconfigTimestampFromFileStat(filename string) string {
|
|
info, err := os.Stat(filename)
|
|
if err != nil {
|
|
return fmt.Sprintf("Could not fetch file modified timestamp: %s", err)
|
|
}
|
|
|
|
modTime := info.ModTime().UTC()
|
|
buf, _ := modTime.MarshalJSON()
|
|
|
|
return string(buf)
|
|
}
|
|
|
|
// Parse config file linewise, find matching line and extract date
|
|
func lastReconfigTimestampFromFileContent(filename string, regex string) string {
|
|
rx := regexp.MustCompile(regex)
|
|
|
|
// Read config file linewise
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
return fmt.Sprintf("Could not read: %s", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
matches := rx.FindStringSubmatch(scanner.Text())
|
|
if len(matches) > 0 {
|
|
return matches[1]
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return fmt.Sprintf("Error reading config: %s", err)
|
|
}
|
|
|
|
return ""
|
|
}
|