Go 34 lines
package hooks
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// throttleElapsed reports whether at least min has passed since the unix
// timestamp recorded in the stamp file. A missing or unparseable stamp counts
// as elapsed, so a first run always fires — mirroring the FlashOS `due()`
// helper the cockpit machinery ports.
func throttleElapsed(stamp string, now time.Time, min time.Duration) bool {
b, err := os.ReadFile(stamp)
if err != nil {
return true
}
last, perr := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64)
if perr != nil {
return true
}
return now.Sub(time.Unix(last, 0)) >= min
}
// writeStamp records now as a unix timestamp in the stamp file, creating the
// parent state dir if needed. Best-effort: an error is ignored — the throttle
// simply does not advance, which is safe (the next run re-evaluates).
func writeStamp(stamp string, now time.Time) {
_ = os.MkdirAll(filepath.Dir(stamp), 0o755)
_ = os.WriteFile(stamp, []byte(strconv.FormatInt(now.Unix(), 10)+"\n"), 0o644)
}