2017-02-27 21:44:08 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-06-16 16:24:01 +02:00
|
|
|
"path/filepath"
|
2017-02-27 21:44:08 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mattn/go-zglob"
|
|
|
|
)
|
|
|
|
|
2017-02-27 22:03:25 +02:00
|
|
|
func minTime(a, b time.Time) time.Time {
|
|
|
|
if !a.IsZero() && a.Before(b) {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
func maxTime(a, b time.Time) time.Time {
|
|
|
|
if a.After(b) {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-06-16 16:24:01 +02:00
|
|
|
func getPatternsMinTime(dir string, patterns []string) (m time.Time, err error) {
|
2017-02-27 22:03:25 +02:00
|
|
|
for _, p := range patterns {
|
2017-07-12 11:30:28 +02:00
|
|
|
if !filepath.IsAbs(p) {
|
|
|
|
p = filepath.Join(dir, p)
|
|
|
|
}
|
2017-02-27 22:03:25 +02:00
|
|
|
mp, err := getPatternMinTime(p)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
m = minTime(m, mp)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-06-16 16:24:01 +02:00
|
|
|
func getPatternsMaxTime(dir string, patterns []string) (m time.Time, err error) {
|
2017-02-27 22:03:25 +02:00
|
|
|
for _, p := range patterns {
|
2017-07-12 11:30:28 +02:00
|
|
|
if !filepath.IsAbs(p) {
|
|
|
|
p = filepath.Join(dir, p)
|
|
|
|
}
|
2017-02-27 22:03:25 +02:00
|
|
|
mp, err := getPatternMaxTime(p)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
m = maxTime(m, mp)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPatternMinTime(pattern string) (minTime time.Time, err error) {
|
2017-02-27 21:44:08 +02:00
|
|
|
files, err := zglob.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
info, err := os.Stat(f)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
modTime := info.ModTime()
|
|
|
|
if minTime.IsZero() || modTime.Before(minTime) {
|
|
|
|
minTime = modTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:03:25 +02:00
|
|
|
func getPatternMaxTime(pattern string) (maxTime time.Time, err error) {
|
2017-02-27 21:44:08 +02:00
|
|
|
files, err := zglob.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
info, err := os.Stat(f)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
modTime := info.ModTime()
|
|
|
|
if modTime.After(maxTime) {
|
|
|
|
maxTime = modTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|