Skip to content

Commit

Permalink
refactor: extract file from GetFileNameText to support webhook title (d…
Browse files Browse the repository at this point in the history
…atreeio#685)

* refactor: extract sprintf file from GetFileNameText to support webhook title

* refactor: rename GetFileNameText

* fix: remove comment

* refactor: extract file from GetFileNameText to support webhook title

* refactor: move the new function into the printer

Co-authored-by: teselil <[email protected]>
Co-authored-by: myishay <[email protected]>
  • Loading branch information
3 people authored Jun 13, 2022
1 parent d496104 commit b75009d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/evaluation/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func parseInvalidYamlFilesToWarnings(invalidYamlFiles []*extractor.InvalidFile)

for _, invalidFile := range invalidYamlFiles {
warnings = append(warnings, printer.Warning{
Title: invalidFile.Path,
Title: printer.GetFileNameText(invalidFile.Path),
FailedRules: []printer.FailedRule{},
InvalidYamlInfo: printer.InvalidYamlInfo{
ValidationErrors: invalidFile.ValidationErrors,
Expand All @@ -198,7 +198,7 @@ func parseInvalidK8sFilesToWarnings(invalidK8sFiles []*extractor.InvalidFile, k8

for _, invalidFile := range invalidK8sFiles {
warnings = append(warnings, printer.Warning{
Title: invalidFile.Path,
Title: printer.GetFileNameText(invalidFile.Path),
FailedRules: []printer.FailedRule{},
InvalidK8sInfo: printer.InvalidK8sInfo{
ValidationErrors: invalidFile.ValidationErrors,
Expand Down Expand Up @@ -289,7 +289,7 @@ func parseToPrinterWarnings(results *EvaluationResults, invalidYamlFiles []*extr
relativePath, _ := filepath.Rel(pwd, filename)

if relativePath != "" {
title = relativePath
title = printer.GetFileNameText(relativePath)
}

warnings = append(warnings, printer.Warning{
Expand Down
10 changes: 7 additions & 3 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (p *Printer) getYamlSchemaResultsText(errorsResult []jsonschema.Detailed, e
func (p *Printer) GetWarningsText(warnings []Warning) string {
var sb strings.Builder
for _, warning := range warnings {
sb.WriteString(p.GetFileNameText(warning.Title))
sb.WriteString(p.GetTitleText(warning.Title))

if len(warning.InvalidYamlInfo.ValidationErrors) > 0 {
sb.WriteString(p.getYamlValidationWarningText(warning))
Expand Down Expand Up @@ -241,8 +241,12 @@ type EvaluationSummary struct {
PassedPolicyCheckCount int
}

func (p *Printer) GetFileNameText(title string) string {
return p.GetTextInColor(fmt.Sprintf(">> File: %s\n\n", title), p.Theme.Colors.Yellow)
func (p *Printer) GetTitleText(title string) string {
return p.GetTextInColor(title, p.Theme.Colors.Yellow)
}

func GetFileNameText(title string) string {
return fmt.Sprintf(">> File: %s\n\n", title)
}

func (p *Printer) GetEvaluationSummaryText(summary EvaluationSummary, k8sVersion string) string {
Expand Down
9 changes: 4 additions & 5 deletions pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (

func TestGetWarningsText(t *testing.T) {
printer := CreateNewPrinter()

warnings := []Warning{{
Title: "~/.datree/k8-demo.yaml",
Title: GetFileNameText("~/.datree/k8-demo.yaml"),
FailedRules: []FailedRule{
{
Name: "Caption",
Expand All @@ -23,17 +22,17 @@ func TestGetWarningsText(t *testing.T) {
},
},
{
Title: "/datree/datree/internal/fixtures/kube/yaml-validation-error.yaml\n",
Title: GetFileNameText("/datree/datree/internal/fixtures/kube/yaml-validation-error.yaml\n"),
FailedRules: []FailedRule{},
InvalidYamlInfo: InvalidYamlInfo{ValidationErrors: []error{fmt.Errorf("yaml validation error")}},
},
{
Title: "/datree/datree/internal/fixtures/kube/k8s-validation-error.yaml\n",
Title: GetFileNameText("/datree/datree/internal/fixtures/kube/k8s-validation-error.yaml\n"),
FailedRules: []FailedRule{},
InvalidK8sInfo: InvalidK8sInfo{ValidationErrors: []error{fmt.Errorf("K8S validation error")}, K8sVersion: "1.18.0"},
},
{
Title: "/datree/datree/internal/fixtures/kube/Chart.yaml\n",
Title: GetFileNameText("/datree/datree/internal/fixtures/kube/Chart.yaml\n"),
FailedRules: []FailedRule{},
InvalidK8sInfo: InvalidK8sInfo{ValidationErrors: []error{fmt.Errorf("K8S validation error")}, K8sVersion: "1.18.0"},
ExtraMessages: []ExtraMessage{{Text: "Are you trying to test a raw helm file? To run Datree with Helm - check out the helm plugin README:\nhttps://github.com/datreeio/helm-datree",
Expand Down

0 comments on commit b75009d

Please sign in to comment.