Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
04fb45d
Simple includes implementation
marc-gr Mar 18, 2025
f778462
Second iteration
marc-gr Mar 18, 2025
f24f948
Use link files instead of includes file
marc-gr Mar 19, 2025
52132f5
Collect included pipelines in tests and benchmarks
marc-gr Mar 20, 2025
fc8f054
Create complete path if not exist
marc-gr Mar 25, 2025
268e23e
Add test package
marc-gr Mar 25, 2025
4b6dcb0
Add links commands
marc-gr Mar 25, 2025
0fb950c
Improve links commands
marc-gr Mar 26, 2025
d4f4c36
List also if not in package
marc-gr Mar 26, 2025
f044059
Reorganize code
marc-gr Mar 27, 2025
e9196c0
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Mar 27, 2025
44a1d1a
go mod tidy
marc-gr Mar 27, 2025
afc4b26
Only read entire file when copying
marc-gr Mar 28, 2025
53eead0
Add unit tests
marc-gr Mar 31, 2025
c2cdf7b
Always copy file on build
marc-gr Mar 31, 2025
eb8b05c
remove unused function
marc-gr Mar 31, 2025
0fdd2df
Use package in spec
marc-gr Apr 2, 2025
a4322e5
Use package spec
marc-gr Apr 2, 2025
663ec29
Update links usage
marc-gr Apr 10, 2025
c10bdee
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Apr 10, 2025
6c94355
replace package-spec
marc-gr Apr 10, 2025
ed5a641
Update readme
marc-gr Apr 10, 2025
3438ac4
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jun 23, 2025
f30f574
Remove go.mod replace
marc-gr Jun 23, 2025
6526118
Secure linked files: fix path traversal vulnerabilities and improve t…
marc-gr Jun 26, 2025
1e50f26
Improve linkedfiles API with convenience functions and structured res…
marc-gr Jun 26, 2025
efd9c9f
fix test cleanup
marc-gr Jun 26, 2025
3c56a23
Use spec 3.4.0 for test package
marc-gr Jun 26, 2025
ebf7c56
Fix shared folder placement
marc-gr Jun 26, 2025
1880bcd
Close root usage on cleanup
marc-gr Jun 26, 2025
e0e8754
close root
marc-gr Jun 26, 2025
b450f33
handle paths better for linksfs
marc-gr Jun 27, 2025
83478ac
Enhance LinksFS security and path handling
marc-gr Jun 27, 2025
c091ed0
lint
marc-gr Jun 27, 2025
0d70806
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jun 27, 2025
1fb6aa5
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jun 30, 2025
12a0bc1
Restore LinksFS support lost in upstream merge
marc-gr Jun 30, 2025
bd5b9b8
Use LinksFS as the source for operations
marc-gr Jul 8, 2025
7668010
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr Jul 8, 2025
d4d7634
make linksfs work with relative paths from root and wd
marc-gr Jul 8, 2025
2ab1703
check
marc-gr Jul 8, 2025
70e6bc2
remove err declaration
marc-gr Jul 9, 2025
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
Prev Previous commit
Next Next commit
Second iteration
  • Loading branch information
marc-gr committed Mar 18, 2025
commit f778462e701cea04ce93b0ebb370478c048a164a
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func buildCommandAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("updating included files failed: %w", err)
}
for _, i := range included {
cmd.Printf("%s file copied to: %s\n", filepath.Join(i.Package, i.From), i.To)
cmd.Printf("%s file copied to: %s\n", filepath.FromSlash(i.From), i.To)
}

targets, err := docs.UpdateReadmes(packageRoot)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/elastic/elastic-package

go 1.23.0
go 1.24.0

toolchain go1.23.1
toolchain go1.24.1

replace github.com/elastic/package-spec/v3 => github.com/marc-gr/package-spec/v3 v3.0.0-20250318101845-919190f8ff28
replace github.com/elastic/package-spec/v3 => github.com/marc-gr/package-spec/v3 v3.0.0-20250318133830-8c7c5b9aebde

require (
github.com/AlecAivazis/survey/v2 v2.3.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/marc-gr/package-spec/v3 v3.0.0-20250318101845-919190f8ff28 h1:abIqzB3NBb971NNZPR8cWa+zNItK4/Z5a76DzCIhwdg=
github.com/marc-gr/package-spec/v3 v3.0.0-20250318101845-919190f8ff28/go.mod h1:+q7JpjqBFnNVMmh9VAVfZdOxQ3EmdCD+KM8Cg6VhKgg=
github.com/marc-gr/package-spec/v3 v3.0.0-20250318133830-8c7c5b9aebde h1:CR67Va0mMnQHq16yp6f5kL3nvETSITzy3CujjvmFTsw=
github.com/marc-gr/package-spec/v3 v3.0.0-20250318133830-8c7c5b9aebde/go.mod h1:+q7JpjqBFnNVMmh9VAVfZdOxQ3EmdCD+KM8Cg6VhKgg=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand Down
70 changes: 39 additions & 31 deletions internal/includes/includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"

Expand All @@ -19,74 +20,87 @@ import (

// IncludesFileEntry contains a file reference to include.
type IncludesFileEntry struct {
Package string `config:"package"`
From string `config:"from"`
To string `config:"to"`
UpToDate bool `config:"-"`
Diff string `config:"-"`
Error error `config:"-"`
}

type IncludesFile []IncludesFileEntry
type IncludesFile struct {
Include []IncludesFileEntry `config:"include"`
}

// IncludeSharedFiles function collects any necessary files to include in the package.
func IncludeSharedFiles() (IncludesFile, error) {
func IncludeSharedFiles() ([]IncludesFileEntry, error) {
packageRoot, err := packages.MustFindPackageRoot()
if err != nil {
return nil, fmt.Errorf("package root not found: %w", err)
}

includesFile, err := readIncludes(packageRoot)
// scope any possible operation in the packages/ folder
dirRoot, err := os.OpenRoot(filepath.Join(packageRoot, ".."))
if err != nil {
return nil, fmt.Errorf("could not open root: %w", err)
}

includes, err := readIncludes(packageRoot)
if err != nil {
return nil, fmt.Errorf("could not read includes.yml: %w", err)
}

for _, f := range includesFile {
b, err := collectFile(packageRoot, f)
for _, f := range includes {
b, err := collectFile(dirRoot.FS().(fs.ReadFileFS), f)
if err != nil {
return nil, fmt.Errorf("could not collect file %q: %w", filepath.Join(f.Package, f.From), err)
return nil, fmt.Errorf("could not collect file %q: %w", filepath.FromSlash(f.From), err)
}
if err := writeFile(packageRoot, f.To, b); err != nil {
return nil, fmt.Errorf("could not write destination file %q: %w", filepath.Join(packageRoot, f.To), err)
return nil, fmt.Errorf("could not write destination file %q: %w", filepath.Join(packageRoot, filepath.FromSlash(f.To)), err)
}
}
return includesFile, nil
return includes, nil
}

// AreFilesUpToDate function checks if all the included files are up-to-date.
func AreFilesUpToDate() (IncludesFile, error) {
func AreFilesUpToDate() ([]IncludesFileEntry, error) {
packageRoot, err := packages.MustFindPackageRoot()
if err != nil {
return nil, fmt.Errorf("package root not found: %w", err)
}

includesFile, err := readIncludes(packageRoot)
includes, err := readIncludes(packageRoot)
if err != nil {
return nil, fmt.Errorf("could not read includes.yml: %w", err)
}

var outdated bool
for i := 0; i < len(includesFile); i++ {
f := includesFile[i]
for i := 0; i < len(includes); i++ {
f := includes[i]
uptodate, diff, err := isFileUpToDate(packageRoot, f)
if !uptodate || err != nil {
includesFile[i].UpToDate = uptodate
includesFile[i].Diff = diff
includesFile[i].Error = err
includes[i].UpToDate = uptodate
includes[i].Diff = diff
includes[i].Error = err
outdated = true
}
}

if outdated {
return includesFile, fmt.Errorf("files do not match")
return includes, fmt.Errorf("files do not match")
}
return includesFile, nil
return includes, nil
}

func isFileUpToDate(packageRoot string, includedFile IncludesFileEntry) (bool, string, error) {
logger.Debugf("Check if %s is up-to-date", includedFile.To)

newFile, err := collectFile(packageRoot, includedFile)
// scope any possible operation in the packages/ folder
dirRoot, err := os.OpenRoot(filepath.Join(packageRoot, ".."))
if err != nil {
return false, "", fmt.Errorf("could not open root: %w", err)
}

newFile, err := collectFile(dirRoot.FS().(fs.ReadFileFS), includedFile)
if err != nil {
return false, "", err
}
Expand All @@ -112,27 +126,21 @@ func isFileUpToDate(packageRoot string, includedFile IncludesFileEntry) (bool, s
return false, buf.String(), err
}

func collectFile(packageRoot string, includedFile IncludesFileEntry) ([]byte, error) {
var filePath string
if includedFile.Package != "" {
filePath = filepath.Join("..", includedFile.Package, includedFile.From)
} else {
filePath = filepath.Join(packageRoot, includedFile.From)
}
b, err := os.ReadFile(filePath)
func collectFile(root fs.ReadFileFS, includedFile IncludesFileEntry) ([]byte, error) {
b, err := root.ReadFile(filepath.FromSlash(includedFile.From))
if err != nil {
return nil, err
}
return b, nil
}

func writeFile(packageRoot, to string, b []byte) error {
filePath := filepath.Join(packageRoot, to)
filePath := filepath.Join(packageRoot, filepath.FromSlash(to))
return os.WriteFile(filePath, b, 0644)
}

func readIncludes(packageRoot string) (IncludesFile, error) {
includesPath := filepath.Join(packageRoot, "_dev", "shared", "includes.yml")
func readIncludes(packageRoot string) ([]IncludesFileEntry, error) {
includesPath := filepath.Join(packageRoot, "_dev", "build", "build.yml")

b, err := os.ReadFile(includesPath)
if err != nil && errors.Is(err, os.ErrNotExist) {
Expand All @@ -148,7 +156,7 @@ func readIncludes(packageRoot string) (IncludesFile, error) {
if err := cfg.Unpack(&includesFile); err != nil {
return nil, fmt.Errorf("could not parse includes config: %w", err)
}
return includesFile, nil
return includesFile.Include, nil
}

func readFile(packageRoot, filePath string) ([]byte, bool, error) {
Expand Down