SOGOMS v1.0.3 - Admin UI, Cron, Config reload

Phase 13 : sogoms-cron
- Jobs planifiés avec schedule cron standard
- Types: query_email, http, service
- Actions: list, trigger, status

Phase 16 : Réorganisation config/apps/{app}/
- Tous les fichiers d'une app dans un seul dossier
- Migration prokov vers nouvelle structure

Phase 17 : sogoms-admin
- Interface web d'administration (Go templates + htmx)
- Auth sessions cookies signées HMAC-SHA256
- Rôles super_admin / app_admin avec permissions

Phase 19 : Création d'app via Admin UI
- Formulaire création app avec config DB/auth
- Bouton "Scanner la base" : introspection + schema.yaml
- Rechargement automatique sogoway via SIGHUP

Infrastructure :
- sogoctl : socket de contrôle /run/sogoctl.sock
- sogoway : reload config sur SIGHUP sans restart

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-19 20:30:56 +01:00
parent a4694a10d1
commit 65da4efdad
76 changed files with 5305 additions and 80 deletions

105
internal/admin/audit.go Normal file
View File

@@ -0,0 +1,105 @@
package admin
import (
"context"
"time"
"sogoms.com/internal/protocol"
)
// AuditEvent représente un type d'événement audit.
type AuditEvent string
// Événements audit.
const (
AuditLoginSuccess AuditEvent = "login_success"
AuditLoginFailed AuditEvent = "login_failed"
AuditLogout AuditEvent = "logout"
AuditSessionExpired AuditEvent = "session_expired"
AuditActionPerformed AuditEvent = "action_performed"
AuditPermissionDenied AuditEvent = "permission_denied"
)
// AuditLogger enregistre les événements admin vers sogoms-logs.
type AuditLogger struct {
logsPool *protocol.Pool
appID string // "admin" pour les logs admin
}
// NewAuditLogger crée un nouveau logger d'audit.
func NewAuditLogger(logsPool *protocol.Pool) *AuditLogger {
return &AuditLogger{
logsPool: logsPool,
appID: "admin",
}
}
// Log enregistre un événement d'audit (non-bloquant).
func (a *AuditLogger) Log(event AuditEvent, username string, data map[string]any) {
if a.logsPool == nil {
return
}
if data == nil {
data = make(map[string]any)
}
data["username"] = username
data["event"] = string(event)
data["timestamp"] = time.Now().Format(time.RFC3339)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req := protocol.NewRequest("log_event", map[string]any{
"app_id": a.appID,
"event_type": "audit_" + string(event),
"data": data,
})
a.logsPool.Call(ctx, req)
}()
}
// LogLogin enregistre une tentative de connexion.
func (a *AuditLogger) LogLogin(success bool, username, ip, userAgent string, reason string) {
event := AuditLoginSuccess
if !success {
event = AuditLoginFailed
}
data := map[string]any{
"ip": ip,
"user_agent": userAgent,
}
if reason != "" {
data["reason"] = reason
}
a.Log(event, username, data)
}
// LogLogout enregistre une déconnexion.
func (a *AuditLogger) LogLogout(username, ip string) {
a.Log(AuditLogout, username, map[string]any{
"ip": ip,
})
}
// LogAction enregistre une action effectuée.
func (a *AuditLogger) LogAction(username, action, appID string, details map[string]any) {
data := map[string]any{
"action": action,
"app_id": appID,
"details": details,
}
a.Log(AuditActionPerformed, username, data)
}
// LogPermissionDenied enregistre un refus de permission.
func (a *AuditLogger) LogPermissionDenied(username, action, appID, permission string) {
a.Log(AuditPermissionDenied, username, map[string]any{
"action": action,
"app_id": appID,
"permission": permission,
})
}

112
internal/admin/config.go Normal file
View File

@@ -0,0 +1,112 @@
// Package admin gère la configuration et les permissions de l'interface d'administration.
package admin
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
// AdminConfig représente la configuration complète de l'admin.
type AdminConfig struct {
Session SessionConfig `yaml:"session"`
RateLimit RateLimitConfig `yaml:"rate_limit"`
Users []AdminUser `yaml:"users"`
}
// SessionConfig configure les sessions.
type SessionConfig struct {
SecretFile string `yaml:"secret_file"`
MaxAge int `yaml:"max_age"` // secondes
CookieName string `yaml:"cookie_name"`
Secret string `yaml:"-"` // chargé depuis fichier
}
// RateLimitConfig configure le rate limiting.
type RateLimitConfig struct {
LoginMax int `yaml:"login_max"`
LoginWindow int `yaml:"login_window"` // secondes
}
// AdminUser représente un utilisateur admin.
type AdminUser struct {
Username string `yaml:"username"`
PasswordHash string `yaml:"password_hash"`
Role string `yaml:"role"`
Email string `yaml:"email"`
Apps []string `yaml:"apps,omitempty"` // pour app_admin
Permissions []string `yaml:"permissions,omitempty"` // pour app_admin
}
// IsSuperAdmin retourne true si l'utilisateur est super_admin.
func (u *AdminUser) IsSuperAdmin() bool {
return u.Role == "super_admin"
}
// LoadAdminConfig charge la configuration admin depuis un fichier YAML.
func LoadAdminConfig(path string) (*AdminConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read admin config: %w", err)
}
var cfg AdminConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parse admin config: %w", err)
}
// Valeurs par défaut
if cfg.Session.MaxAge == 0 {
cfg.Session.MaxAge = 3600 // 1 heure
}
if cfg.Session.CookieName == "" {
cfg.Session.CookieName = "sogoms_admin_sid"
}
if cfg.RateLimit.LoginMax == 0 {
cfg.RateLimit.LoginMax = 5
}
if cfg.RateLimit.LoginWindow == 0 {
cfg.RateLimit.LoginWindow = 60
}
// Charger le secret de session depuis le fichier
if cfg.Session.SecretFile != "" {
secretData, err := os.ReadFile(cfg.Session.SecretFile)
if err != nil {
return nil, fmt.Errorf("read session secret: %w", err)
}
cfg.Session.Secret = strings.TrimSpace(string(secretData))
}
// Valider
if len(cfg.Users) == 0 {
return nil, fmt.Errorf("no users defined")
}
if cfg.Session.Secret == "" {
return nil, fmt.Errorf("session secret is required")
}
return &cfg, nil
}
// GetUser retourne un utilisateur par son username.
func (cfg *AdminConfig) GetUser(username string) *AdminUser {
for i := range cfg.Users {
if cfg.Users[i].Username == username {
return &cfg.Users[i]
}
}
return nil
}
// GetUserByEmail retourne un utilisateur par son email.
func (cfg *AdminConfig) GetUserByEmail(email string) *AdminUser {
for i := range cfg.Users {
if cfg.Users[i].Email == email {
return &cfg.Users[i]
}
}
return nil
}

View File

@@ -0,0 +1,131 @@
package admin
// Permission représente une permission granulaire.
type Permission string
// Permissions disponibles.
const (
PermSchemaRead Permission = "schema:read"
PermSchemaWrite Permission = "schema:write"
PermSchemaUpload Permission = "schema:upload"
PermQueriesRead Permission = "queries:read"
PermQueriesWrite Permission = "queries:write"
PermEmailsRead Permission = "emails:read"
PermEmailsWrite Permission = "emails:write"
PermCronRead Permission = "cron:read"
PermCronTrigger Permission = "cron:trigger"
PermCronWrite Permission = "cron:write"
PermLogsRead Permission = "logs:read"
PermDBIntrospect Permission = "db:introspect"
PermAll Permission = "*"
)
// PermissionChecker vérifie les droits d'un utilisateur.
type PermissionChecker struct {
config *AdminConfig
}
// NewPermissionChecker crée un nouveau vérificateur de permissions.
func NewPermissionChecker(config *AdminConfig) *PermissionChecker {
return &PermissionChecker{config: config}
}
// HasPermission vérifie si l'utilisateur a une permission pour une app.
func (pc *PermissionChecker) HasPermission(user *AdminUser, appID string, perm Permission) bool {
if user == nil {
return false
}
// Super admin a toutes les permissions
if user.IsSuperAdmin() {
return true
}
// Vérifier l'accès à l'app
if !pc.CanAccessApp(user, appID) {
return false
}
// Vérifier la permission
for _, p := range user.Permissions {
if Permission(p) == PermAll || Permission(p) == perm {
return true
}
}
return false
}
// CanAccessApp vérifie si l'utilisateur peut accéder à une app.
func (pc *PermissionChecker) CanAccessApp(user *AdminUser, appID string) bool {
if user == nil {
return false
}
// Super admin a accès à tout
if user.IsSuperAdmin() {
return true
}
// App admin : vérifier la liste des apps autorisées
for _, app := range user.Apps {
if app == appID {
return true
}
}
return false
}
// GetAccessibleApps retourne les apps accessibles par l'utilisateur.
func (pc *PermissionChecker) GetAccessibleApps(user *AdminUser, allApps []string) []string {
if user == nil {
return nil
}
// Super admin voit tout
if user.IsSuperAdmin() {
return allApps
}
// App admin : filtrer sur ses apps autorisées
var accessible []string
for _, app := range allApps {
if pc.CanAccessApp(user, app) {
accessible = append(accessible, app)
}
}
return accessible
}
// GetUserPermissions retourne les permissions effectives d'un utilisateur.
func (pc *PermissionChecker) GetUserPermissions(user *AdminUser) []Permission {
if user == nil {
return nil
}
// Super admin a toutes les permissions
if user.IsSuperAdmin() {
return []Permission{
PermSchemaRead, PermSchemaWrite, PermSchemaUpload,
PermQueriesRead, PermQueriesWrite,
PermEmailsRead, PermEmailsWrite,
PermCronRead, PermCronTrigger, PermCronWrite,
PermLogsRead,
PermDBIntrospect,
}
}
// Convertir les permissions string en Permission
perms := make([]Permission, 0, len(user.Permissions))
for _, p := range user.Permissions {
perms = append(perms, Permission(p))
}
return perms
}

View File

@@ -41,6 +41,11 @@ func NewJWT(secret string, expiration time.Duration) *JWT {
}
}
// Secret retourne le secret utilisé (pour comparaison lors du reload).
func (j *JWT) Secret() string {
return string(j.secret)
}
// Generate génère un nouveau token JWT.
func (j *JWT) Generate(userID int64, email, name, appID string) (string, error) {
now := time.Now()

View File

@@ -20,7 +20,8 @@ type AppConfig struct {
Database Database `yaml:"database"`
Auth Auth `yaml:"auth"`
Routes []Route `yaml:"routes"`
Queries *Queries // Chargé depuis config/queries/{app}/
Queries *Queries // Chargé depuis config/apps/{app}/queries/
Schema *Schema // Chargé depuis config/apps/{app}/schema.yaml
}
// Queries stocke les requêtes SQL par domaine.
@@ -48,6 +49,14 @@ func (q *Queries) Get(domain, key string) string {
return ""
}
// FileCount retourne le nombre de fichiers de queries chargés.
func (q *Queries) FileCount() int {
if q == nil || q.files == nil {
return 0
}
return len(q.files)
}
// GetMap retourne une map de requêtes (ex: login_data).
func (q *Queries) GetMap(domain, key string) map[string]string {
if q == nil || q.files == nil {
@@ -301,26 +310,33 @@ func NewRegistry(configDir string) *Registry {
}
}
// Load charge toutes les configurations depuis le répertoire routes.
// Load charge toutes les configurations depuis le répertoire apps.
func (r *Registry) Load() error {
r.mu.Lock()
defer r.mu.Unlock()
routesDir := filepath.Join(r.configDir, "routes")
entries, err := os.ReadDir(routesDir)
appsDir := filepath.Join(r.configDir, "apps")
entries, err := os.ReadDir(appsDir)
if err != nil {
return fmt.Errorf("read routes dir: %w", err)
return fmt.Errorf("read apps dir: %w", err)
}
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".yaml") {
if !entry.IsDir() {
continue
}
path := filepath.Join(routesDir, entry.Name())
cfg, err := r.loadAppConfig(path)
appID := entry.Name()
appConfigPath := filepath.Join(appsDir, appID, "app.yaml")
// Vérifier que app.yaml existe
if _, err := os.Stat(appConfigPath); os.IsNotExist(err) {
continue
}
cfg, err := r.loadAppConfig(appConfigPath, appID)
if err != nil {
return fmt.Errorf("load %s: %w", entry.Name(), err)
return fmt.Errorf("load %s: %w", appID, err)
}
r.apps[cfg.App] = cfg
@@ -333,7 +349,7 @@ func (r *Registry) Load() error {
}
// loadAppConfig charge une configuration d'application depuis un fichier YAML.
func (r *Registry) loadAppConfig(path string) (*AppConfig, error) {
func (r *Registry) loadAppConfig(path string, appID string) (*AppConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
@@ -372,15 +388,18 @@ func (r *Registry) loadAppConfig(path string) (*AppConfig, error) {
cfg.Auth.JWTExpiry = "24h"
}
// Charger les requêtes depuis config/queries/{app}/
cfg.Queries = r.loadQueries(cfg.App)
// Charger les requêtes depuis config/apps/{app}/queries/
cfg.Queries = r.loadQueries(appID)
// Charger le schema depuis config/apps/{app}/schema.yaml
cfg.Schema = loadSchema(r.configDir, appID)
return &cfg, nil
}
// loadQueries charge les fichiers de requêtes pour une application.
func (r *Registry) loadQueries(appID string) *Queries {
queriesDir := filepath.Join(r.configDir, "queries", appID)
queriesDir := filepath.Join(r.configDir, "apps", appID, "queries")
entries, err := os.ReadDir(queriesDir)
if err != nil {
return nil // Pas de répertoire queries, c'est OK

255
internal/config/schema.go Normal file
View File

@@ -0,0 +1,255 @@
// Package config - Schema représente la structure de la base de données.
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
// Schema représente le schema d'une application.
type Schema struct {
App string `yaml:"app"`
Version string `yaml:"version"`
Tables map[string]*Table `yaml:"tables"`
}
// Table représente une table de la base de données.
type Table struct {
Columns map[string]*Column `yaml:"columns"`
Primary []string `yaml:"primary,omitempty"` // Clé primaire composite
CRUD []string `yaml:"crud"`
Order string `yaml:"order,omitempty"`
}
// Column représente une colonne d'une table.
type Column struct {
Type string `yaml:"type"`
Length int64 `yaml:"length,omitempty"`
Required bool `yaml:"required,omitempty"`
Primary bool `yaml:"primary,omitempty"`
Auto bool `yaml:"auto,omitempty"`
Unique bool `yaml:"unique,omitempty"`
Default string `yaml:"default,omitempty"`
Foreign string `yaml:"foreign,omitempty"` // table.column
Filter string `yaml:"filter,omitempty"` // "owner" pour filtrage auto
}
// HasCRUD vérifie si une opération CRUD est autorisée pour cette table.
func (t *Table) HasCRUD(op string) bool {
for _, c := range t.CRUD {
if c == op {
return true
}
}
return false
}
// GetOwnerColumn retourne le nom de la colonne avec filter: owner, ou "".
func (t *Table) GetOwnerColumn() string {
for name, col := range t.Columns {
if col.Filter == "owner" {
return name
}
}
return ""
}
// GetPrimaryKey retourne le nom de la clé primaire (simple).
func (t *Table) GetPrimaryKey() string {
// D'abord chercher dans Primary (composite)
if len(t.Primary) == 1 {
return t.Primary[0]
}
// Sinon chercher une colonne avec primary: true
for name, col := range t.Columns {
if col.Primary {
return name
}
}
return "id" // Par défaut
}
// IsCompositePK vérifie si la table a une clé primaire composite.
func (t *Table) IsCompositePK() bool {
return len(t.Primary) > 1
}
// GetSelectColumns retourne la liste des colonnes pour un SELECT.
func (t *Table) GetSelectColumns() []string {
cols := make([]string, 0, len(t.Columns))
for name := range t.Columns {
cols = append(cols, name)
}
return cols
}
// GetInsertColumns retourne les colonnes pour un INSERT (exclut auto-increment).
func (t *Table) GetInsertColumns() []string {
cols := make([]string, 0, len(t.Columns))
for name, col := range t.Columns {
if !col.Auto {
cols = append(cols, name)
}
}
return cols
}
// GetUpdateColumns retourne les colonnes pour un UPDATE (exclut PK et auto).
func (t *Table) GetUpdateColumns() []string {
cols := make([]string, 0, len(t.Columns))
pk := t.GetPrimaryKey()
for name, col := range t.Columns {
if name != pk && !col.Auto && col.Filter != "owner" {
cols = append(cols, name)
}
}
return cols
}
// BuildListQuery génère la requête SELECT pour list.
func (t *Table) BuildListQuery(tableName string) string {
cols := t.GetSelectColumns()
query := fmt.Sprintf("SELECT %s FROM %s", strings.Join(cols, ", "), tableName)
// Ajouter filtre owner si présent
if ownerCol := t.GetOwnerColumn(); ownerCol != "" {
query += fmt.Sprintf(" WHERE %s = ?", ownerCol)
}
// Ajouter ORDER BY si défini
if t.Order != "" {
query += " ORDER BY " + t.Order
}
return query
}
// BuildShowQuery génère la requête SELECT pour show (un seul enregistrement).
func (t *Table) BuildShowQuery(tableName string) string {
cols := t.GetSelectColumns()
pk := t.GetPrimaryKey()
query := fmt.Sprintf("SELECT %s FROM %s WHERE %s = ?", strings.Join(cols, ", "), tableName, pk)
// Ajouter filtre owner si présent
if ownerCol := t.GetOwnerColumn(); ownerCol != "" {
query += fmt.Sprintf(" AND %s = ?", ownerCol)
}
return query
}
// BuildInsertQuery génère la requête INSERT.
func (t *Table) BuildInsertQuery(tableName string, data map[string]any) (string, []any) {
cols := make([]string, 0)
placeholders := make([]string, 0)
args := make([]any, 0)
for name, col := range t.Columns {
if col.Auto {
continue // Skip auto-increment
}
if val, ok := data[name]; ok {
cols = append(cols, name)
placeholders = append(placeholders, "?")
args = append(args, val)
} else if col.Required && col.Default == "" {
// Champ requis sans valeur et sans default - sera une erreur DB
continue
}
}
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
tableName,
strings.Join(cols, ", "),
strings.Join(placeholders, ", "))
return query, args
}
// BuildUpdateQuery génère la requête UPDATE.
func (t *Table) BuildUpdateQuery(tableName string, id any, ownerID any, data map[string]any) (string, []any) {
setClauses := make([]string, 0)
args := make([]any, 0)
pk := t.GetPrimaryKey()
ownerCol := t.GetOwnerColumn()
for name := range t.Columns {
// Skip PK, auto, et owner
if name == pk || t.Columns[name].Auto || name == ownerCol {
continue
}
if val, ok := data[name]; ok {
setClauses = append(setClauses, name+" = ?")
args = append(args, val)
}
}
if len(setClauses) == 0 {
return "", nil
}
query := fmt.Sprintf("UPDATE %s SET %s WHERE %s = ?",
tableName,
strings.Join(setClauses, ", "),
pk)
args = append(args, id)
// Ajouter filtre owner
if ownerCol != "" && ownerID != nil {
query += fmt.Sprintf(" AND %s = ?", ownerCol)
args = append(args, ownerID)
}
return query, args
}
// BuildDeleteQuery génère la requête DELETE.
func (t *Table) BuildDeleteQuery(tableName string, id any, ownerID any) (string, []any) {
pk := t.GetPrimaryKey()
query := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", tableName, pk)
args := []any{id}
// Ajouter filtre owner
if ownerCol := t.GetOwnerColumn(); ownerCol != "" && ownerID != nil {
query += fmt.Sprintf(" AND %s = ?", ownerCol)
args = append(args, ownerID)
}
return query, args
}
// ValidateInput valide les données d'entrée selon le schema.
// Retourne les données filtrées (seuls les champs connus sont gardés).
func (t *Table) ValidateInput(data map[string]any) map[string]any {
filtered := make(map[string]any)
for name := range t.Columns {
if val, ok := data[name]; ok {
filtered[name] = val
}
}
return filtered
}
// loadSchema charge le schema depuis config/apps/{app}/schema.yaml.
func loadSchema(configDir, appID string) *Schema {
schemaPath := filepath.Join(configDir, "apps", appID, "schema.yaml")
data, err := os.ReadFile(schemaPath)
if err != nil {
return nil // Pas de schema, c'est OK
}
var schema Schema
if err := yaml.Unmarshal(data, &schema); err != nil {
return nil
}
return &schema
}

238
internal/cron/scheduler.go Normal file
View File

@@ -0,0 +1,238 @@
// Package cron fournit un scheduler pour les tâches planifiées.
// Supporte le format cron standard (* * * * *) avec timezone.
package cron
import (
"fmt"
"strconv"
"strings"
"time"
)
// Schedule représente une expression cron parsée.
type Schedule struct {
Minute []int // 0-59
Hour []int // 0-23
DayOfMonth []int // 1-31
Month []int // 1-12
DayOfWeek []int // 0-6 (0=dimanche)
Location *time.Location
}
// ParseSchedule parse une expression cron standard.
// Format: "minute hour day month weekday"
// Exemples:
// - "0 8 * * 1-5" : 8h00 du lundi au vendredi
// - "*/15 * * * *" : toutes les 15 minutes
// - "0 9 1 * *" : 9h00 le premier de chaque mois
func ParseSchedule(expr string, location *time.Location) (*Schedule, error) {
if location == nil {
location = time.UTC
}
parts := strings.Fields(expr)
if len(parts) != 5 {
return nil, fmt.Errorf("invalid cron expression: expected 5 fields, got %d", len(parts))
}
s := &Schedule{Location: location}
var err error
// Minute (0-59)
s.Minute, err = parseField(parts[0], 0, 59)
if err != nil {
return nil, fmt.Errorf("minute: %w", err)
}
// Hour (0-23)
s.Hour, err = parseField(parts[1], 0, 23)
if err != nil {
return nil, fmt.Errorf("hour: %w", err)
}
// Day of month (1-31)
s.DayOfMonth, err = parseField(parts[2], 1, 31)
if err != nil {
return nil, fmt.Errorf("day of month: %w", err)
}
// Month (1-12)
s.Month, err = parseField(parts[3], 1, 12)
if err != nil {
return nil, fmt.Errorf("month: %w", err)
}
// Day of week (0-6, 0=Sunday)
s.DayOfWeek, err = parseField(parts[4], 0, 6)
if err != nil {
return nil, fmt.Errorf("day of week: %w", err)
}
return s, nil
}
// parseField parse un champ cron individuel.
// Supporte: *, */n, n, n-m, n,m,o
func parseField(field string, min, max int) ([]int, error) {
var values []int
// Gérer les listes (ex: "1,3,5")
for _, part := range strings.Split(field, ",") {
part = strings.TrimSpace(part)
// Step (*/n ou n-m/s)
step := 1
if idx := strings.Index(part, "/"); idx != -1 {
stepStr := part[idx+1:]
var err error
step, err = strconv.Atoi(stepStr)
if err != nil || step < 1 {
return nil, fmt.Errorf("invalid step: %s", stepStr)
}
part = part[:idx]
}
// Range ou valeur
var rangeMin, rangeMax int
if part == "*" {
rangeMin, rangeMax = min, max
} else if idx := strings.Index(part, "-"); idx != -1 {
var err error
rangeMin, err = strconv.Atoi(part[:idx])
if err != nil {
return nil, fmt.Errorf("invalid range start: %s", part[:idx])
}
rangeMax, err = strconv.Atoi(part[idx+1:])
if err != nil {
return nil, fmt.Errorf("invalid range end: %s", part[idx+1:])
}
} else {
val, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("invalid value: %s", part)
}
rangeMin, rangeMax = val, val
}
// Valider les bornes
if rangeMin < min || rangeMax > max || rangeMin > rangeMax {
return nil, fmt.Errorf("value out of range [%d-%d]: %d-%d", min, max, rangeMin, rangeMax)
}
// Générer les valeurs
for v := rangeMin; v <= rangeMax; v += step {
values = append(values, v)
}
}
if len(values) == 0 {
return nil, fmt.Errorf("no values")
}
return values, nil
}
// Next calcule la prochaine exécution après 'from'.
func (s *Schedule) Next(from time.Time) time.Time {
// Convertir dans la timezone du schedule
t := from.In(s.Location)
// Commencer à la minute suivante
t = t.Truncate(time.Minute).Add(time.Minute)
// Limite de recherche (1 an)
limit := t.Add(366 * 24 * time.Hour)
for t.Before(limit) {
// Vérifier le mois
if !contains(s.Month, int(t.Month())) {
// Passer au premier jour du mois suivant
t = time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, s.Location)
continue
}
// Vérifier le jour du mois ET le jour de la semaine
// En cron standard, si les deux sont spécifiés (non-*), c'est un OR
dayMatch := contains(s.DayOfMonth, t.Day())
weekdayMatch := contains(s.DayOfWeek, int(t.Weekday()))
// Si les deux champs sont "*", les deux sont vrais
// Si l'un est spécifié et pas l'autre, seul celui spécifié compte
// Si les deux sont spécifiés, c'est OR (comportement cron standard)
bothWildcard := len(s.DayOfMonth) == 31 && len(s.DayOfWeek) == 7
if bothWildcard {
// Les deux sont *, donc on accepte tous les jours
} else if len(s.DayOfMonth) == 31 {
// Jour du mois est *, seul le jour de semaine compte
if !weekdayMatch {
t = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, s.Location)
continue
}
} else if len(s.DayOfWeek) == 7 {
// Jour de semaine est *, seul le jour du mois compte
if !dayMatch {
t = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, s.Location)
continue
}
} else {
// Les deux sont spécifiés : OR
if !dayMatch && !weekdayMatch {
t = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, s.Location)
continue
}
}
// Vérifier l'heure
if !contains(s.Hour, t.Hour()) {
// Passer à l'heure suivante
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+1, 0, 0, 0, s.Location)
continue
}
// Vérifier la minute
if !contains(s.Minute, t.Minute()) {
// Passer à la minute suivante
t = t.Add(time.Minute)
continue
}
// Trouvé !
return t
}
// Pas trouvé dans l'année, retourner zero
return time.Time{}
}
// NextN retourne les N prochaines exécutions.
func (s *Schedule) NextN(from time.Time, n int) []time.Time {
var times []time.Time
t := from
for i := 0; i < n; i++ {
next := s.Next(t)
if next.IsZero() {
break
}
times = append(times, next)
t = next
}
return times
}
// contains vérifie si une valeur est dans une liste.
func contains(list []int, val int) bool {
for _, v := range list {
if v == val {
return true
}
}
return false
}
// LoadLocation charge une timezone par nom (ex: "Europe/Paris").
func LoadLocation(name string) (*time.Location, error) {
if name == "" || name == "UTC" {
return time.UTC, nil
}
return time.LoadLocation(name)
}