@@ -6,12 +6,8 @@ package cmd
66
77import (
88 "fmt"
9- "io"
10- "os"
11- "os/exec"
129 "slices"
1310 "strings"
14- "sync"
1511
1612 "github.com/spf13/cobra"
1713
@@ -49,26 +45,18 @@ func setupForeachCommand() *cobraext.Command {
4945 Short : "Execute a command for filtered packages" ,
5046 Long : foreachLongDescription ,
5147 Example : ` # Run system tests for packages with specific inputs
52- elastic-package foreach --input tcp,udp --parallel 10 -- test system -g` ,
48+ elastic-package foreach --input tcp,udp -- test system -g` ,
5349 RunE : foreachCommandAction ,
5450 Args : cobra .MinimumNArgs (1 ),
5551 }
5652
5753 // Add filter flags
5854 filter .SetFilterFlags (cmd )
5955
60- // Add pool size flag
61- cmd .Flags ().IntP (cobraext .ForeachPoolSizeFlagName , cobraext .ForeachPoolSizeFlagShorthand , 1 , cobraext .ForeachPoolSizeFlagDescription )
62-
6356 return cobraext .NewCommand (cmd , cobraext .ContextPackage )
6457}
6558
6659func foreachCommandAction (cmd * cobra.Command , args []string ) error {
67- poolSize , err := cmd .Flags ().GetInt (cobraext .ForeachPoolSizeFlagName )
68- if err != nil {
69- return fmt .Errorf ("getting pool size failed: %w" , err )
70- }
71-
7260 if err := validateSubCommand (args [0 ]); err != nil {
7361 return fmt .Errorf ("validating sub command failed: %w" , err )
7462 }
@@ -79,74 +67,29 @@ func foreachCommandAction(cmd *cobra.Command, args []string) error {
7967 return fmt .Errorf ("filtering packages failed: %w" , err )
8068 }
8169
82- wg := sync.WaitGroup {}
83- mu := sync.Mutex {}
84- errs := multierror.Error {}
85- successes := 0
86-
87- packagePathChan := make (chan string , poolSize )
88-
89- for range poolSize {
90- wg .Add (1 )
91- go func (packagePathChan <- chan string ) {
92- defer wg .Done ()
93- for packagePath := range packagePathChan {
94- err := executeCommand (args , packagePath )
95-
96- mu .Lock ()
97- if err != nil {
98- errs = append (errs , fmt .Errorf ("executing command for package %s failed: %w" , packagePath , err ))
99- } else {
100- successes ++
101- }
102- mu .Unlock ()
103- }
104- }(packagePathChan )
105- }
70+ errors := multierror.Error {}
10671
10772 for _ , pkg := range filtered {
108- packagePathChan <- pkg .Path
109- }
110- close (packagePathChan )
111-
112- wg .Wait ()
113-
114- logger .Infof ("Successfully executed command for %d packages\n " , successes )
115- logger .Infof ("Errors occurred while executing command for %d packages\n " , len (errs ))
116-
117- if errs .Error () != "" {
118- return fmt .Errorf ("errors occurred while executing command for packages: \n %s" , errs .Error ())
119- }
120-
121- return nil
122- }
123-
124- func executeCommand (args []string , path string ) error {
125- // Look up the elastic-package binary in PATH
126- execPath , err := exec .LookPath ("elastic-package" )
127- if err != nil {
128- return fmt .Errorf ("elastic-package binary not found in PATH: %w" , err )
73+ rootCmd := cmd .Root ()
74+ rootCmd .SetArgs (append (args , "--change-directory" , pkg .Path ))
75+ if err := rootCmd .Execute (); err != nil {
76+ errors = append (errors , err )
77+ }
12978 }
13079
131- cmd := & exec.Cmd {
132- Path : execPath ,
133- Args : append ([]string {execPath }, args ... ),
134- Dir : path ,
135- Stdout : io .Discard ,
136- Stderr : os .Stderr ,
137- Env : os .Environ (),
138- }
80+ logger .Infof ("Successfully executed command for %d packages" , len (filtered )- len (errors ))
13981
140- if err := cmd .Run (); err != nil {
141- return fmt .Errorf ("executing command for package %s failed: %w" , path , err )
82+ if errors .Error () != "" {
83+ logger .Errorf ("Errors occurred for %d packages" , len (errors ))
84+ return fmt .Errorf ("errors occurred while executing command for packages: \n %s" , errors .Error ())
14285 }
14386
14487 return nil
14588}
14689
14790func validateSubCommand (subCommand string ) error {
14891 if ! slices .Contains (getAllowedSubCommands (), subCommand ) {
149- return fmt .Errorf ("invalid subcommand: %s. Allowed subcommands are: %s " , subCommand , strings .Join (getAllowedSubCommands (), ", " ))
92+ return fmt .Errorf ("invalid subcommand: %s. Allowed subcommands are: [%s] " , subCommand , strings .Join (getAllowedSubCommands (), ", " ))
15093 }
15194
15295 return nil
0 commit comments