Permalink
Browse files

marshaller options for current

Signed-off-by: Peter Edge <peter.edge@gmail.com>
  • Loading branch information...
peter-edge committed Feb 3, 2016
1 parent 262eafb commit 1db63580c4c171775e67cae30d1036cd375c1c6a
Showing with 30 additions and 24 deletions.
  1. +24 −11 current/current.go
  2. +6 −13 current/marshaller.go
@@ -34,21 +34,34 @@ func NewPusher(
return sysloglion.NewPusher(
writer,
sysloglion.PusherWithMarshaller(
newMarshaller(
token,
false,
true,
NewMarshaller(
MarshallerWithToken(token),
MarshallerDisableNewlines(),
),
),
), nil
}

// NewMarshaller returns a new Marshaller that marshals messages into JSON, appropriate
// MarshallerOption is an option for creating Marshallers.
type MarshallerOption func(*marshaller)

// MarshallerWithToken will add @current:token before every marshalled Entry.
// This is used for syslog output.
func MarshallerWithToken(token string) MarshallerOption {
return func(marshaller *marshaller) {
marshaller.token = token
}
}

// MarshallerDisableNewlines disables newlines after each marshalled Entry.
func MarshallerDisableNewlines() MarshallerOption {
return func(marshaller *marshaller) {
marshaller.disableNewlines = true
}
}

// NewMarshaller returns a new Marshaller that marshals messages into , appropriate
// to send to an io.Writer that can be tailed by the current cli tool.
func NewMarshaller() lion.Marshaller {
return newMarshaller(
"",
true,
false,
)
func NewMarshaller(options ...MarshallerOption) lion.Marshaller {
return newMarshaller(options...)
}
@@ -9,35 +9,28 @@ import (

type marshaller struct {
token string
disableHeader bool
disableNewlines bool
}

func newMarshaller(
token string,
disableHeader bool,
disableNewlines bool,
) *marshaller {
return &marshaller{
token,
disableHeader,
disableNewlines,
func newMarshaller(options ...MarshallerOption) *marshaller {
marshaller := &marshaller{"", false}
for _, option := range options {
option(marshaller)
}
return marshaller
}

func (t *marshaller) Marshal(entry *lion.Entry) ([]byte, error) {
return jsonMarshalEntry(
entry,
t.token,
t.disableHeader,
t.disableNewlines,
)
}

func jsonMarshalEntry(
entry *lion.Entry,
token string,
disableHeader bool,
disableNewlines bool,
) ([]byte, error) {
jsonEntry, err := entryToJSONEntry(entry)
@@ -48,7 +41,7 @@ func jsonMarshalEntry(
return nil, nil
}
buffer := bytes.NewBuffer(nil)
if !disableHeader {
if token != "" {
_, _ = buffer.WriteString("@current:")
_, _ = buffer.WriteString(token)
_ = buffer.WriteByte(' ')

0 comments on commit 1db6358

Please sign in to comment.