diff --git a/cmd/lint.go b/cmd/lint.go index 648c188960..80f2a8c0e8 100644 --- a/cmd/lint.go +++ b/cmd/lint.go @@ -24,7 +24,17 @@ func setupLintCommand() *cobraext.Command { Use: "lint", Short: "Lint the package", Long: lintLongDescription, - RunE: lintCommandAction, + RunE: func(cmd *cobra.Command, args []string) error { + err := cobraext.ComposeCommandActions(cmd, args, + lintCommandAction, + validateSourceCommandAction, + ) + if err != nil { + return err + } + cmd.Println("Done") + return nil + }, } return cobraext.NewCommand(cmd, cobraext.ContextPackage) @@ -33,14 +43,6 @@ func setupLintCommand() *cobraext.Command { func lintCommandAction(cmd *cobra.Command, args []string) error { cmd.Println("Lint the package") - packageRootPath, found, err := packages.FindPackageRoot() - if !found { - return errors.New("package root not found") - } - if err != nil { - return errors.Wrap(err, "locating package root failed") - } - readmeFiles, err := docs.AreReadmesUpToDate() if err != nil { for _, f := range readmeFiles { @@ -53,12 +55,21 @@ func lintCommandAction(cmd *cobra.Command, args []string) error { } return errors.Wrap(err, "checking readme files are up-to-date failed") } + return nil +} +func validateSourceCommandAction(cmd *cobra.Command, args []string) error { + packageRootPath, found, err := packages.FindPackageRoot() + if !found { + return errors.New("package root not found") + } + if err != nil { + return errors.Wrap(err, "locating package root failed") + } err = validator.ValidateFromPath(packageRootPath) if err != nil { return errors.Wrap(err, "linting package failed") } - cmd.Println("Done") return nil } diff --git a/internal/builder/packages.go b/internal/builder/packages.go index 231df37660..e936d95a6c 100644 --- a/internal/builder/packages.go +++ b/internal/builder/packages.go @@ -9,11 +9,12 @@ import ( "os" "path/filepath" + "github.com/elastic/package-spec/code/go/pkg/validator" + "github.com/pkg/errors" + "github.com/elastic/elastic-package/internal/files" "github.com/elastic/elastic-package/internal/logger" "github.com/elastic/elastic-package/internal/packages" - - "github.com/pkg/errors" ) const buildIntegrationsFolder = "integrations" @@ -166,10 +167,18 @@ func BuildPackage(options BuildOptions) (string, error) { return "", errors.Wrap(err, "resolving external fields failed") } - if !options.CreateZip { - return destinationDir, nil + if options.CreateZip { + return buildZippedPackage(options, destinationDir) } + err = validator.ValidateFromPath(destinationDir) + if err != nil { + return "", errors.Wrap(err, "invalid content found in built package") + } + return destinationDir, nil +} + +func buildZippedPackage(options BuildOptions, destinationDir string) (string, error) { logger.Debug("Build zipped package") zippedPackagePath, err := buildPackagesZipPath(options.PackageRoot) if err != nil { @@ -181,14 +190,26 @@ func BuildPackage(options BuildOptions) (string, error) { return "", errors.Wrapf(err, "can't compress the built package (compressed file path: %s)", zippedPackagePath) } - if !options.SignPackage { - return zippedPackagePath, nil + err = validator.ValidateFromZip(zippedPackagePath) + if err != nil { + return "", errors.Wrapf(err, "invalid content found in built zip package") } + if options.SignPackage { + err := signZippedPackage(options, zippedPackagePath) + if err != nil { + return "", err + } + } + + return zippedPackagePath, nil +} + +func signZippedPackage(options BuildOptions, zippedPackagePath string) error { logger.Debug("Sign the package") m, err := packages.ReadPackageManifestFromPackageRoot(options.PackageRoot) if err != nil { - return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", options.PackageRoot) + return errors.Wrapf(err, "reading package manifest failed (path: %s)", options.PackageRoot) } err = files.Sign(zippedPackagePath, files.SignOptions{ @@ -196,9 +217,9 @@ func BuildPackage(options BuildOptions) (string, error) { PackageVersion: m.Version, }) if err != nil { - return "", errors.Wrapf(err, "can't sign the zipped package (path: %s)", zippedPackagePath) + return errors.Wrapf(err, "can't sign the zipped package (path: %s)", zippedPackagePath) } - return zippedPackagePath, nil + return nil } func createBuildDirectory(dirs ...string) (string, error) {