47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package judgement
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Calendar says whether the organisation's schedule holds changing actions
|
|
// now: business hours and blackout windows.
|
|
type Calendar interface {
|
|
Hold(ctx context.Context, now time.Time) (reason string, held bool, err error)
|
|
}
|
|
|
|
// Timing holds every action that changes something at propose and wait outside
|
|
// business hours and inside a blackout. Reading is never held.
|
|
type Timing struct {
|
|
Calendar Calendar
|
|
// Now is the clock; nil means time.Now.
|
|
Now func() time.Time
|
|
}
|
|
|
|
// Name is the guard's name in verdicts.
|
|
func (Timing) Name() string { return "timing" }
|
|
|
|
// Advisory is false.
|
|
func (Timing) Advisory() bool { return false }
|
|
|
|
// Judge returns the timing ceiling.
|
|
func (t Timing) Judge(ctx context.Context, a Action) (Verdict, error) {
|
|
if a.Effect.ReadOnly {
|
|
return Verdict{Ceiling: ActQuietly}, nil
|
|
}
|
|
now := time.Now
|
|
if t.Now != nil {
|
|
now = t.Now
|
|
}
|
|
reason, held, err := t.Calendar.Hold(ctx, now())
|
|
if err != nil {
|
|
return Verdict{}, fmt.Errorf("read schedule: %w", err)
|
|
}
|
|
if held {
|
|
return Verdict{Ceiling: ProposeAndWait, Findings: []Finding{{Text: reason}}}, nil
|
|
}
|
|
return Verdict{Ceiling: ActQuietly}, nil
|
|
}
|