-
Notifications
You must be signed in to change notification settings - Fork 783
/
msgbuilder.go
39 lines (32 loc) · 1.02 KB
/
msgbuilder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package event
import (
"fmt"
"regexp"
)
//MsgKey is an identified to determine the preset message formats
type MsgKey int
//Message id for pre-defined messages
const (
FPolicyApply = iota
FResourcePolicyApply
SPolicyApply
)
func (k MsgKey) String() string {
return [...]string{
"Rule(s) '%s' failed to apply on resource %s",
"Rule(s) '%s' of policy '%s' failed to apply on the resource",
"Rule(s) '%s' successfully applied on resource %s",
}[k]
}
const argRegex = "%[s,d,v]"
var re = regexp.MustCompile(argRegex)
//GetEventMsg return the application message based on the message id and the arguments,
// if the number of arguments passed to the message are incorrect generate an error
func getEventMsg(key MsgKey, args ...interface{}) (string, error) {
// Verify the number of arguments
argsCount := len(re.FindAllString(key.String(), -1))
if argsCount != len(args) {
return "", fmt.Errorf("message expects %d arguments, but %d arguments passed", argsCount, len(args))
}
return fmt.Sprintf(key.String(), args...), nil
}