From 18b38ff3dcb37c42b7a4c7faafe3f05f89b4bfd8 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 7 Jul 2022 17:35:54 +0200 Subject: [PATCH 1/4] Use .zip packages by default in Elastic stack --- cmd/build.go | 2 +- internal/files/copy.go | 25 ++++++++++++++++--------- internal/stack/boot.go | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index d6e1df9a1f..21220d77db 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -35,7 +35,7 @@ func setupBuildCommand() *cobraext.Command { Long: buildLongDescription, RunE: buildCommandAction, } - cmd.Flags().Bool(cobraext.BuildZipFlagName, false, cobraext.BuildZipFlagDescription) + cmd.Flags().Bool(cobraext.BuildZipFlagName, true, cobraext.BuildZipFlagDescription) cmd.Flags().Bool(cobraext.SignPackageFlagName, false, cobraext.SignPackageFlagDescription) return cobraext.NewCommand(cmd, cobraext.ContextPackage) } diff --git a/internal/files/copy.go b/internal/files/copy.go index 3cb5249f25..c083495225 100644 --- a/internal/files/copy.go +++ b/internal/files/copy.go @@ -11,17 +11,18 @@ import ( "github.com/magefile/mage/sh" ) -// CopyAll method copies files from the source to the destination. +// CopyAll method copies files from the source to the destination skipping empty directories. func CopyAll(sourcePath, destinationPath string) error { - return copy(sourcePath, destinationPath, []string{}) + return CopyWithSkipped(sourcePath, destinationPath, []string{}) } -// CopyWithoutDev method copies files from the source to the destination, but skips _dev directories. +// CopyWithoutDev method copies files from the source to the destination, but skips _dev directories and empty folders. func CopyWithoutDev(sourcePath, destinationPath string) error { - return copy(sourcePath, destinationPath, []string{"_dev"}) + return CopyWithSkipped(sourcePath, destinationPath, []string{"_dev"}) } -func copy(sourcePath, destinationPath string, skippedDirs []string) error { +// CopyWithSkipped method copies files from the source to the destination, but skips selected directories and empty folders. +func CopyWithSkipped(sourcePath, destinationPath string, skippedDirs []string) error { return filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -36,12 +37,17 @@ func copy(sourcePath, destinationPath string, skippedDirs []string) error { return nil } - if info.IsDir() && shouldDirectoryBeSkipped(info.Name(), skippedDirs) { + if info.IsDir() && shouldDirectoryBeSkipped(relativePath, skippedDirs) { return filepath.SkipDir } if info.IsDir() { - return os.MkdirAll(filepath.Join(destinationPath, relativePath), 0755) + return nil // don't create empty directories inside packages, if the directory is empty, skip it. + } + + err = os.MkdirAll(filepath.Join(destinationPath, filepath.Dir(relativePath)), 0755) + if err != nil { + return err } return sh.Copy( @@ -50,9 +56,10 @@ func copy(sourcePath, destinationPath string, skippedDirs []string) error { }) } -func shouldDirectoryBeSkipped(name string, skippedDirs []string) bool { +// shouldDirectoryBeSkipped function checks if absolute path or last element should be skipped. +func shouldDirectoryBeSkipped(path string, skippedDirs []string) bool { for _, d := range skippedDirs { - if name == d { + if path == d || filepath.Base(path) == d { return true } } diff --git a/internal/stack/boot.go b/internal/stack/boot.go index 052318fa29..7162538e51 100644 --- a/internal/stack/boot.go +++ b/internal/stack/boot.go @@ -6,6 +6,9 @@ package stack import ( "fmt" + "os" + "path/filepath" + "strings" "github.com/pkg/errors" @@ -37,7 +40,7 @@ func BootUp(options Options) error { if found { fmt.Printf("Custom build packages directory found: %s\n", buildPackagesPath) - err = files.CopyAll(buildPackagesPath, stackPackagesDir.PackagesDir()) + err = copyUniquePackages(buildPackagesPath, stackPackagesDir.PackagesDir()) if err != nil { return errors.Wrap(err, "copying package contents failed") } @@ -71,3 +74,32 @@ func TearDown(options Options) error { } return nil } + +func copyUniquePackages(sourcePath, destinationPath string) error { + var skippedDirs []string + + dirEntries, err := os.ReadDir(sourcePath) + if err != nil { + return errors.Wrapf(err, "can't read source dir (sourcePath: %s)", sourcePath) + } + for _, entry := range dirEntries { + if entry.IsDir() { + continue + } + + if !strings.HasSuffix(entry.Name(), ".zip") { + continue + } + + name, versionZip, found := strings.Cut(entry.Name(), "-") + if !found { + continue + } + version, _, found := strings.Cut(versionZip, ".zip") + if !found { + continue + } + skippedDirs = append(skippedDirs, filepath.Join(name, version)) + } + return files.CopyWithSkipped(sourcePath, destinationPath, skippedDirs) +} From 035a7c53d41d4fbc181afadb0d9f0c0a972ea9dd Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 7 Jul 2022 18:02:43 +0200 Subject: [PATCH 2/4] Bump up Go version --- .go-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.go-version b/.go-version index 622f042fdc..b9fb27ab4f 100644 --- a/.go-version +++ b/.go-version @@ -1 +1 @@ -1.17.6 +1.18.3 From d6d5dc672fff6b1df334ca4f53932b88c392b460 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 7 Jul 2022 18:03:17 +0200 Subject: [PATCH 3/4] Revert "Bump up Go version" This reverts commit 035a7c53d41d4fbc181afadb0d9f0c0a972ea9dd. --- .go-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.go-version b/.go-version index b9fb27ab4f..622f042fdc 100644 --- a/.go-version +++ b/.go-version @@ -1 +1 @@ -1.18.3 +1.17.6 From 5b28fecf7be69c45b8fad6105d343a4ccf06df52 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 7 Jul 2022 18:05:44 +0200 Subject: [PATCH 4/4] stringsCut --- internal/stack/boot.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/stack/boot.go b/internal/stack/boot.go index 7162538e51..61b1426bb3 100644 --- a/internal/stack/boot.go +++ b/internal/stack/boot.go @@ -91,11 +91,11 @@ func copyUniquePackages(sourcePath, destinationPath string) error { continue } - name, versionZip, found := strings.Cut(entry.Name(), "-") + name, versionZip, found := stringsCut(entry.Name(), "-") if !found { continue } - version, _, found := strings.Cut(versionZip, ".zip") + version, _, found := stringsCut(versionZip, ".zip") if !found { continue } @@ -103,3 +103,13 @@ func copyUniquePackages(sourcePath, destinationPath string) error { } return files.CopyWithSkipped(sourcePath, destinationPath, skippedDirs) } + +// stringsCut has been imported from Go source code. +// Link: https://github.com/golang/go/blob/master/src/strings/strings.go#L1187 +// Once we bump up Go dependency, this will be replaced with runtime function. +func stringsCut(s, sep string) (before, after string, found bool) { + if i := strings.Index(s, sep); i >= 0 { + return s[:i], s[i+len(sep):], true + } + return s, "", false +}