Skip to content

Commit

Permalink
Fix diff with noop logger
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Feb 22, 2020
1 parent f2d1a2f commit dc781fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/dots/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"strings"

"go.evanpurkhiser.com/dots/events"
"go.evanpurkhiser.com/dots/installer"
"go.evanpurkhiser.com/dots/resolver"

Expand Down Expand Up @@ -50,6 +51,12 @@ var diffCmd = cobra.Command{
OverrideInstallPath: sourceTmp,
}

// No need to output any logging, but we must process the events.
noopLogger := events.NewNoopLogger()
installConfig.EventLogger = noopLogger.GetEventChan()

defer noopLogger.LogEvents()()

dotfiles := resolver.ResolveDotfiles(*sourceConfig, *sourceLockfile)
prepared := installer.PrepareDotfiles(dotfiles.Filter(files), *sourceConfig)
installer.InstallDotfiles(prepared, installConfig)
Expand Down
38 changes: 38 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,41 @@ type Event struct {
Type EventType
Object interface{}
}

// NewNoopLogger creates a NoopLogger.
func NewNoopLogger() *NoopLogger {
logger := &NoopLogger{
eventChan: make(chan Event),
}

return logger
}

// NoopLogger may be used to handle event processing without doing
// anything with the events.
type NoopLogger struct {
eventChan chan Event
}

// GetEventChan returns the event channel in which no events will be logged.
func (l *NoopLogger) GetEventChan() chan<- Event {
return l.eventChan
}

// LogEvents processes the Event channel, but will do nothing with the events
func (l *NoopLogger) LogEvents() func() {
stop := make(chan bool)

go func() {
for {
select {
case <-l.eventChan:
continue
case <-stop:
return
}
}
}()

return func() { stop <- true }
}
4 changes: 2 additions & 2 deletions output/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Config struct {
// New creates a output logger given a configuration.
func New(config Config) *Output {
logger := &Output{
Config: config,
Config: config,
eventChan: make(chan events.Event),
}

// Get the max length of the groups
Expand All @@ -36,7 +37,6 @@ func New(config Config) *Output {
maxDotfileLength = len(d.Path)
}
}
logger.eventChan = make(chan events.Event)
logger.maxDotfileLength = maxDotfileLength

return logger
Expand Down

0 comments on commit dc781fc

Please sign in to comment.