Skip to content

Commit

Permalink
rulefmt: Remove the need to export ruleGroups
Browse files Browse the repository at this point in the history
Fixes prometheus#7128

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie committed May 5, 2020
1 parent 532f7bb commit f711216
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions pkg/rulefmt/rulefmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,32 @@ func (err *Error) Error() string {
// RuleGroups is a set of rule groups that are typically exposed in a file.
type RuleGroups struct {
Groups []RuleGroup `yaml:"groups"`
nodes ruleGroups
}

type ruleGroups struct {
Groups []yaml.Node `yaml:"groups"`
}

// Validate validates all rules in the rule groups.
func (g *RuleGroups) Validate(node ruleGroups) (errs []error) {
func (g *RuleGroups) Validate() (errs []error) {
set := map[string]struct{}{}

for j, g := range g.Groups {
if g.Name == "" {
errs = append(errs, errors.Errorf("%d:%d: Groupname should not be empty", node.Groups[j].Line, node.Groups[j].Column))
for j, group := range g.Groups {
if group.Name == "" {
errs = append(errs, errors.Errorf("%d:%d: Groupname should not be empty", g.nodes.Groups[j].Line, g.nodes.Groups[j].Column))
}

if _, ok := set[g.Name]; ok {
if _, ok := set[group.Name]; ok {
errs = append(
errs,
errors.Errorf("%d:%d: groupname: \"%s\" is repeated in the same file", node.Groups[j].Line, node.Groups[j].Column, g.Name),
errors.Errorf("%d:%d: groupname: \"%s\" is repeated in the same file", g.nodes.Groups[j].Line, g.nodes.Groups[j].Column, group.Name),
)
}

set[g.Name] = struct{}{}
set[group.Name] = struct{}{}

for i, r := range g.Rules {
for i, r := range group.Rules {
for _, node := range r.Validate() {
var ruleName yaml.Node
if r.Alert.Value != "" {
Expand All @@ -89,7 +90,7 @@ func (g *RuleGroups) Validate(node ruleGroups) (errs []error) {
ruleName = r.Record
}
errs = append(errs, &Error{
Group: g.Name,
Group: group.Name,
Rule: i,
RuleName: ruleName.Value,
Err: node,
Expand Down Expand Up @@ -263,15 +264,14 @@ func testTemplateParsing(rl *RuleNode) (errs []error) {
func Parse(content []byte) (*RuleGroups, []error) {
var (
groups RuleGroups
node ruleGroups
errs []error
)

err := yaml.Unmarshal(content, &groups)
if err != nil {
errs = append(errs, err)
}
err = yaml.Unmarshal(content, &node)
err = yaml.Unmarshal(content, &groups.nodes)
if err != nil {
errs = append(errs, err)
}
Expand All @@ -280,7 +280,7 @@ func Parse(content []byte) (*RuleGroups, []error) {
return nil, errs
}

return &groups, groups.Validate(node)
return &groups, groups.Validate()
}

// ParseFile reads and parses rules from a file.
Expand Down

0 comments on commit f711216

Please sign in to comment.