move limits logic to main

This commit is contained in:
filifa 2024-03-19 20:31:32 -05:00
parent 1892e77e4f
commit 270bf73b4d
1 changed files with 10 additions and 25 deletions

35
main.go
View File

@ -34,22 +34,6 @@ func formula(p float64) int64 {
return int64(yearsAgo * 31556926)
}
func limits(t int64, after bool, before bool) (int64, int64, error) {
llimit := int64(math.MinInt64)
ulimit := int64(math.MaxInt64)
var err error
if after && before {
err = errors.New("after and before can't both be true")
} else if after {
llimit = t
} else if before {
ulimit = t
}
return llimit, ulimit, err
}
func (db *TimelineDB) closestEvents(t int64, n int, llimit int64, ulimit int64) ([]EventsRow, error) {
query := `
select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown
@ -116,10 +100,6 @@ func main() {
before := flag.Bool("b", false, "only return events occurring before computed timestamp")
flag.Parse()
if *after && *before {
log.Fatal("cannot pass both -a and -b")
}
if *percent < 0 || *percent > 1 {
log.Fatal("invalid percentage")
} else if *nevents < 0 {
@ -128,6 +108,16 @@ func main() {
t := time.Now().Unix() - formula(*percent)
llimit := int64(math.MinInt64)
ulimit := int64(math.MaxInt64)
if *after && *before {
log.Fatal("cannot pass both -a and -b")
} else if *after {
llimit = t
} else if *before {
ulimit = t
}
var db TimelineDB
var err error
db.DB, err = sql.Open("sqlite3", "./timeline.db")
@ -136,11 +126,6 @@ func main() {
}
defer db.Close()
llimit, ulimit, err := limits(t, *after, *before)
if err != nil {
log.Fatal(err)
}
events, err := db.closestEvents(t, *nevents, llimit, ulimit)
if err != nil {
log.Fatal(err)