Skip to content

Commit

Permalink
better output formatting
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <[email protected]>
  • Loading branch information
everettraven committed Nov 1, 2024
1 parent d51228c commit bd17ed2
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 73 deletions.
4 changes: 3 additions & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"io"
"log"
"net/url"
Expand Down Expand Up @@ -90,7 +91,8 @@ Example use cases:

err = validator.Validate(oldCrd, newCrd)
if err != nil {
log.Fatalf("comparing old and new CustomResourceDefinitions: %v", err)
baseErr := errors.New("comparing old and new CustomResourceDefinitions")
log.Fatal(errors.Join(baseErr, err))
}
},
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/validations/property/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package property
import (
"bytes"
"fmt"

"github.com/everettraven/crd-diff/pkg/validations/results"
)

type Default struct{}

func (d *Default) Name() string {
return "Default"
return "Default"
}

func (d *Default) Validate(diff Diff) (bool, error) {
func (d *Default) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -31,5 +33,8 @@ func (d *Default) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("default value changed from %q to %q", string(diff.Old().Default.Raw), string(diff.New().Default.Raw))
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}
8 changes: 6 additions & 2 deletions pkg/validations/property/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package property
import (
"fmt"

"github.com/everettraven/crd-diff/pkg/validations/results"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
Expand All @@ -13,7 +14,7 @@ func (e *Enum) Name() string {
return "Enum"
}

func (e *Enum) Validate(diff Diff) (bool, error) {
func (e *Enum) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand Down Expand Up @@ -41,5 +42,8 @@ func (e *Enum) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("enums %v removed from the set of previously allowed values", diffEnums.UnsortedList())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}
30 changes: 22 additions & 8 deletions pkg/validations/property/max.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package property
import (
"cmp"
"fmt"

"github.com/everettraven/crd-diff/pkg/validations/results"
)

func maxVerification[T cmp.Ordered](older *T, newer *T) error {
Expand All @@ -22,7 +24,7 @@ func (m *Maximum) Name() string {
return "Maximum"
}

func (m *Maximum) Validate(diff Diff) (bool, error) {
func (m *Maximum) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -36,7 +38,10 @@ func (m *Maximum) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("maximum: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}

type MaxItems struct{}
Expand All @@ -45,7 +50,7 @@ func (m *MaxItems) Name() string {
return "MaxItems"
}

func (m *MaxItems) Validate(diff Diff) (bool, error) {
func (m *MaxItems) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -59,7 +64,10 @@ func (m *MaxItems) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("maxItems: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}

type MaxLength struct{}
Expand All @@ -68,7 +76,7 @@ func (m *MaxLength) Name() string {
return "MaxLength"
}

func (m *MaxLength) Validate(diff Diff) (bool, error) {
func (m *MaxLength) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -82,7 +90,10 @@ func (m *MaxLength) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("maxLength: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}

type MaxProperties struct{}
Expand All @@ -91,7 +102,7 @@ func (m *MaxProperties) Name() string {
return "MaxProperties"
}

func (m *MaxProperties) Validate(diff Diff) (bool, error) {
func (m *MaxProperties) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -105,5 +116,8 @@ func (m *MaxProperties) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("maxProperties: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}
30 changes: 22 additions & 8 deletions pkg/validations/property/min.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package property
import (
"cmp"
"fmt"

"github.com/everettraven/crd-diff/pkg/validations/results"
)

func minVerification[T cmp.Ordered](older *T, newer *T) error {
Expand All @@ -22,7 +24,7 @@ func (m *Minimum) Name() string {
return "Minimum"
}

func (m *Minimum) Validate(diff Diff) (bool, error) {
func (m *Minimum) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -36,7 +38,10 @@ func (m *Minimum) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("minimum: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}

type MinItems struct{}
Expand All @@ -45,7 +50,7 @@ func (m *MinItems) Name() string {
return "MinItems"
}

func (m *MinItems) Validate(diff Diff) (bool, error) {
func (m *MinItems) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -59,7 +64,10 @@ func (m *MinItems) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("minItems: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}

type MinLength struct{}
Expand All @@ -68,7 +76,7 @@ func (m *MinLength) Name() string {
return "MinLength"
}

func (m *MinLength) Validate(diff Diff) (bool, error) {
func (m *MinLength) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -82,7 +90,10 @@ func (m *MinLength) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("minLength: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}

type MinProperties struct{}
Expand All @@ -91,7 +102,7 @@ func (m *MinProperties) Name() string {
return "MinProperties"
}

func (m *MinProperties) Validate(diff Diff) (bool, error) {
func (m *MinProperties) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -105,5 +116,8 @@ func (m *MinProperties) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("minProperties: %s", err.Error())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}
3 changes: 2 additions & 1 deletion pkg/validations/property/property.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package property

import (
"github.com/everettraven/crd-diff/pkg/validations/results"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

Expand Down Expand Up @@ -30,6 +31,6 @@ func (pd *diff) New() *apiextensionsv1.JSONSchemaProps {
}

type Validation interface {
Validate(Diff) (bool, error)
Validate(Diff) (bool, *results.Result)
Name() string
}
8 changes: 6 additions & 2 deletions pkg/validations/property/required.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package property
import (
"fmt"

"github.com/everettraven/crd-diff/pkg/validations/results"
"k8s.io/apimachinery/pkg/util/sets"
)

Expand All @@ -12,7 +13,7 @@ func (r *Required) Name() string {
return "Required"
}

func (r *Required) Validate(diff Diff) (bool, error) {
func (r *Required) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -30,5 +31,8 @@ func (r *Required) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("new required fields %v added", diffRequired.UnsortedList())
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}
9 changes: 7 additions & 2 deletions pkg/validations/property/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package property

import (
"fmt"

"github.com/everettraven/crd-diff/pkg/validations/results"
)

type Type struct{}
Expand All @@ -10,7 +12,7 @@ func (t *Type) Name() string {
return "Type"
}

func (t *Type) Validate(diff Diff) (bool, error) {
func (t *Type) Validate(diff Diff) (bool, *results.Result) {
reset := func(diff Diff) Diff {
oldProperty := diff.Old()
newProperty := diff.New()
Expand All @@ -24,5 +26,8 @@ func (t *Type) Validate(diff Diff) (bool, error) {
err = fmt.Errorf("type changed from %q to %q", diff.Old().Type, diff.New().Type)
}

return IsHandled(diff, reset), err
return IsHandled(diff, reset), &results.Result{
Error: err,
Subresults: []*results.Result{},
}
}
39 changes: 39 additions & 0 deletions pkg/validations/results/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package results

import (
"errors"
"fmt"
"strings"
)

type Result struct {
Error error
Subresults []*Result
}

func ErrorFromResult(res *Result, depth int) error {
if res == nil {
return nil
}

if res.Error == nil && len(res.Subresults) == 0 {
return nil
}

var out strings.Builder
nestedErrors := []error{}
for _, subresult := range res.Subresults {
nestedErrors = append(nestedErrors, ErrorFromResult(subresult, depth+1))
}

if res.Error != nil {
out.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", depth*2), res.Error.Error()))
}

for _, nestedErr := range nestedErrors {
if nestedErr != nil {
out.WriteString(nestedErr.Error())
}
}
return errors.New(out.String())
}
Loading

0 comments on commit bd17ed2

Please sign in to comment.