libpod/events: refactor to eliminate unused code

Shuffle the code around to eliminate "unused" warnings when linting
with various GOOS and build tags.

The only change in functionality should be that now NewEventer
returns ErrNoJournaldLogging (rather than "unknown event logger type")
on freebsd when journald is requested.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2025-03-29 19:02:49 -07:00
parent 91113c46ef
commit 6b1033eaa0
9 changed files with 35 additions and 44 deletions

View File

@ -0,0 +1,6 @@
//go:build systemd && linux
package events
// DefaultEventerType is journald when systemd is available.
const DefaultEventerType = Journald

View File

@ -0,0 +1,7 @@
//go:build !systemd || !linux
package events
// DefaultEventerType is logfile when systemd is not present
// or not supported.
const DefaultEventerType = LogFile

View File

@ -98,16 +98,6 @@ func (e *Event) ToHumanReadable(truncate bool) string {
return humanFormat
}
// newEventFromJSONString takes stringified json and converts
// it to an event
func newEventFromJSONString(event string) (*Event, error) {
e := new(Event)
if err := json.Unmarshal([]byte(event), e); err != nil {
return nil, err
}
return e, nil
}
// String converts a Type to a string
func (t Type) String() string {
return string(t)

View File

@ -1,21 +0,0 @@
package events
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
)
// NewEventer creates an eventer based on the eventer type
func NewEventer(options EventerOptions) (Eventer, error) {
logrus.Debugf("Initializing event backend %s", options.EventerType)
switch EventerType(strings.ToLower(options.EventerType)) {
case LogFile:
return EventLogFile{options}, nil
case Null:
return newNullEventer(), nil
default:
return nil, fmt.Errorf("unknown event logger type: %s", strings.ToLower(options.EventerType))
}
}

View File

@ -1,6 +1,9 @@
//go:build linux || freebsd
package events
import (
"encoding/json"
"fmt"
"strings"
@ -21,3 +24,19 @@ func NewEventer(options EventerOptions) (Eventer, error) {
return nil, fmt.Errorf("unknown event logger type: %s", strings.ToLower(options.EventerType))
}
}
// newEventFromJSONString takes stringified json and converts
// it to an event
func newEventFromJSONString(event string) (*Event, error) {
e := new(Event)
if err := json.Unmarshal([]byte(event), e); err != nil {
return nil, err
}
return e, nil
}
// newNullEventer returns a new null eventer. You should only do this for
// the purposes of internal libpod testing.
func newNullEventer() Eventer {
return EventToNull{}
}

View File

@ -1,3 +1,5 @@
//go:build linux || freebsd
package events
import (

View File

@ -17,9 +17,6 @@ import (
"github.com/sirupsen/logrus"
)
// DefaultEventerType is journald when systemd is available
const DefaultEventerType = Journald
// EventJournalD is the journald implementation of an eventer
type EventJournalD struct {
options EventerOptions

View File

@ -1,10 +1,7 @@
//go:build !systemd
//go:build (linux && !systemd) || freebsd
package events
// DefaultEventerType is logfile when systemd is not present
const DefaultEventerType = LogFile
// newJournalDEventer always returns an error if libsystemd not found
func newJournalDEventer(options EventerOptions) (Eventer, error) {
return nil, ErrNoJournaldLogging

View File

@ -19,12 +19,6 @@ func (e EventToNull) Read(ctx context.Context, options ReadOptions) error {
return errors.New("cannot read events with the \"none\" backend")
}
// newNullEventer returns a new null eventer. You should only do this for
// the purposes of internal libpod testing.
func newNullEventer() Eventer {
return EventToNull{}
}
// String returns a string representation of the logger
func (e EventToNull) String() string {
return "none"