add config to enable/disable colored output

This commit is contained in:
Dominic Breuker
2018-03-29 08:43:13 +02:00
parent 093b9ec69c
commit 84db6dd806
5 changed files with 43 additions and 6 deletions

View File

@@ -45,6 +45,8 @@ func Start(cfg *config.Config, b *Bindings, sigCh chan os.Signal) chan struct{}
func printOutput(cfg *config.Config, b *Bindings, sigCh chan os.Signal, fsEventCh chan string, psEventCh chan string) chan struct{} {
exit := make(chan struct{})
fsEventColor, psEventColor := getColors(cfg.Colored)
go func() {
for {
select {
@@ -53,11 +55,11 @@ func printOutput(cfg *config.Config, b *Bindings, sigCh chan os.Signal, fsEventC
exit <- struct{}{}
case fe := <-fsEventCh:
if cfg.LogFS {
b.Logger.Eventf(logging.ColorGreen, "FS: %+v", fe)
b.Logger.Eventf(fsEventColor, "FS: %+v", fe)
}
case pe := <-psEventCh:
if cfg.LogPS {
b.Logger.Eventf(logging.ColorRed, "CMD: %+v", pe)
b.Logger.Eventf(psEventColor, "CMD: %+v", pe)
}
}
}
@@ -65,6 +67,17 @@ func printOutput(cfg *config.Config, b *Bindings, sigCh chan os.Signal, fsEventC
return exit
}
func getColors(colored bool) (fsEventColor int, psEventColor int) {
if colored {
fsEventColor = logging.ColorGreen
psEventColor = logging.ColorRed
} else {
fsEventColor = logging.ColorNone
psEventColor = logging.ColorNone
}
return
}
func initFSW(fsw FSWatcher, rdirs, dirs []string, logger Logger) {
errCh, doneCh := fsw.Init(rdirs, dirs)
for {

View File

@@ -67,6 +67,24 @@ func TestStartPSS(t *testing.T) {
expectMessage(t, l.Error, "ERROR: error during refresh")
}
func TestGetColors(t *testing.T) {
tests := []struct {
colored bool
fsEventColor int
psEventColor int
}{
{colored: true, fsEventColor: logging.ColorGreen, psEventColor: logging.ColorRed},
{colored: false, fsEventColor: logging.ColorNone, psEventColor: logging.ColorNone},
}
for _, tt := range tests {
c1, c2 := getColors(tt.colored)
if c1 != tt.fsEventColor || c2 != tt.psEventColor {
t.Errorf("Got wrong colors when colored=%t: expected %d, %d but got %d, %d", tt.colored, tt.fsEventColor, tt.psEventColor, c1, c2)
}
}
}
func TestStart(t *testing.T) {
drainFor := 10 * time.Millisecond
triggerEvery := 999 * time.Second
@@ -86,6 +104,7 @@ func TestStart(t *testing.T) {
LogPS: true,
DrainFor: drainFor,
TriggerEvery: triggerEvery,
Colored: true,
}
sigCh := make(chan os.Signal)
@@ -101,7 +120,7 @@ func TestStart(t *testing.T) {
}()
exitCh := Start(cfg, b, sigCh)
expectMessage(t, l.Info, "Config: Printing events: processes=true | file-system-events=true ||| Scannning for processes every 16m39s and on inotify events ||| Watching directories: [rdir1 rdir2] (recursive) | [dir1 dir2] (non-recursive)")
expectMessage(t, l.Info, "Config: Printing events (colored=true): processes=true | file-system-events=true ||| Scannning for processes every 16m39s and on inotify events ||| Watching directories: [rdir1 rdir2] (recursive) | [dir1 dir2] (non-recursive)")
expectMessage(t, l.Info, "Draining file system events due to startup...")
<-time.After(2 * drainFor)
expectMessage(t, l.Info, "done")