From 6f68c98c163f55d48c61ff98afb14390f1ed2daf Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sat, 30 Dec 2017 13:52:51 +0100 Subject: [PATCH] logger.Levle: implement flag.Value --- logger/datastructures.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/logger/datastructures.go b/logger/datastructures.go index f3c9093..d6460ee 100644 --- a/logger/datastructures.go +++ b/logger/datastructures.go @@ -1,6 +1,7 @@ package logger import ( + "bytes" "encoding/json" "fmt" "github.com/pkg/errors" @@ -23,6 +24,29 @@ func (l *Level) UnmarshalJSON(input []byte) (err error) { return err } +// implement flag.Value +// implement github.com/spf13/pflag.Value +func (l *Level) Set(s string) error { + newl, err := ParseLevel(s) + if err != nil { + return err + } + *l = newl + return nil +} + +// implement github.com/spf13/pflag.Value +func (l *Level) Type() string { + var buf bytes.Buffer + for i, l := range AllLevels { + fmt.Fprintf(&buf, "%s", l) + if i != len(AllLevels)-1 { + fmt.Fprintf(&buf, "|") + } + } + return fmt.Sprintf("(%s)", buf.String()) +} + const ( Debug Level = iota Info