From e4e7bc41f327e4d43d52d02ff94042450bcabe6a Mon Sep 17 00:00:00 2001 From: ksw2000 <13825170+ksw2000@users.noreply.github.com> Date: Mon, 25 Nov 2024 08:52:53 +0000 Subject: [PATCH] refact: EventerType and improve consistency 1. Completed the EventerType comment. 2. Changed EventerType to be represented as a string. 3. Since EventerType is designed to be entirely lowercase, changed the comparison to use lowercase instead of uppercase. 4. Renamed newEventJournalD to newJournalDEventer. 5. Removed redundant error-checking steps in events_linux.go. Signed-off-by: ksw2000 <13825170+ksw2000@users.noreply.github.com> --- libpod/events/config.go | 11 ++++++----- libpod/events/events.go | 19 +++---------------- libpod/events/events_freebsd.go | 8 ++++---- libpod/events/events_linux.go | 16 ++++++---------- libpod/events/journal_linux.go | 4 ++-- libpod/events/journal_unsupported.go | 4 ++-- 6 files changed, 23 insertions(+), 39 deletions(-) diff --git a/libpod/events/config.go b/libpod/events/config.go index 6337e4c155..ed3f4f7078 100644 --- a/libpod/events/config.go +++ b/libpod/events/config.go @@ -6,16 +6,17 @@ import ( "time" ) -// EventerType ... -type EventerType int +// EventerType describes the type of event logger +// The string values for EventerType should be entirely lowercase. +type EventerType string const ( // LogFile indicates the event logger will be a logfile - LogFile EventerType = iota + LogFile EventerType = "file" // Journald indicates journald should be used to log events - Journald EventerType = iota + Journald EventerType = "journald" // Null is a no-op events logger. It does not read or write events. - Null EventerType = iota + Null EventerType = "none" ) // Event describes the attributes of a libpod event diff --git a/libpod/events/events.go b/libpod/events/events.go index 7b82ca5785..c69e5dab14 100644 --- a/libpod/events/events.go +++ b/libpod/events/events.go @@ -15,26 +15,13 @@ var ErrNoJournaldLogging = errors.New("no support for journald logging") // String returns a string representation of EventerType func (et EventerType) String() string { - switch et { - case LogFile: - return "file" - case Journald: - return "journald" - case Null: - return "none" - default: - return "invalid" - } + return string(et) } // IsValidEventer checks if the given string is a valid eventer type. func IsValidEventer(eventer string) bool { - switch eventer { - case LogFile.String(): - return true - case Journald.String(): - return true - case Null.String(): + switch EventerType(eventer) { + case LogFile, Journald, Null: return true default: return false diff --git a/libpod/events/events_freebsd.go b/libpod/events/events_freebsd.go index 116398d06e..64528cfdf9 100644 --- a/libpod/events/events_freebsd.go +++ b/libpod/events/events_freebsd.go @@ -10,12 +10,12 @@ import ( // NewEventer creates an eventer based on the eventer type func NewEventer(options EventerOptions) (Eventer, error) { logrus.Debugf("Initializing event backend %s", options.EventerType) - switch strings.ToUpper(options.EventerType) { - case strings.ToUpper(LogFile.String()): + switch EventerType(strings.ToLower(options.EventerType)) { + case LogFile: return EventLogFile{options}, nil - case strings.ToUpper(Null.String()): + case Null: return newNullEventer(), nil default: - return nil, fmt.Errorf("unknown event logger type: %s", strings.ToUpper(options.EventerType)) + return nil, fmt.Errorf("unknown event logger type: %s", strings.ToLower(options.EventerType)) } } diff --git a/libpod/events/events_linux.go b/libpod/events/events_linux.go index f17f9708a8..0b8d380a34 100644 --- a/libpod/events/events_linux.go +++ b/libpod/events/events_linux.go @@ -10,18 +10,14 @@ import ( // NewEventer creates an eventer based on the eventer type func NewEventer(options EventerOptions) (Eventer, error) { logrus.Debugf("Initializing event backend %s", options.EventerType) - switch strings.ToUpper(options.EventerType) { - case strings.ToUpper(Journald.String()): - eventer, err := newEventJournalD(options) - if err != nil { - return nil, fmt.Errorf("eventer creation: %w", err) - } - return eventer, nil - case strings.ToUpper(LogFile.String()): + switch EventerType(strings.ToLower(options.EventerType)) { + case Journald: + return newJournalDEventer(options) + case LogFile: return newLogFileEventer(options) - case strings.ToUpper(Null.String()): + case Null: return newNullEventer(), nil default: - return nil, fmt.Errorf("unknown event logger type: %s", strings.ToUpper(options.EventerType)) + return nil, fmt.Errorf("unknown event logger type: %s", strings.ToLower(options.EventerType)) } } diff --git a/libpod/events/journal_linux.go b/libpod/events/journal_linux.go index 8232503644..caee397c10 100644 --- a/libpod/events/journal_linux.go +++ b/libpod/events/journal_linux.go @@ -25,8 +25,8 @@ type EventJournalD struct { options EventerOptions } -// newEventJournalD creates a new journald Eventer -func newEventJournalD(options EventerOptions) (Eventer, error) { +// newJournalDEventer creates a new EventJournalD Eventer +func newJournalDEventer(options EventerOptions) (Eventer, error) { return EventJournalD{options}, nil } diff --git a/libpod/events/journal_unsupported.go b/libpod/events/journal_unsupported.go index 5cbd75acd1..10fb00b716 100644 --- a/libpod/events/journal_unsupported.go +++ b/libpod/events/journal_unsupported.go @@ -5,7 +5,7 @@ package events // DefaultEventerType is logfile when systemd is not present const DefaultEventerType = LogFile -// newEventJournalD always returns an error if libsystemd not found -func newEventJournalD(options EventerOptions) (Eventer, error) { +// newJournalDEventer always returns an error if libsystemd not found +func newJournalDEventer(options EventerOptions) (Eventer, error) { return nil, ErrNoJournaldLogging }