From 619c3baab28540115fef937abadd7bc6de3f1668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 27 Oct 2023 11:50:36 +0200 Subject: [PATCH] chore: init ext packages (#8758) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- .../kubectl-kyverno/commands/apply/command.go | 2 +- .../kubectl-kyverno/commands/test/command.go | 2 +- cmd/cli/kubectl-kyverno/commands/test/test.go | 2 +- cmd/cli/kubectl-kyverno/output/color/color.go | 41 +++++++------------ .../output/color/color_test.go | 22 +++++----- ext/output/color/color.go | 25 +++++++++++ ext/output/color/color_test.go | 21 ++++++++++ .../output/pluralize/pluralize.go | 0 .../output/pluralize/pluralize_test.go | 0 9 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 ext/output/color/color.go create mode 100644 ext/output/color/color_test.go rename {cmd/cli/kubectl-kyverno => ext}/output/pluralize/pluralize.go (100%) rename {cmd/cli/kubectl-kyverno => ext}/output/pluralize/pluralize_test.go (100%) diff --git a/cmd/cli/kubectl-kyverno/commands/apply/command.go b/cmd/cli/kubectl-kyverno/commands/apply/command.go index 14cbda6ed7..55e63282c0 100644 --- a/cmd/cli/kubectl-kyverno/commands/apply/command.go +++ b/cmd/cli/kubectl-kyverno/commands/apply/command.go @@ -76,7 +76,7 @@ func Command() *cobra.Command { SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) (err error) { out := cmd.OutOrStdout() - color.InitColors(removeColor) + color.Init(removeColor) applyCommandConfig.PolicyPaths = args rc, _, skipInvalidPolicies, responses, err := applyCommandConfig.applyCommandHelper(out) if err != nil { diff --git a/cmd/cli/kubectl-kyverno/commands/test/command.go b/cmd/cli/kubectl-kyverno/commands/test/command.go index 84725bdbb0..deb98cebc8 100644 --- a/cmd/cli/kubectl-kyverno/commands/test/command.go +++ b/cmd/cli/kubectl-kyverno/commands/test/command.go @@ -30,7 +30,7 @@ func Command() *cobra.Command { Args: cobra.MinimumNArgs(1), SilenceUsage: true, RunE: func(cmd *cobra.Command, dirPath []string) (err error) { - color.InitColors(removeColor) + color.Init(removeColor) store.SetRegistryAccess(registryAccess) return testCommandExecute(cmd.OutOrStdout(), dirPath, fileName, gitBranch, testCase, failOnly, detailedResults) }, diff --git a/cmd/cli/kubectl-kyverno/commands/test/test.go b/cmd/cli/kubectl-kyverno/commands/test/test.go index 8693b8d24c..ca94530989 100644 --- a/cmd/cli/kubectl-kyverno/commands/test/test.go +++ b/cmd/cli/kubectl-kyverno/commands/test/test.go @@ -7,7 +7,6 @@ import ( kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1" "github.com/kyverno/kyverno/api/kyverno/v1beta1" "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log" - "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/pluralize" "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/path" "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy" "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/processor" @@ -17,6 +16,7 @@ import ( "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/userinfo" "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common" "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/variables" + "github.com/kyverno/kyverno/ext/output/pluralize" "github.com/kyverno/kyverno/pkg/autogen" "github.com/kyverno/kyverno/pkg/background/generate" "github.com/kyverno/kyverno/pkg/clients/dclient" diff --git a/cmd/cli/kubectl-kyverno/output/color/color.go b/cmd/cli/kubectl-kyverno/output/color/color.go index 437419726a..f04196e383 100644 --- a/cmd/cli/kubectl-kyverno/output/color/color.go +++ b/cmd/cli/kubectl-kyverno/output/color/color.go @@ -3,30 +3,17 @@ package color import ( "strings" - "github.com/fatih/color" "github.com/kataras/tablewriter" + "github.com/kyverno/kyverno/ext/output/color" ) var ( - BoldGreen *color.Color - BoldRed *color.Color - BoldYellow *color.Color - BoldFgCyan *color.Color HeaderBgColor int HeaderFgColor int ) -func InitColors(noColor bool) { - toggleColor := func(c *color.Color) *color.Color { - if noColor { - c.DisableColor() - } - return c - } - BoldGreen = toggleColor(color.New(color.FgGreen).Add(color.Bold)) - BoldRed = toggleColor(color.New(color.FgRed).Add(color.Bold)) - BoldYellow = toggleColor(color.New(color.FgYellow).Add(color.Bold)) - BoldFgCyan = toggleColor(color.New(color.FgCyan).Add(color.Bold)) +func Init(noColor bool) { + color.Init(noColor) if !noColor { HeaderBgColor = tablewriter.BgBlackColor HeaderFgColor = tablewriter.FgGreenColor @@ -42,13 +29,13 @@ func Policy(namespace, name string) string { } } if namespace == "" { - return BoldFgCyan.Sprint(name) + return color.BoldFgCyan.Sprint(name) } - return BoldFgCyan.Sprint(namespace) + "/" + BoldFgCyan.Sprint(name) + return color.BoldFgCyan.Sprint(namespace) + "/" + color.BoldFgCyan.Sprint(name) } func Rule(name string) string { - return BoldFgCyan.Sprint(name) + return color.BoldFgCyan.Sprint(name) } func Resource(kind, namespace, name string) string { @@ -60,31 +47,31 @@ func Resource(kind, namespace, name string) string { } } if namespace == "" { - return BoldFgCyan.Sprint(kind) + "/" + BoldFgCyan.Sprint(name) + return color.BoldFgCyan.Sprint(kind) + "/" + color.BoldFgCyan.Sprint(name) } - return BoldFgCyan.Sprint(namespace) + "/" + BoldFgCyan.Sprint(kind) + "/" + BoldFgCyan.Sprint(name) + return color.BoldFgCyan.Sprint(namespace) + "/" + color.BoldFgCyan.Sprint(kind) + "/" + color.BoldFgCyan.Sprint(name) } func NotFound() string { - return BoldYellow.Sprint("Not found") + return color.BoldYellow.Sprint("Not found") } func ResultPass() string { - return BoldGreen.Sprint("Pass") + return color.BoldGreen.Sprint("Pass") } func ResultFail() string { - return BoldRed.Sprint("Fail") + return color.BoldRed.Sprint("Fail") } func ResultWarn() string { - return BoldYellow.Sprint("Warn") + return color.BoldYellow.Sprint("Warn") } func ResultError() string { - return BoldRed.Sprint("Error") + return color.BoldRed.Sprint("Error") } func ResultSkip() string { - return BoldFgCyan.Sprint("Skip") + return color.BoldFgCyan.Sprint("Skip") } diff --git a/cmd/cli/kubectl-kyverno/output/color/color_test.go b/cmd/cli/kubectl-kyverno/output/color/color_test.go index a5a1d7dbf6..35fafebdae 100644 --- a/cmd/cli/kubectl-kyverno/output/color/color_test.go +++ b/cmd/cli/kubectl-kyverno/output/color/color_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func TestInitColors(t *testing.T) { +func TestInit(t *testing.T) { tests := []struct { name string noColor bool @@ -15,13 +15,13 @@ func TestInitColors(t *testing.T) { }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - InitColors(tt.noColor) + Init(tt.noColor) }) } } func TestPolicy(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string policyNamespace string @@ -46,7 +46,7 @@ func TestPolicy(t *testing.T) { } func TestRule(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string ruleName string @@ -65,7 +65,7 @@ func TestRule(t *testing.T) { } func TestResource(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string kind string @@ -93,7 +93,7 @@ func TestResource(t *testing.T) { } func TestNotFound(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string want string @@ -110,7 +110,7 @@ func TestNotFound(t *testing.T) { } func TestResultPass(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string want string @@ -127,7 +127,7 @@ func TestResultPass(t *testing.T) { } func TestResultFail(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string want string @@ -144,7 +144,7 @@ func TestResultFail(t *testing.T) { } func TestResultWarn(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string want string @@ -161,7 +161,7 @@ func TestResultWarn(t *testing.T) { } func TestResultError(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string want string @@ -178,7 +178,7 @@ func TestResultError(t *testing.T) { } func TestResultSkip(t *testing.T) { - InitColors(true) + Init(true) tests := []struct { name string want string diff --git a/ext/output/color/color.go b/ext/output/color/color.go new file mode 100644 index 0000000000..46d7173422 --- /dev/null +++ b/ext/output/color/color.go @@ -0,0 +1,25 @@ +package color + +import ( + "github.com/fatih/color" +) + +var ( + BoldGreen *color.Color + BoldRed *color.Color + BoldYellow *color.Color + BoldFgCyan *color.Color +) + +func Init(noColor bool) { + toggleColor := func(c *color.Color) *color.Color { + if noColor { + c.DisableColor() + } + return c + } + BoldGreen = toggleColor(color.New(color.FgGreen).Add(color.Bold)) + BoldRed = toggleColor(color.New(color.FgRed).Add(color.Bold)) + BoldYellow = toggleColor(color.New(color.FgYellow).Add(color.Bold)) + BoldFgCyan = toggleColor(color.New(color.FgCyan).Add(color.Bold)) +} diff --git a/ext/output/color/color_test.go b/ext/output/color/color_test.go new file mode 100644 index 0000000000..33eb477b2d --- /dev/null +++ b/ext/output/color/color_test.go @@ -0,0 +1,21 @@ +package color + +import ( + "testing" +) + +func TestInit(t *testing.T) { + tests := []struct { + name string + noColor bool + }{{ + noColor: true, + }, { + noColor: false, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + Init(tt.noColor) + }) + } +} diff --git a/cmd/cli/kubectl-kyverno/output/pluralize/pluralize.go b/ext/output/pluralize/pluralize.go similarity index 100% rename from cmd/cli/kubectl-kyverno/output/pluralize/pluralize.go rename to ext/output/pluralize/pluralize.go diff --git a/cmd/cli/kubectl-kyverno/output/pluralize/pluralize_test.go b/ext/output/pluralize/pluralize_test.go similarity index 100% rename from cmd/cli/kubectl-kyverno/output/pluralize/pluralize_test.go rename to ext/output/pluralize/pluralize_test.go