From 192e665317a9bdd35469a59a7393e636b34ba1c2 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Mon, 22 Jan 2018 11:43:52 +0100 Subject: [PATCH] rulefmt: Allow parsing from bytes instead of file This makes the rulefmt package more reusable for users whose rulesets are not stored in files on disk. Concrete example: Cortex. --- pkg/rulefmt/rulefmt.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/rulefmt/rulefmt.go b/pkg/rulefmt/rulefmt.go index e8e072f74..1f8189a99 100644 --- a/pkg/rulefmt/rulefmt.go +++ b/pkg/rulefmt/rulefmt.go @@ -174,15 +174,20 @@ func checkOverflow(m map[string]interface{}, ctx string) error { return nil } -// ParseFile parses the rule file and validates it. +// Parse parses and validates a set of rules. +func Parse(content []byte) (*RuleGroups, []error) { + var groups RuleGroups + if err := yaml.Unmarshal(content, &groups); err != nil { + return nil, []error{err} + } + return &groups, groups.Validate() +} + +// ParseFile reads and parses rules from a file. func ParseFile(file string) (*RuleGroups, []error) { b, err := ioutil.ReadFile(file) if err != nil { return nil, []error{err} } - var groups RuleGroups - if err := yaml.Unmarshal(b, &groups); err != nil { - return nil, []error{err} - } - return &groups, groups.Validate() + return Parse(b) }