From d92bd070bacccebc0fc164f485b1643c39a4e13f Mon Sep 17 00:00:00 2001 From: Karolis Rusenas Date: Tue, 3 Oct 2017 20:22:11 +0100 Subject: [PATCH] parse level --- types/types.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/types/types.go b/types/types.go index 46ca34f9..a3b39437 100644 --- a/types/types.go +++ b/types/types.go @@ -9,6 +9,7 @@ package types import ( "bytes" "fmt" + "strings" "time" ) @@ -224,6 +225,46 @@ const ( LevelFatal ) +// ParseLevel takes a string level and returns notification level constant. +func ParseLevel(lvl string) (Level, error) { + switch strings.ToLower(lvl) { + case "fatal": + return LevelFatal, nil + case "error": + return LevelError, nil + case "warn", "warning": + return LevelWarn, nil + case "info": + return LevelInfo, nil + case "success": + return LevelSuccess, nil + case "debug": + return LevelDebug, nil + } + + var l Level + return l, fmt.Errorf("not a valid notification Level: %q", lvl) +} + +func (l Level) String() string { + switch l { + case LevelDebug: + return "debug" + case LevelInfo: + return "info" + case LevelSuccess: + return "success" + case LevelWarn: + return "warn" + case LevelError: + return "error" + case LevelFatal: + return "fatal" + default: + return "unknown" + } +} + // Color - used to assign different colors for events func (l Level) Color() string { switch l {