Skip to content

Commit

Permalink
sync: add -i flag
Browse files Browse the repository at this point in the history
  • Loading branch information
miku committed May 12, 2022
1 parent 3212229 commit 28529e9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
16 changes: 13 additions & 3 deletions cmd/span-crossref-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)}
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions dateutil/intervals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions dateutil/intervals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 28529e9

Please sign in to comment.