-
Notifications
You must be signed in to change notification settings - Fork 0
/
snooze.go
34 lines (31 loc) · 936 Bytes
/
snooze.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package shutd
import (
"context"
"errors"
"fmt"
"time"
)
func newNotificationSnoozeTask() SchedulerTask {
return func(s *Scheduler) error {
shutdownTime, err := s.ShutdownTime()
if err != nil {
return err
}
title := fmt.Sprintf("Shutd - Shutdown at %v", shutdownTime.Format("15:04"))
text := fmt.Sprintf("Shutdown in %.0f minutes, snooze for %v minutes?", time.Until(shutdownTime).Minutes(), s.Config().SnoozeInterval)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Config().Notification.Duration)*time.Minute)
defer cancel()
yes, err := question(ctx, title, text)
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("failed to display snooze notification: %v", err)
}
s.Logger().Infof("snooze is required: %v", yes)
if yes {
err := s.Snooze()
if err != nil {
return err
}
}
return nil
}
}