From 28529e9fbc36c40c990dbe90f5a67d19c02deac0 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Thu, 12 May 2022 13:04:09 +0200 Subject: [PATCH] sync: add -i flag --- cmd/span-crossref-sync/main.go | 16 +++++++++++++--- dateutil/intervals.go | 10 +++++----- dateutil/intervals_test.go | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/span-crossref-sync/main.go b/cmd/span-crossref-sync/main.go index 0559e294..0734ff7f 100644 --- a/cmd/span-crossref-sync/main.go +++ b/cmd/span-crossref-sync/main.go @@ -274,9 +274,8 @@ import ( "strings" "time" - gzip "github.com/klauspost/pgzip" - "github.com/adrg/xdg" + gzip "github.com/klauspost/pgzip" "github.com/miku/span/atomic" "github.com/miku/span/dateutil" "github.com/miku/span/xflag" @@ -297,6 +296,7 @@ var ( timeout = flag.Duration("t", 60*time.Second, "connectiont timeout") maxRetries = flag.Int("x", 10, "max retries") mode = flag.String("mode", "t", "t=tabs, s=sync") + intervals = flag.String("i", "d", "intervals: d=daily, w=weekly, m=monthly") syncStart xflag.Date = xflag.Date{Time: dateutil.MustParse("2021-01-01")} syncEnd xflag.Date = xflag.Date{Time: time.Now().UTC().Add(-24 * time.Hour)} @@ -493,8 +493,18 @@ func main() { Mode: *mode, MaxRetries: *maxRetries, } - ivs = dateutil.Daily(syncStart.Time, syncEnd.Time) + ivs []dateutil.Interval ) + switch *intervals { + case "d", "D", "daily": + ivs = dateutil.Daily(syncStart.Time, syncEnd.Time) + case "w", "W", "weekly": + ivs = dateutil.Weekly(syncStart.Time, syncEnd.Time) + case "m", "M", "monthly": + ivs = dateutil.Monthly(syncStart.Time, syncEnd.Time) + default: + log.Println("invalid interval") + } var w io.Writer = os.Stdout if *outputFile != "" { f, err := atomic.New(*outputFile, 0644) diff --git a/dateutil/intervals.go b/dateutil/intervals.go index e51a5877..eda1a4cb 100644 --- a/dateutil/intervals.go +++ b/dateutil/intervals.go @@ -30,11 +30,11 @@ func (iv Interval) String() string { } type ( - // padFunc allows to move a given time back and forth. - padFunc func(t time.Time) time.Time - // intervalFunc takes a start and endtime and returns a number of + // PadFunc allows to move a given time back and forth. + PadFunc func(t time.Time) time.Time + // IntervalFunc takes a start and endtime and returns a number of // intervals. How intervals are generated is flexible. - intervalFunc func(s, e time.Time) []Interval + IntervalFunc func(s, e time.Time) []Interval ) var ( @@ -64,7 +64,7 @@ var ( // makeIntervalFunc is a helper to create daily, weekly and other intervals. // Given two shiftFuncs (to mark the beginning of an interval and the end), we // return a function, that will allow us to generate intervals. -func makeIntervalFunc(padLeft, padRight padFunc) intervalFunc { +func makeIntervalFunc(padLeft, padRight PadFunc) IntervalFunc { return func(start, end time.Time) (result []Interval) { if end.Before(start) || end.Equal(start) { return diff --git a/dateutil/intervals_test.go b/dateutil/intervals_test.go index 5310385c..b007ae34 100644 --- a/dateutil/intervals_test.go +++ b/dateutil/intervals_test.go @@ -7,8 +7,8 @@ import ( func TestMakeIntervalFunc(t *testing.T) { var cases = []struct { - padLeft padFunc - padRight padFunc + padLeft PadFunc + padRight PadFunc start time.Time end time.Time numIntervals int