From b1c0f731666643bd1039166596f080093ce0762a Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Wed, 27 Sep 2023 19:08:28 +0200 Subject: [PATCH] Update package-spec and code accordingly --- go.mod | 2 +- go.sum | 4 +-- internal/validation/validation.go | 43 +++++++++++++++---------------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index c6e4f9198..4a44cf09a 100644 --- a/go.mod +++ b/go.mod @@ -177,4 +177,4 @@ require ( sigs.k8s.io/yaml v1.3.0 // indirect ) -replace github.com/elastic/package-spec/v2 => github.com/mrodm/package-spec/v2 v2.0.0-20230926171924-1b0b6af1b3eb +replace github.com/elastic/package-spec/v2 => github.com/mrodm/package-spec/v2 v2.0.0-20230927165325-f1f5449c2284 diff --git a/go.sum b/go.sum index 28787fe93..ba2096b86 100644 --- a/go.sum +++ b/go.sum @@ -305,8 +305,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0= github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= -github.com/mrodm/package-spec/v2 v2.0.0-20230926171924-1b0b6af1b3eb h1:ehFz+TxgJoSCVHMk2xa1Jjt6Q0BoG00Lhkey/MwdiqU= -github.com/mrodm/package-spec/v2 v2.0.0-20230926171924-1b0b6af1b3eb/go.mod h1:hKQ0du9JO310TiTP1UZkjngMAFr+dYr1MdVJcTvvC4Q= +github.com/mrodm/package-spec/v2 v2.0.0-20230927165325-f1f5449c2284 h1:hyQ8ICJkzsoJXNaMntPZq1jtpzlLEVDHEgOFiHyrQ28= +github.com/mrodm/package-spec/v2 v2.0.0-20230927165325-f1f5449c2284/go.mod h1:hKQ0du9JO310TiTP1UZkjngMAFr+dYr1MdVJcTvvC4Q= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= diff --git a/internal/validation/validation.go b/internal/validation/validation.go index 59547f81f..19cc8eaa3 100644 --- a/internal/validation/validation.go +++ b/internal/validation/validation.go @@ -6,19 +6,15 @@ package validation import ( "archive/zip" + "errors" "fmt" "io/fs" "os" "github.com/elastic/package-spec/v2/code/go/pkg/specerrors" - "github.com/elastic/package-spec/v2/code/go/pkg/specerrors/processors" "github.com/elastic/package-spec/v2/code/go/pkg/validator" - - "github.com/elastic/elastic-package/internal/logger" ) -const validationConfigPath = "validation.yml" - func ValidateFromPath(rootPath string) error { return validator.ValidateFromPath(rootPath) } @@ -34,11 +30,11 @@ func ValidateAndFilterFromPath(rootPath string) (error, error) { } fsys := os.DirFS(rootPath) - errors, skipped, err := filterErrors(allErrors, fsys, validationConfigPath) + result, err := filterErrors(allErrors, fsys) if err != nil { return err, nil } - return errors, skipped + return result.Processed, result.Removed } func ValidateAndFilterFromZip(packagePath string) (error, error) { @@ -58,11 +54,11 @@ func ValidateAndFilterFromZip(packagePath string) (error, error) { return fmt.Errorf("failed to extract filesystem from zip file (%s): %w", packagePath, err), nil } - errors, skipped, err := filterErrors(allErrors, fsZip, validationConfigPath) + result, err := filterErrors(allErrors, fsZip) if err != nil { return err, nil } - return errors, skipped + return result.Processed, result.Removed } func fsFromPackageZip(fsys fs.FS) (fs.FS, error) { @@ -81,28 +77,31 @@ func fsFromPackageZip(fsys fs.FS) (fs.FS, error) { return subDir, nil } -func filterErrors(allErrors error, fsys fs.FS, configPath string) (error, error, error) { +func filterErrors(allErrors error, fsys fs.FS) (specerrors.FilterResult, error) { errs, ok := allErrors.(specerrors.ValidationErrors) if !ok { - return allErrors, nil, nil + return specerrors.FilterResult{Processed: allErrors, Removed: nil}, nil } - _, err := fs.Stat(fsys, configPath) - if err != nil { - logger.Debugf("file not found: %s", configPath) - return allErrors, nil, nil + config, err := specerrors.LoadConfigFilter(fsys) + if err != nil && errors.Is(err, fs.ErrNotExist) { + return specerrors.FilterResult{Processed: allErrors, Removed: nil}, nil } - - config, err := processors.LoadConfigFilter(fsys, configPath) if err != nil { - return nil, nil, fmt.Errorf("failed to read config filter: %w", err) + return specerrors.FilterResult{Processed: allErrors, Removed: nil}, + fmt.Errorf("failed to read config filter: %w", err) + } + if config == nil { + return specerrors.FilterResult{Processed: allErrors, Removed: nil}, nil } - filter := processors.NewFilter(config) + filter := specerrors.NewFilter(config) - filteredErrors, skippedErrors, err := filter.Run(errs) + result, err := filter.Run(errs) if err != nil { - return nil, nil, fmt.Errorf("failed to filter errors: %w", err) + return specerrors.FilterResult{Processed: allErrors, Removed: nil}, + fmt.Errorf("failed to filter errors: %w", err) } - return filteredErrors, skippedErrors, nil + return result, nil + }