Skip to content

Commit

Permalink
refact: EventerType and improve consistency
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ksw2000 committed Nov 25, 2024
1 parent 5d7700b commit e4e7bc4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 39 deletions.
11 changes: 6 additions & 5 deletions libpod/events/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 3 additions & 16 deletions libpod/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions libpod/events/events_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
16 changes: 6 additions & 10 deletions libpod/events/events_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
4 changes: 2 additions & 2 deletions libpod/events/journal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions libpod/events/journal_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e4e7bc4

Please sign in to comment.