Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
39 changes: 30 additions & 9 deletions internal/builder/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -181,24 +190,36 @@ 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{
PackageName: m.Name,
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) {
Expand Down