Skip to content

Commit

Permalink
Merge pull request #134 from doitintl/feature/jsonNewLineWriterOption
Browse files Browse the repository at this point in the history
ref: Printer Updates
  • Loading branch information
stepanstipl authored May 4, 2021
2 parents 5859a13 + 313bb60 commit e158834
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 50 deletions.
12 changes: 3 additions & 9 deletions cmd/kubent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubent/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 8 additions & 20 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,12 @@ 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"
)

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
Expand All @@ -44,7 +26,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")
Expand All @@ -59,6 +43,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)
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/config/zerologlevel.go
Original file line number Diff line number Diff line change
@@ -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"
}
24 changes: 13 additions & 11 deletions pkg/printer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 47 additions & 0 deletions pkg/printer/json_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
23 changes: 23 additions & 0 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 24 additions & 0 deletions pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
13 changes: 4 additions & 9 deletions pkg/printer/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e158834

Please sign in to comment.