From 09c6d21866cbdc8768ef1c9b6b45b1f6dd6da2b7 Mon Sep 17 00:00:00 2001 From: Joshua Boelter Date: Fri, 7 Jul 2017 17:02:20 -0700 Subject: [PATCH] add ignore-interrupts option --- cmd/humanlog/main.go | 14 +++++++++++++- handler.go | 38 ++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/cmd/humanlog/main.go b/cmd/humanlog/main.go index 0cc2b5f..352647b 100644 --- a/cmd/humanlog/main.go +++ b/cmd/humanlog/main.go @@ -3,6 +3,8 @@ package main import ( "log" "os" + "os/signal" + "strings" "github.com/aybabtme/humanlog" "github.com/aybabtme/rgbterm" @@ -81,6 +83,11 @@ func newApp() *cli.App { Value: humanlog.DefaultOptions.TimeFormat, } + ignoreInterrupts := cli.BoolFlag{ + Name: "ignore-interrupts, i", + Usage: "ignore interrupts", + } + app := cli.NewApp() app.Author = "Antoine Grondin" app.Email = "antoine@digitalocean.com" @@ -88,7 +95,7 @@ func newApp() *cli.App { app.Version = version app.Usage = "reads structured logs from stdin, makes them pretty on stdout!" - app.Flags = []cli.Flag{skipFlag, keepFlag, sortLongest, skipUnchanged, truncates, truncateLength, lightBg, timeFormat} + app.Flags = []cli.Flag{skipFlag, keepFlag, sortLongest, skipUnchanged, truncates, truncateLength, lightBg, timeFormat, ignoreInterrupts} app.Action = func(c *cli.Context) error { @@ -99,6 +106,7 @@ func newApp() *cli.App { opts.TruncateLength = c.Int(truncateLength.Name) opts.LightBg = c.BoolT(lightBg.Name) opts.TimeFormat = c.String(timeFormat.Name) + opts.IgnoreInterrupts = c.BoolT(ignoreInterrupts.Name) switch { case c.IsSet(skipFlag.Name) && c.IsSet(keepFlag.Name): @@ -109,6 +117,10 @@ func newApp() *cli.App { opts.SetKeep(keep) } + if c.IsSet(strings.Split(ignoreInterrupts.Name, ",")[0]) { + signal.Ignore(os.Interrupt) + } + log.Print("reading stdin...") if err := humanlog.Scanner(os.Stdin, colorable.NewColorableStdout(), opts); err != nil { log.Fatalf("scanning caught an error: %v", err) diff --git a/handler.go b/handler.go index 58ed129..4695ff0 100644 --- a/handler.go +++ b/handler.go @@ -14,14 +14,15 @@ type Handler interface { } var DefaultOptions = &HandlerOptions{ - SortLongest: true, - SkipUnchanged: true, - Truncates: true, - LightBg: false, - TruncateLength: 15, - KeyRGB: RGB{1, 108, 89}, - ValRGB: RGB{125, 125, 125}, - TimeFormat: time.Stamp, + SortLongest: true, + SkipUnchanged: true, + Truncates: true, + LightBg: false, + TruncateLength: 15, + KeyRGB: RGB{1, 108, 89}, + ValRGB: RGB{125, 125, 125}, + TimeFormat: time.Stamp, + IgnoreInterrupts: false, } type RGB struct{ R, G, B uint8 } @@ -29,16 +30,17 @@ type RGB struct{ R, G, B uint8 } func (r *RGB) tuple() (uint8, uint8, uint8) { return r.R, r.G, r.B } type HandlerOptions struct { - Skip map[string]struct{} - Keep map[string]struct{} - SortLongest bool - SkipUnchanged bool - Truncates bool - LightBg bool - TruncateLength int - KeyRGB RGB - ValRGB RGB - TimeFormat string + Skip map[string]struct{} + Keep map[string]struct{} + SortLongest bool + SkipUnchanged bool + Truncates bool + LightBg bool + TruncateLength int + KeyRGB RGB + ValRGB RGB + TimeFormat string + IgnoreInterrupts bool } func (h *HandlerOptions) shouldShowKey(key string) bool {