-
Notifications
You must be signed in to change notification settings - Fork 124
Simple includes implementation #2483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marc-gr
wants to merge
22
commits into
elastic:main
Choose a base branch
from
marc-gr:feat/includes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
04fb45d
Simple includes implementation
marc-gr f778462
Second iteration
marc-gr f24f948
Use link files instead of includes file
marc-gr 52132f5
Collect included pipelines in tests and benchmarks
marc-gr fc8f054
Create complete path if not exist
marc-gr 268e23e
Add test package
marc-gr 4b6dcb0
Add links commands
marc-gr 0fb950c
Improve links commands
marc-gr d4f4c36
List also if not in package
marc-gr f044059
Reorganize code
marc-gr e9196c0
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr 44a1d1a
go mod tidy
marc-gr afc4b26
Only read entire file when copying
marc-gr 53eead0
Add unit tests
marc-gr c2cdf7b
Always copy file on build
marc-gr eb8b05c
remove unused function
marc-gr 0fdd2df
Use package in spec
marc-gr a4322e5
Use package spec
marc-gr 663ec29
Update links usage
marc-gr c10bdee
Merge remote-tracking branch 'upstream/main' into feat/includes
marc-gr 6c94355
replace package-spec
marc-gr ed5a641
Update readme
marc-gr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/elastic/elastic-package/internal/cobraext" | ||
"github.com/elastic/elastic-package/internal/files" | ||
) | ||
|
||
const ( | ||
linksLongDescription = `Use this command to manage linked files in the repository.` | ||
linksCheckLongDescription = `Use this command to check if linked files references inside the current directory are up to date.` | ||
linksUpdateLongDescription = `Use this command to update all linked files references inside the current directory.` | ||
linksListLongDescription = `Use this command to list all packages that have linked file references that include the current directory.` | ||
) | ||
|
||
func setupLinksCommand() *cobraext.Command { | ||
cmd := &cobra.Command{ | ||
Use: "links", | ||
Short: "Manage linked files", | ||
Long: linksLongDescription, | ||
RunE: func(parent *cobra.Command, args []string) error { | ||
return cobraext.ComposeCommandsParentContext(parent, args, parent.Commands()...) | ||
}, | ||
} | ||
|
||
cmd.AddCommand(getLinksCheckCommand()) | ||
cmd.AddCommand(getLinksUpdateCommand()) | ||
cmd.AddCommand(getLinksListCommand()) | ||
|
||
return cobraext.NewCommand(cmd, cobraext.ContextGlobal) | ||
} | ||
|
||
func getLinksCheckCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "check", | ||
Short: "Check for linked files changes", | ||
Long: linksCheckLongDescription, | ||
Args: cobra.NoArgs, | ||
RunE: linksCheckCommandAction, | ||
} | ||
return cmd | ||
} | ||
|
||
func linksCheckCommandAction(cmd *cobra.Command, args []string) error { | ||
cmd.Printf("Check for linked files changes\n") | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("reading current working directory failed: %w", err) | ||
} | ||
|
||
linkedFiles, err := files.AreLinkedFilesUpToDate(pwd) | ||
if err != nil { | ||
return fmt.Errorf("checking linked files are up-to-date failed: %w", err) | ||
} | ||
for _, f := range linkedFiles { | ||
if !f.UpToDate { | ||
cmd.Printf("%s is outdated.\n", filepath.Join(f.WorkDir, f.LinkFilePath)) | ||
} | ||
} | ||
if len(linkedFiles) > 0 { | ||
return fmt.Errorf("linked files are outdated") | ||
} | ||
return nil | ||
} | ||
|
||
func getLinksUpdateCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update", | ||
Short: "Update linked files checksums if needed.", | ||
Long: linksUpdateLongDescription, | ||
Args: cobra.NoArgs, | ||
RunE: linksUpdateCommandAction, | ||
} | ||
return cmd | ||
} | ||
|
||
func linksUpdateCommandAction(cmd *cobra.Command, args []string) error { | ||
cmd.Printf("Update linked files checksums if needed.\n") | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("reading current working directory failed: %w", err) | ||
} | ||
|
||
updatedLinks, err := files.UpdateLinkedFilesChecksums(pwd) | ||
if err != nil { | ||
return fmt.Errorf("updating linked files checksums failed: %w", err) | ||
} | ||
|
||
for _, l := range updatedLinks { | ||
cmd.Printf("%s was updated.\n", l.LinkFilePath) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getLinksListCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List packages linking files from this path", | ||
Long: linksListLongDescription, | ||
Args: cobra.NoArgs, | ||
RunE: linksListCommandAction, | ||
} | ||
cmd.Flags().BoolP(cobraext.PackagesFlagName, "", false, cobraext.PackagesFlagDescription) | ||
return cmd | ||
} | ||
|
||
func linksListCommandAction(cmd *cobra.Command, args []string) error { | ||
onlyPackages, err := cmd.Flags().GetBool(cobraext.PackagesFlagName) | ||
if err != nil { | ||
return cobraext.FlagParsingError(err, cobraext.PackagesFlagName) | ||
} | ||
|
||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("reading current working directory failed: %w", err) | ||
} | ||
|
||
byPackage, err := files.LinkedFilesByPackageFrom(pwd) | ||
if err != nil { | ||
return fmt.Errorf("listing linked packages failed: %w", err) | ||
} | ||
|
||
for i := range byPackage { | ||
for p, links := range byPackage[i] { | ||
if onlyPackages { | ||
cmd.Println(p) | ||
continue | ||
} | ||
for _, l := range links { | ||
cmd.Println(l) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should also add documentation on this file about the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# HOWTO: Use links to reuse common files. | ||
|
||
## Introduction | ||
|
||
Many packages have files that are equal between them. This is more common in pipelines, | ||
input configurations, and field definitions. | ||
|
||
In order to help developers, there is the ability to define links, so a file that might be reused needs to only be defined once, and can be reused from any other packages. | ||
|
||
|
||
# Links | ||
|
||
Currently, there are some specific places where links can be defined: | ||
|
||
- `elasticsearch/ingest_pipeline` | ||
- `data_stream/**/elasticsearch/ingest_pipeline` | ||
- `agent/input` | ||
- `data_stream/**/agent/stream` | ||
- `data_stream/**/fields` | ||
|
||
A link consists of a file with a `.link` extension that contains a path, relative to its location, to the file that it will be replaced with. It also consists of a checksum to validate the linked file is up to date with the package expectations. | ||
|
||
`data_stream/foo/elasticsearch/ingest_pipeline/default.yml.link` | ||
|
||
``` | ||
../../../../../testpackage/data_stream/test/elasticsearch/ingest_pipeline/default.yml f7c5f0c03aca8ef68c379a62447bdafbf0dcf32b1ff2de143fd6878ee01a91ad | ||
``` | ||
|
||
This will use the contents of the linked file during validation, tests, and building of the package, so functionally nothing changes from the package point of view. | ||
|
||
## The `_dev/shared` folder | ||
|
||
As a convenience, shared files can be placed under `_dev/shared` if they are going to be | ||
reused from several places. They can even be added outside of any package, in any place in the repository. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.