-
Notifications
You must be signed in to change notification settings - Fork 129
Add filter and foreach sub commands for bulk operations #3027
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
base: main
Are you sure you want to change the base?
Changes from all commits
85d659a
17e4b39
01a681c
010f1a4
358f9d6
c3fef19
3d84151
9a1c2aa
1336ff2
b588164
53bd4f8
2d827cc
bc7a167
ada16f7
9123dae
423f035
2e3721c
8180ff4
f2abc11
f72ea26
5c4d63e
d94e9b2
3cc0824
78cce2b
2656a22
c36efd7
0649c73
5e3b8b0
df2c85b
b495ced
812530c
e3e3c9a
54fce2b
13f348f
b10bb7f
ab72763
005c2c3
8189930
caaff9a
f997640
a9699e4
90a93ad
689eb72
6196985
12a7bea
d93ee4f
89ef933
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ elastic-package | |
| # IDEA | ||
| .idea | ||
|
|
||
| # VSCode | ||
| .vscode | ||
|
|
||
| # Build directory | ||
| /build | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // 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" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/elastic/elastic-package/internal/cobraext" | ||
| "github.com/elastic/elastic-package/internal/filter" | ||
| "github.com/elastic/elastic-package/internal/packages" | ||
| ) | ||
|
|
||
| const filterLongDescription = `This command gives you a list of all packages based on the given query. | ||
|
|
||
| The command will search for packages in the working directory for default depth of 2 and | ||
| return the list of packages that match the given criteria. | ||
|
|
||
| Use --change-directory to change the working directory and --depth to change the depth of the search.` | ||
|
|
||
| const filterExample = `elastic-package filter --inputs tcp,udp --categories security --depth 3 --output json | ||
| elastic-package filter --packages 'cisco_*,fortinet_*' --output yaml | ||
| ` | ||
|
|
||
| func setupFilterCommand() *cobraext.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "filter [flags]", | ||
| Short: "filter integrations based on given flags", | ||
| Long: filterLongDescription, | ||
| Args: cobra.NoArgs, | ||
| RunE: filterCommandAction, | ||
| Example: filterExample, | ||
| } | ||
|
|
||
| // add filter flags to the command (input, code owner, kibana version, categories) | ||
| filter.SetFilterFlags(cmd) | ||
|
|
||
| // add the output package name and absolute path flags to the command | ||
| cmd.Flags().StringP(cobraext.FilterOutputFlagName, cobraext.FilterOutputFlagShorthand, "", cobraext.FilterOutputFlagDescription) | ||
| cmd.Flags().StringP(cobraext.FilterOutputInfoFlagName, "", cobraext.FilterOutputInfoFlagDefault, cobraext.FilterOutputInfoFlagDescription) | ||
|
|
||
| return cobraext.NewCommand(cmd, cobraext.ContextPackage) | ||
| } | ||
|
|
||
| func filterCommandAction(cmd *cobra.Command, args []string) error { | ||
| filtered, err := filterPackage(cmd) | ||
| if err != nil { | ||
| return fmt.Errorf("filtering packages failed: %w", err) | ||
| } | ||
|
|
||
| outputFormatStr, err := cmd.Flags().GetString(cobraext.FilterOutputFlagName) | ||
| if err != nil { | ||
| return fmt.Errorf("getting output format flag failed: %w", err) | ||
| } | ||
|
|
||
| outputInfoStr, err := cmd.Flags().GetString(cobraext.FilterOutputInfoFlagName) | ||
| if err != nil { | ||
| return fmt.Errorf("getting output info flag failed: %w", err) | ||
| } | ||
|
|
||
| outputOptions, err := filter.NewOutputOptions(outputInfoStr, outputFormatStr) | ||
| if err != nil { | ||
| return fmt.Errorf("creating output options failed: %w", err) | ||
| } | ||
|
|
||
| if err = printPkgList(filtered, outputOptions, os.Stdout); err != nil { | ||
| return fmt.Errorf("printing JSON failed: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func filterPackage(cmd *cobra.Command) ([]packages.PackageDirNameAndManifest, error) { | ||
| depth, err := cmd.Flags().GetInt(cobraext.FilterDepthFlagName) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("getting depth flag failed: %w", err) | ||
| } | ||
|
|
||
| excludeDirs, err := cmd.Flags().GetString(cobraext.FilterExcludeDirFlagName) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("getting exclude-dir flag failed: %w", err) | ||
| } | ||
|
|
||
| filters := filter.NewFilterRegistry(depth, excludeDirs) | ||
|
|
||
| if err := filters.Parse(cmd); err != nil { | ||
| return nil, fmt.Errorf("parsing filter options failed: %w", err) | ||
| } | ||
|
|
||
| if err := filters.Validate(); err != nil { | ||
| return nil, fmt.Errorf("validating filter options failed: %w", err) | ||
| } | ||
|
|
||
| filtered, errors := filters.Execute() | ||
| if errors != nil { | ||
| return nil, fmt.Errorf("filtering packages failed: %s", errors.Error()) | ||
| } | ||
|
|
||
| return filtered, nil | ||
| } | ||
|
|
||
| func printPkgList(pkgs []packages.PackageDirNameAndManifest, outputOptions *filter.OutputOptions, w io.Writer) error { | ||
| formatted, err := outputOptions.ApplyTo(pkgs) | ||
| if err != nil { | ||
| return fmt.Errorf("applying output format failed: %w", err) | ||
| } | ||
|
|
||
| // write the formatted packages to the writer | ||
| _, err = io.WriteString(w, formatted+"\n") | ||
| return err | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // 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" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/elastic/elastic-package/internal/cobraext" | ||
| "github.com/elastic/elastic-package/internal/filter" | ||
| "github.com/elastic/elastic-package/internal/logger" | ||
| "github.com/elastic/elastic-package/internal/multierror" | ||
| ) | ||
|
|
||
| const foreachLongDescription = `Execute a command for each package matching the given filter criteria. | ||
|
|
||
| This command combines filtering capabilities with command execution, allowing you to run | ||
| any elastic-package subcommand across multiple packages in a single operation. | ||
|
|
||
| The command uses the same filter flags as the 'filter' command to select packages, | ||
| then executes the specified subcommand for each matched package.` | ||
|
|
||
| // getAllowedSubCommands returns the list of allowed subcommands for the foreach command. | ||
| func getAllowedSubCommands() []string { | ||
| return []string{ | ||
| "build", | ||
| "check", | ||
| "changelog", | ||
| "clean", | ||
| "format", | ||
| "install", | ||
| "lint", | ||
| "test", | ||
| "uninstall", | ||
| } | ||
| } | ||
|
|
||
| func setupForeachCommand() *cobraext.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "foreach [flags] -- <SUBCOMMAND>", | ||
| Short: "Execute a command for filtered packages", | ||
| Long: foreachLongDescription, | ||
| Example: ` # Run system tests for packages with specific inputs | ||
| elastic-package foreach --input tcp,udp -- test system -g`, | ||
| RunE: foreachCommandAction, | ||
| Args: cobra.MinimumNArgs(1), | ||
| } | ||
|
|
||
| // Add filter flags | ||
| filter.SetFilterFlags(cmd) | ||
|
|
||
| return cobraext.NewCommand(cmd, cobraext.ContextPackage) | ||
| } | ||
|
|
||
| func foreachCommandAction(cmd *cobra.Command, args []string) error { | ||
| if err := validateSubCommand(args[0]); err != nil { | ||
| return fmt.Errorf("validating sub command failed: %w", err) | ||
| } | ||
|
|
||
| // reuse filterPackage from cmd/filter.go | ||
| filtered, err := filterPackage(cmd) | ||
| if err != nil { | ||
| return fmt.Errorf("filtering packages failed: %w", err) | ||
| } | ||
|
|
||
| errors := multierror.Error{} | ||
|
|
||
| for _, pkg := range filtered { | ||
| rootCmd := cmd.Root() | ||
| rootCmd.SetArgs(append(args, "--change-directory", pkg.Path)) | ||
| if err := rootCmd.Execute(); err != nil { | ||
| errors = append(errors, err) | ||
| } | ||
| } | ||
|
|
||
| logger.Infof("Successfully executed command for %d packages", len(filtered)-len(errors)) | ||
|
|
||
| if errors.Error() != "" { | ||
| logger.Errorf("Errors occurred for %d packages", len(errors)) | ||
| return fmt.Errorf("errors occurred while executing command for packages: \n%s", errors.Error()) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func validateSubCommand(subCommand string) error { | ||
| if !slices.Contains(getAllowedSubCommands(), subCommand) { | ||
| return fmt.Errorf("invalid subcommand: %s. Allowed subcommands are: [%s]", subCommand, strings.Join(getAllowedSubCommands(), ", ")) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,8 +133,49 @@ const ( | |
| FailOnMissingFlagName = "fail-on-missing" | ||
| FailOnMissingFlagDescription = "fail if tests are missing" | ||
|
|
||
| FailFastFlagName = "fail-fast" | ||
| FailFastFlagDescription = "fail immediately if any file requires updates (do not overwrite)" | ||
| FailFastFlagName = "fail-fast" | ||
| FailFastFlagDescription = "fail immediately if any file requires updates (do not overwrite)" | ||
|
|
||
| FilterCategoriesFlagName = "categories" | ||
| FilterCategoriesFlagDescription = "integration categories to filter by (comma-separated values)" | ||
|
|
||
| FilterCodeOwnerFlagName = "code-owners" | ||
| FilterCodeOwnerFlagDescription = "code owners to filter by (comma-separated values)" | ||
|
|
||
| FilterDepthFlagName = "depth" | ||
| FilterDepthFlagDescription = "maximum depth to search for packages" | ||
| FilterDepthFlagDefault = 2 | ||
| FilterDepthFlagShorthand = "d" | ||
|
Member
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 need to filter out built packages by default, otherwise they are discovered, and it is confusing to see them listed as their versions:
Author
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. I came across the same issue, so I've added a
Member
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 |
||
|
|
||
| FilterExcludeDirFlagName = "exclude-dirs" | ||
| FilterExcludeDirFlagDescription = "comma-separated list of directories to exclude from search" | ||
|
|
||
| FilterInputFlagName = "inputs" | ||
| FilterInputFlagDescription = "name of the inputs to filter by (comma-separated values)" | ||
|
|
||
| FilterKibanaVersionFlagName = "kibana-version" | ||
| FilterKibanaVersionFlagDescription = "kibana version to filter by (semver)" | ||
|
|
||
| FilterOutputFlagName = "output" | ||
| FilterOutputFlagDescription = "format of the output. Available options: json, yaml (leave empty for newline-separated list)" | ||
| FilterOutputFlagShorthand = "o" | ||
|
|
||
| FilterOutputInfoFlagName = "output-info" | ||
| FilterOutputInfoFlagDescription = "output information about the packages. Available options: pkgname, dirname, absolute" | ||
| FilterOutputInfoFlagDefault = "dirname" | ||
|
|
||
| FilterPackageDirNameFlagName = "package-dirs" | ||
| FilterPackageDirNameFlagDescription = "package directories to filter by (comma-separated values)" | ||
|
Member
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. How is this flag expected to work? If I use
Author
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.
example: if you want to filter all packages in one directory you can use the following command.
Member
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. Oh ok. So it is not possible to search in multiple directories. I guess this is fine.
Contributor
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. does it make sense to add the example to the description so it goes to the docs? |
||
|
|
||
| FilterPackagesFlagName = "packages" | ||
| FilterPackagesFlagDescription = "package names to filter by (comma-separated values)" | ||
|
|
||
| FilterPackageTypeFlagName = "package-types" | ||
| FilterPackageTypeFlagDescription = "package types to filter by (comma-separated values)" | ||
|
|
||
| FilterSpecVersionFlagName = "spec-version" | ||
| FilterSpecVersionFlagDescription = "Package spec version to filter by (semver)" | ||
|
|
||
| GenerateTestResultFlagName = "generate" | ||
| GenerateTestResultFlagDescription = "generate test result file" | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could define another root command with the specific commands that are supported and use it here, then we could have different sets of subcommands, and maybe we could even plug it directly as subcommands of foreach.
This could be left for a future refactor too.
Something like this:
That could be used here like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something that we need to do to add parallel execution. That's why I left it out of this PR, and would like to revisit it while I work on the other PR.
With that said, we do have a list of allowed sub-commands. Here
Let me know if I missed any command. - I'll also add
changelogcommand in allow list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can leave this for a future refactor.
Yes please.