From 5f1ae005a32bd66e8f749749610be9c43b141355 Mon Sep 17 00:00:00 2001 From: david-doit-intl Date: Tue, 20 Apr 2021 14:26:52 +0100 Subject: [PATCH 1/2] ref: Printer type update Signed-off-by: david-doit-intl --- cmd/kubent/main.go | 12 +++------- cmd/kubent/main_test.go | 2 +- pkg/config/config.go | 9 ++++++- pkg/printer/json.go | 24 ++++++++++--------- pkg/printer/json_test.go | 47 +++++++++++++++++++++++++++++++++++++ pkg/printer/printer.go | 23 ++++++++++++++++++ pkg/printer/printer_test.go | 24 +++++++++++++++++++ pkg/printer/text.go | 13 ++++------ 8 files changed, 123 insertions(+), 31 deletions(-) create mode 100644 pkg/printer/json_test.go create mode 100644 pkg/printer/printer_test.go diff --git a/cmd/kubent/main.go b/cmd/kubent/main.go index 46e9e6fa..c7101913 100644 --- a/cmd/kubent/main.go +++ b/cmd/kubent/main.go @@ -119,18 +119,12 @@ func main() { log.Fatal().Err(err).Str("name", "Rego").Msg("Failed to evaluate input") } - var prnt printer.Printer - switch config.Output { - case "json": - prnt, err = printer.NewJSONPrinter(&printer.JSONOpts{}) - default: - prnt, err = printer.NewTextPrinter(&printer.TextOpts{}) - } + printer, err := printer.NewPrinter(config.Output) if err != nil { - log.Fatal().Err(err).Str("name", config.Output).Msg("Failed to initialize output printer") + log.Fatal().Err(err).Msg("Failed to create printer") } - err = prnt.Print(results) + err = printer.Print(results) if err != nil { log.Fatal().Err(err).Msg("Failed to print results") } diff --git a/cmd/kubent/main_test.go b/cmd/kubent/main_test.go index 98b54197..b4bcb4d7 100644 --- a/cmd/kubent/main_test.go +++ b/cmd/kubent/main_test.go @@ -20,7 +20,7 @@ func TestInitCollectors(t *testing.T) { Helm3: false, Kubeconfig: "test", LogLevel: config.ZeroLogLevel(zerolog.ErrorLevel), - Output: "test", + Output: "text", } collectors := initCollectors(&testConfig) diff --git a/pkg/config/config.go b/pkg/config/config.go index f19d4683..992ef2c9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -7,6 +7,7 @@ import ( "strings" "unicode" + "github.com/doitintl/kube-no-trouble/pkg/printer" "github.com/rs/zerolog" flag "github.com/spf13/pflag" "k8s.io/client-go/util/homedir" @@ -44,7 +45,9 @@ type Config struct { } func NewFromFlags() (*Config, error) { - config := Config{LogLevel: ZeroLogLevel(zerolog.InfoLevel)} + config := Config{ + LogLevel: ZeroLogLevel(zerolog.InfoLevel), + } home := homedir.HomeDir() flag.StringSliceVarP(&config.AdditionalKinds, "additional-kind", "a", []string{}, "additional kinds of resources to report in Kind.version.group.com format") @@ -59,6 +62,10 @@ func NewFromFlags() (*Config, error) { flag.Parse() + if _, err := printer.ParsePrinter(config.Output); err != nil { + return nil, fmt.Errorf("failed to validate argument output: %w", err) + } + if err := validateAdditionalResources(config.AdditionalKinds); err != nil { return nil, fmt.Errorf("failed to validate arguments: %w", err) } diff --git a/pkg/printer/json.go b/pkg/printer/json.go index 6e7070fe..a17881f9 100644 --- a/pkg/printer/json.go +++ b/pkg/printer/json.go @@ -8,29 +8,31 @@ import ( "github.com/doitintl/kube-no-trouble/pkg/judge" ) -type JSONPrinter struct { +type jsonPrinter struct { } -type JSONOpts struct { +func newJSONPrinter() Printer { + return &jsonPrinter{} } -func NewJSONPrinter(opts *JSONOpts) (*JSONPrinter, error) { - printer := &JSONPrinter{} - - return printer, nil -} - -func (c *JSONPrinter) Print(results []judge.Result) error { - +func (c *jsonPrinter) populateOutput(results []judge.Result) (string, error) { buffer := new(bytes.Buffer) encoder := json.NewEncoder(buffer) encoder.SetIndent("", "\t") err := encoder.Encode(results) + if err != nil { + return "", err + } + return buffer.String(), nil +} + +func (c *jsonPrinter) Print(results []judge.Result) error { + output, err := c.populateOutput(results) if err != nil { return err } - fmt.Printf("%s", buffer.String()) + fmt.Printf("%s", output) return nil } diff --git a/pkg/printer/json_test.go b/pkg/printer/json_test.go new file mode 100644 index 00000000..2867dbbb --- /dev/null +++ b/pkg/printer/json_test.go @@ -0,0 +1,47 @@ +package printer + +import ( + "encoding/json" + "testing" + + "github.com/doitintl/kube-no-trouble/pkg/judge" +) + +var findingsJsonTesting []judge.Result = []judge.Result{ + { + Name: "testName1", + Kind: "testKind1", + Namespace: "testNamespace1", + ApiVersion: "v1", + RuleSet: "testRuleset1", + ReplaceWith: "testReplaceWith1", + Since: "testSince1", + }, + { + Name: "testName2", + Kind: "testKind2", + Namespace: "testNamespace2", + ApiVersion: "v1", + RuleSet: "testRuleset2", + ReplaceWith: "testReplaceWith2", + Since: "testSince2", + }, +} + +func TestJsonPopulateOutput(t *testing.T) { + printer := &jsonPrinter{} + output, err := printer.populateOutput(findingsJsonTesting) + + if err != nil { + t.Fatal(err) + } + + var j []judge.Result + err = json.Unmarshal([]byte(output), &j) + if err != nil { + t.Fatal(err) + } + if len(j) != 2 { + t.Error("wrong number of results") + } +} diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 00bce0bf..38372443 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -1,9 +1,32 @@ package printer import ( + "fmt" + "github.com/doitintl/kube-no-trouble/pkg/judge" ) +var printers = map[string]func() Printer{ + "json": newJSONPrinter, + "text": newTextPrinter, +} + type Printer interface { Print([]judge.Result) error } + +func NewPrinter(choice string) (Printer, error) { + printer, err := ParsePrinter(choice) + if err != nil { + return nil, err + } + return printer(), nil +} + +func ParsePrinter(choice string) (func() Printer, error) { + printer, exists := printers[choice] + if !exists { + return nil, fmt.Errorf("unknown printer type %s", choice) + } + return printer, nil +} diff --git a/pkg/printer/printer_test.go b/pkg/printer/printer_test.go new file mode 100644 index 00000000..28fff150 --- /dev/null +++ b/pkg/printer/printer_test.go @@ -0,0 +1,24 @@ +package printer + +import ( + "testing" +) + +func TestParsePrinters(t *testing.T) { + for k, v := range printers { + printer, err := NewPrinter(k) + if err != nil { + t.Fatal(err) + } + if printer != v() { + t.Fatal("Should be a valid printer") + } + } +} + +func TestInvalidStringForParsePrinters(t *testing.T) { + _, err := NewPrinter("BAD") + if err == nil { + t.Fatal(err) + } +} diff --git a/pkg/printer/text.go b/pkg/printer/text.go index dba301e6..65b7486f 100644 --- a/pkg/printer/text.go +++ b/pkg/printer/text.go @@ -10,19 +10,14 @@ import ( "github.com/doitintl/kube-no-trouble/pkg/judge" ) -type TextPrinter struct { +type textPrinter struct { } -type TextOpts struct { +func newTextPrinter() Printer { + return &textPrinter{} } -func NewTextPrinter(opts *TextOpts) (*TextPrinter, error) { - printer := &TextPrinter{} - - return printer, nil -} - -func (c *TextPrinter) Print(results []judge.Result) error { +func (c *textPrinter) Print(results []judge.Result) error { sort.Slice(results, func(i, j int) bool { return results[i].Name < results[j].Name From 313bb606a4ae7e4bba0d7baa983bb4317594352c Mon Sep 17 00:00:00 2001 From: david-doit-intl Date: Tue, 4 May 2021 14:52:40 +0100 Subject: [PATCH 2/2] ref: Zerologlevel gets own file Signed-off-by: david-doit-intl --- pkg/config/config.go | 19 ------------------- pkg/config/zerologlevel.go | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 pkg/config/zerologlevel.go diff --git a/pkg/config/config.go b/pkg/config/config.go index 992ef2c9..f145ceed 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,25 +13,6 @@ import ( "k8s.io/client-go/util/homedir" ) -type ZeroLogLevel int8 - -func (l ZeroLogLevel) String() string { - return zerolog.Level(l).String() -} - -func (l *ZeroLogLevel) Set(s string) error { - level, err := zerolog.ParseLevel(s) - if err != nil { - return err - } - *l = ZeroLogLevel(level) - return nil -} - -func (l ZeroLogLevel) Type() string { - return "string" -} - type Config struct { AdditionalKinds []string Cluster bool diff --git a/pkg/config/zerologlevel.go b/pkg/config/zerologlevel.go new file mode 100644 index 00000000..ae06d379 --- /dev/null +++ b/pkg/config/zerologlevel.go @@ -0,0 +1,24 @@ +package config + +import ( + "github.com/rs/zerolog" +) + +type ZeroLogLevel int8 + +func (l ZeroLogLevel) String() string { + return zerolog.Level(l).String() +} + +func (l *ZeroLogLevel) Set(s string) error { + level, err := zerolog.ParseLevel(s) + if err != nil { + return err + } + *l = ZeroLogLevel(level) + return nil +} + +func (l ZeroLogLevel) Type() string { + return "string" +}