From 8e6b8d39014cd691f96ff76b7640735283a8e25f Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 18 May 2020 17:23:36 -0500 Subject: [PATCH] cleanup --- command/pr_diff.go | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/command/pr_diff.go b/command/pr_diff.go index 90cc608f3..622b50edd 100644 --- a/command/pr_diff.go +++ b/command/pr_diff.go @@ -82,6 +82,9 @@ func prDiff(cmd *cobra.Command, args []string) error { if err != nil { return err } + if !validColor(color) { + return fmt.Errorf("did not understand color: %q. Expected one of always, never, or auto", color) + } out := cmd.OutOrStdout() if color == "auto" { @@ -95,27 +98,24 @@ func prDiff(cmd *cobra.Command, args []string) error { } } - switch color { - case "always": - out = colorableOut(cmd) - for _, diffLine := range strings.Split(diff, "\n") { - output := diffLine - switch { - case bold(diffLine): - output = utils.Bold(diffLine) - case green(diffLine): - output = utils.Green(diffLine) - case red(diffLine): - output = utils.Red(diffLine) - } + if color == "never" { + fmt.Fprint(out, diff) + return nil + } - fmt.Fprintln(out, output) + out = colorableOut(cmd) + for _, diffLine := range strings.Split(diff, "\n") { + output := diffLine + switch { + case bold(diffLine): + output = utils.Bold(diffLine) + case green(diffLine): + output = utils.Green(diffLine) + case red(diffLine): + output = utils.Red(diffLine) } - case "never": - out := cmd.OutOrStdout() - fmt.Fprintf(out, diff) - default: - return fmt.Errorf("did not understand color setting %q", color) + + fmt.Fprintln(out, output) } return nil @@ -138,3 +138,7 @@ func red(dl string) bool { func green(dl string) bool { return strings.HasPrefix(dl, "+") } + +func validColor(c string) bool { + return c == "auto" || c == "always" || c == "never" +}