From 09e88f31d1b4308bf82c7f4eb0dd83855ad67ffc Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Wed, 15 Jun 2016 23:17:51 +0200 Subject: toggledlog: assume command of color handling We want to use colored error messages also outside of main, so let's handle it in the logging package. The fatal logger now automatically prints red. --- internal/readpassword/read.go | 19 ++++++++---------- internal/toggledlog/log.go | 45 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 18 deletions(-) (limited to 'internal') diff --git a/internal/readpassword/read.go b/internal/readpassword/read.go index f316846..1f2a9fa 100644 --- a/internal/readpassword/read.go +++ b/internal/readpassword/read.go @@ -16,9 +16,6 @@ const ( exitCode = 9 ) -// TODO -var colorReset, colorRed string - // Once() tries to get a password from the user, either from the terminal, // extpass or stdin. func Once(extpass string) string { @@ -43,7 +40,7 @@ func Twice(extpass string) string { p1 := readPasswordTerminal("Password: ") p2 := readPasswordTerminal("Repeat: ") if p1 != p2 { - toggledlog.Fatal.Println(colorRed + "Passwords do not match" + colorReset) + toggledlog.Fatal.Println("Passwords do not match") os.Exit(exitCode) } return p1 @@ -57,12 +54,12 @@ func readPasswordTerminal(prompt string) string { // terminal.ReadPassword removes the trailing newline p, err := terminal.ReadPassword(fd) if err != nil { - toggledlog.Fatal.Printf(colorRed+"Could not read password from terminal: %v\n"+colorReset, err) + toggledlog.Fatal.Printf("Could not read password from terminal: %v\n", err) os.Exit(exitCode) } fmt.Fprintf(os.Stderr, "\n") if len(p) == 0 { - toggledlog.Fatal.Println(colorRed + "Password is empty" + colorReset) + toggledlog.Fatal.Println("Password is empty") os.Exit(exitCode) } return string(p) @@ -75,7 +72,7 @@ func readPasswordStdin() string { p := readLineUnbuffered(os.Stdin) if len(p) == 0 { fmt.Fprintf(os.Stderr, "FOOOOOO\n") - toggledlog.Fatal.Println(colorRed + "Got empty password from stdin" + colorReset) + toggledlog.Fatal.Println("Got empty password from stdin") os.Exit(exitCode) } return p @@ -91,19 +88,19 @@ func readPasswordExtpass(extpass string) string { cmd.Stderr = os.Stderr pipe, err := cmd.StdoutPipe() if err != nil { - toggledlog.Fatal.Printf(colorRed+"extpass pipe setup failed: %v\n"+colorReset, err) + toggledlog.Fatal.Printf("extpass pipe setup failed: %v", err) os.Exit(exitCode) } err = cmd.Start() if err != nil { - toggledlog.Fatal.Printf(colorRed+"extpass cmd start failed: %v\n"+colorReset, err) + toggledlog.Fatal.Printf("extpass cmd start failed: %v", err) os.Exit(exitCode) } p := readLineUnbuffered(pipe) pipe.Close() cmd.Wait() if len(p) == 0 { - toggledlog.Fatal.Println(colorRed + "extpass: password is empty" + colorReset) + toggledlog.Fatal.Println("extpass: password is empty") os.Exit(exitCode) } return p @@ -119,7 +116,7 @@ func readLineUnbuffered(r io.Reader) (l string) { return l } if err != nil { - toggledlog.Fatal.Printf(colorRed+"readLineUnbuffered: %v\n"+colorReset, err) + toggledlog.Fatal.Printf("readLineUnbuffered: %v", err) os.Exit(exitCode) } if n == 0 { diff --git a/internal/toggledlog/log.go b/internal/toggledlog/log.go index 23d7347..30fab18 100644 --- a/internal/toggledlog/log.go +++ b/internal/toggledlog/log.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "os" + + "golang.org/x/crypto/ssh/terminal" ) const ( @@ -12,6 +14,10 @@ const ( wpanicMsg = "-wpanic turns this warning into a panic: " ) +// Escape sequences for terminal colors. These will be empty strings if stdout +// is not a terminal. +var ColorReset, ColorGrey, ColorRed, ColorGreen, ColorYellow string + func JSONDump(obj interface{}) string { b, err := json.MarshalIndent(obj, "", "\t") if err != nil { @@ -27,6 +33,10 @@ type toggledLogger struct { Enabled bool // Panic after logging a message, useful in regression tests Wpanic bool + // Private prefix and postfix are used for coloring + prefix string + postfix string + *log.Logger } @@ -34,7 +44,7 @@ func (l *toggledLogger) Printf(format string, v ...interface{}) { if !l.Enabled { return } - l.Logger.Printf(format, v...) + l.Logger.Printf(l.prefix + fmt.Sprintf(format, v...) + l.postfix) if l.Wpanic { l.Logger.Panic(wpanicMsg + fmt.Sprintf(format, v...)) } @@ -43,9 +53,9 @@ func (l *toggledLogger) Println(v ...interface{}) { if !l.Enabled { return } - l.Logger.Println(v...) + l.Logger.Println(l.prefix + fmt.Sprint(v...) + l.postfix) if l.Wpanic { - l.Logger.Panic(wpanicMsg + fmt.Sprintln(v...)) + l.Logger.Panic(wpanicMsg + fmt.Sprint(v...)) } } @@ -65,8 +75,29 @@ var Warn *toggledLogger var Fatal *toggledLogger func init() { - Debug = &toggledLogger{false, false, log.New(os.Stdout, "", 0)} - Info = &toggledLogger{true, false, log.New(os.Stdout, "", 0)} - Warn = &toggledLogger{true, false, log.New(os.Stderr, "", 0)} - Fatal = &toggledLogger{true, false, log.New(os.Stderr, "", 0)} + if terminal.IsTerminal(int(os.Stdout.Fd())) { + ColorReset = "\033[0m" + ColorGrey = "\033[2m" + ColorRed = "\033[31m" + ColorGreen = "\033[32m" + ColorYellow = "\033[33m" + } + + Debug = &toggledLogger{ + Logger: log.New(os.Stdout, "", 0), + } + Info = &toggledLogger{ + Enabled: true, + Logger: log.New(os.Stdout, "", 0), + } + Warn = &toggledLogger{ + Enabled: true, + Logger: log.New(os.Stderr, "", 0), + } + Fatal = &toggledLogger{ + Enabled: true, + Logger: log.New(os.Stderr, "", 0), + prefix: ColorRed, + postfix: ColorReset, + } } -- cgit v1.2.3