Compare commits

...

4 Commits

Author SHA1 Message Date
Bel LaPointe
572f129ddb less 2025-06-23 12:26:10 -06:00
Bel LaPointe
d05789b36b better 2025-05-14 09:19:45 -04:00
Bel LaPointe
5f38a36027 exit with err if cancelled and newline 2025-05-14 09:15:37 -04:00
Bel LaPointe
d7d888453b shhh 2025-05-14 09:13:16 -04:00

36
main.go
View File

@@ -34,9 +34,19 @@ func main() {
}
for alert := range alerts {
if err := alertAt(ctx, *ntfy, time.Now(), alert, time.Now().Format("15:04")); err != nil {
if ctx.Err() != nil {
break
}
if err := alertAt(ctx, *ntfy, time.Now(), alert, time.Now().Format("15:04")); err != nil && ctx.Err() == nil {
panic(err)
}
if ctx.Err() != nil {
break
}
}
if err := ctx.Err(); err != nil {
os.Exit(1)
}
}
@@ -48,18 +58,30 @@ func alerts(ctx context.Context, gcal bool, args []string) (chan string, error)
if err != nil {
return nil, err
}
return alertsAfter(ctx, duration)
msg := "alerting after " + duration.String()
if len(args) > 1 {
msg = fmt.Sprintf("%s (%s)", args[1], duration.String())
}
return alertsAfter(ctx, duration, msg)
}
func alertsAfter(ctx context.Context, dur time.Duration) (chan string, error) {
func alertsAfter(ctx context.Context, dur time.Duration, msg string) (chan string, error) {
ch := make(chan string)
deadline := time.Now().Add(dur)
go func() {
defer close(ch)
select {
case <-ctx.Done():
case <-time.After(dur):
for ctx.Err() == nil && time.Now().Before(deadline) {
seconds := int(time.Until(deadline).Seconds())
fmt.Printf("\r%v ", time.Duration(seconds)*time.Second)
select {
case <-ctx.Done():
case <-time.After(time.Second):
}
}
ch <- "alerting after " + dur.String()
if ctx.Err() == nil {
fmt.Println(msg)
}
ch <- msg
}()
return ch, nil
}