-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from doitintl/feature/jsonNewLineWriterOption
ref: Printer Updates
- Loading branch information
Showing
9 changed files
with
147 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters