Compare commits
4 Commits
7d2b9764c3
...
572f129ddb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
572f129ddb | ||
|
|
d05789b36b | ||
|
|
5f38a36027 | ||
|
|
d7d888453b |
36
main.go
36
main.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user