Skip to content

Commit

Permalink
chore: format certificate validation time left in cert manager to be …
Browse files Browse the repository at this point in the history
…more human readable
  • Loading branch information
unkn0wn-root committed Jan 5, 2025
1 parent f636eec commit 444cf74
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion internal/crypto/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (cm *CertManager) checkCerts() {
cm.logger.Debug("Certificate valid",
zap.String("domain", domain),
zap.Time("expires_at", status.expiresAt),
zap.Duration("time_left", timeLeft))
zap.String("time_left", formatDuration(timeLeft)))

return true
})
Expand Down Expand Up @@ -388,3 +388,19 @@ func (e *EmailAlerter) Alert(domain string, expiry time.Time) error {
func (cm *CertManager) Stop() {
close(cm.stopChan)
}

// formatDuration formats time to more human readable string
func formatDuration(d time.Duration) string {
d = d.Round(time.Hour) // round to nearest hour
days := int(d.Hours() / 24)
hours := int(d.Hours()) % 24

switch {
case days > 0:
return fmt.Sprintf("%dd %dh", days, hours)
case hours > 0:
return fmt.Sprintf("%dh", hours)
default:
return "less than 1h"
}
}

0 comments on commit 444cf74

Please sign in to comment.