2021-07-16 14:28:13 +00:00
|
|
|
package severity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-07-19 18:04:08 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-07-16 14:28:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Severity int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Info Severity = iota
|
|
|
|
Low
|
|
|
|
Medium
|
|
|
|
High
|
|
|
|
Critical
|
|
|
|
limit
|
|
|
|
)
|
|
|
|
|
|
|
|
var severityMappings = map[Severity]string{
|
|
|
|
Info: "info",
|
|
|
|
Low: "low",
|
|
|
|
Medium: "medium",
|
|
|
|
High: "high",
|
|
|
|
Critical: "critical",
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSeverity(valueToMap string) (Severity, error) {
|
|
|
|
normalizedValue := normalizeValue(valueToMap)
|
|
|
|
for key, currentValue := range severityMappings {
|
|
|
|
if normalizedValue == currentValue {
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, errors.New("Invalid severity: " + valueToMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSupportedSeverities() Severities {
|
|
|
|
var result []Severity
|
|
|
|
for index := Severity(0); index < limit; index++ {
|
|
|
|
result = append(result, index)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeValue(value string) string {
|
|
|
|
return strings.TrimSpace(strings.ToLower(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (severity Severity) String() string {
|
|
|
|
return severityMappings[severity]
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:04:08 +00:00
|
|
|
//nolint:exported,revive //prefer to be explicit about the name, and make it refactor-safe
|
|
|
|
//goland:noinspection GoNameStartsWithPackageName
|
2021-07-16 14:28:13 +00:00
|
|
|
type SeverityHolder struct {
|
|
|
|
Severity Severity
|
|
|
|
}
|
|
|
|
|
|
|
|
func (severityHolder *SeverityHolder) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2021-07-19 14:29:28 +00:00
|
|
|
var marshalledSeverity string
|
2021-07-16 14:28:13 +00:00
|
|
|
if err := unmarshal(&marshalledSeverity); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:29:28 +00:00
|
|
|
computedSeverity, err := toSeverity(marshalledSeverity)
|
2021-07-16 14:28:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
severityHolder.Severity = computedSeverity
|
|
|
|
return nil
|
|
|
|
}
|