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

37 lines
1.4 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 (
"strings"
"github.com/vladimir/goherence/internal/parser"
)
// DynamicIncludeRule требует, чтобы путь include_tasks/import_role был
// статической строкой, известной уже на этапе парсинга плейбука — без
// подстановки переменных внутрь пути. Это убирает GOTO-с-параметрами
// как способ "программировать" маршрутизацию тасков вместо декларативного
// описания состояния.
type DynamicIncludeRule struct{}
func (r *DynamicIncludeRule) Name() string { return "static-include-only" }
func (r *DynamicIncludeRule) Check(pb *parser.Playbook) []Violation {
var out []Violation
walkTasks(pb, func(loc string, t *parser.Task) {
if t.Module != "include_tasks" && t.Module != "import_role" {
return
}
path, _ := t.Args["path"].(string)
if path == "" {
path, _ = t.Args["name"].(string) // import_role обычно использует "name"
}
if strings.Contains(path, "{{") {
out = append(out, Violation{
Rule: r.Name(), Severity: Error, Location: loc,
Message: "путь include должен быть статическим — динамический " +
t.Module + " запрещён: " + path,
})
}
})
return out
}