From bd72e42bfe3d4de22f35b175a6236b5b18b2454d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Sat, 28 Oct 2023 17:32:48 +0200 Subject: [PATCH] feat: add force color in color ext pkg (#8767) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- cmd/cli/kubectl-kyverno/output/color/color.go | 2 +- ext/output/color/color.go | 17 +++++++++++------ ext/output/color/color_test.go | 11 ++++++++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/cli/kubectl-kyverno/output/color/color.go b/cmd/cli/kubectl-kyverno/output/color/color.go index f04196e383..8ad5d05fa1 100644 --- a/cmd/cli/kubectl-kyverno/output/color/color.go +++ b/cmd/cli/kubectl-kyverno/output/color/color.go @@ -13,7 +13,7 @@ var ( ) func Init(noColor bool) { - color.Init(noColor) + color.Init(noColor, false) if !noColor { HeaderBgColor = tablewriter.BgBlackColor HeaderFgColor = tablewriter.FgGreenColor diff --git a/ext/output/color/color.go b/ext/output/color/color.go index 46d7173422..5d148eee7c 100644 --- a/ext/output/color/color.go +++ b/ext/output/color/color.go @@ -4,17 +4,22 @@ import ( "github.com/fatih/color" ) +// Color is an alias to color.Color +type Color = color.Color + var ( - BoldGreen *color.Color - BoldRed *color.Color - BoldYellow *color.Color - BoldFgCyan *color.Color + BoldGreen *Color + BoldRed *Color + BoldYellow *Color + BoldFgCyan *Color ) -func Init(noColor bool) { - toggleColor := func(c *color.Color) *color.Color { +func Init(noColor bool, force bool) { + toggleColor := func(c *Color) *Color { if noColor { c.DisableColor() + } else if force { + c.EnableColor() } return c } diff --git a/ext/output/color/color_test.go b/ext/output/color/color_test.go index 33eb477b2d..e2412593e3 100644 --- a/ext/output/color/color_test.go +++ b/ext/output/color/color_test.go @@ -8,14 +8,23 @@ func TestInit(t *testing.T) { tests := []struct { name string noColor bool + force bool }{{ noColor: true, + force: false, + }, { + noColor: true, + force: true, }, { noColor: false, + force: false, + }, { + noColor: false, + force: true, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - Init(tt.noColor) + Init(tt.noColor, tt.force) }) } }