goherence/internal/lint/rules_complexity.go
2026-09-11 10:17:25 +03:00

42 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package lint
import (
"fmt"
"github.com/vladimir/goherence/internal/parser"
)
// ComplexityRule ограничивает число условных тасков (`when`) в одной роли.
// Превышение порога — обычно значит, что роль пытается быть программой
// с ветвлениями, а не описанием желаемого состояния хостов. Это Warning,
// а не Error: сложность — вопрос вкуса и контекста, а не жёсткого правила,
// как shell-usage или dynamic-include.
type ComplexityRule struct {
MaxNesting int // зарезервировано на будущее — реальная вложенность block/block
MaxBranches int // сколько тасков с `when` допустимо в одной роли
}
func (r *ComplexityRule) Name() string { return "complexity" }
func (r *ComplexityRule) Check(pb *parser.Playbook) []Violation {
var out []Violation
for roleName, role := range pb.Roles {
branches := 0
for _, t := range role.Tasks {
if t.When != "" {
branches++
}
}
if branches > r.MaxBranches {
out = append(out, Violation{
Rule: r.Name(), Severity: Warning,
Location: "role " + roleName,
Message: fmt.Sprintf(
"условных тасков (when) = %d, порог %d — похоже на процедурный код "+
"внутри роли, стоит разбить на несколько ролей поменьше",
branches, r.MaxBranches),
})
}
}
return out
}