diff --git a/cmd/create.go b/cmd/create.go index 73e55c4..2460eea 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -36,11 +38,7 @@ var createCmd = &cobra.Command{ names or categories. Multiple arguments are supported.`, Run: func(_ *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.CreateCommands, "CREATE", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Create failed") - } - } + getAndExecute("CREATE", conts, func(c *container.Container) command.Commands { return c.CreateCommands() }) }, } diff --git a/cmd/execute.go b/cmd/execute.go index a8d284a..c3640e0 100644 --- a/cmd/execute.go +++ b/cmd/execute.go @@ -23,15 +23,27 @@ package cmd import ( "fmt" - "io" + "sync" "gitea.elkins.co/Networking/ccl/internal/pkg/command" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" ) -func getAndExecute(getCmds func() command.Commands, action, label string, output io.Writer, fake bool) error { - fmt.Fprintln(output, action, label) - if err := getCmds().Execute(output, fake); err != nil { - return err +func getAndExecute(action string, tgts []container.Container, getCmds func(*container.Container) command.Commands) { + var wg sync.WaitGroup + for i := range tgts { + fmt.Fprintln(output, action, tgts[i].Name) + wg.Add(1) + go func(cont *container.Container) { + defer wg.Done() + for _, cmd := range getCmds(cont) { + if err := cmd.Execute(output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Could not", action) + return + } + } + }(&tgts[i]) } - return nil + wg.Wait() } + diff --git a/cmd/pull.go b/cmd/pull.go index b8e9a25..a0375b5 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -37,11 +39,7 @@ affected: the old image will still remain, though untagged, and any defined cont will still use it.`, Run: func(cmd *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.PullCommands, "PULL", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Pull failed") - } - } + getAndExecute("PULL", conts, func(c *container.Container) command.Commands { return c.PullCommands() }) }, } diff --git a/cmd/recreate.go b/cmd/recreate.go index a9acc11..bed4c1a 100644 --- a/cmd/recreate.go +++ b/cmd/recreate.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -36,11 +38,7 @@ var recreateCmd = &cobra.Command{ one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.RecreateCommands, "RECREATE", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Recreation failed") - } - } + getAndExecute("RECREATE", conts, func(c *container.Container) command.Commands { return c.RecreateCommands() }) }, } diff --git a/cmd/restart.go b/cmd/restart.go index 90bbc22..6860d27 100644 --- a/cmd/restart.go +++ b/cmd/restart.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -36,11 +38,7 @@ var restartCmd = &cobra.Command{ one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.RestartCommands, "RESTART", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Restart failed") - } - } + getAndExecute("RESTART", conts, func(c *container.Container) command.Commands { return c.RestartCommands() }) }, } diff --git a/cmd/rm.go b/cmd/rm.go index 95dd2c8..5e7b6e3 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -37,11 +39,7 @@ var rmCmd = &cobra.Command{ If running, they will first be stopped.`, Run: func(cmd *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.DestroyCommands, "REMOVE", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Remove failed") - } - } + getAndExecute("REMOVE", conts, func(c *container.Container) command.Commands { return c.DestroyCommands() }) }, } diff --git a/cmd/start.go b/cmd/start.go index e0502d6..1f4b26f 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -36,11 +38,7 @@ var startCmd = &cobra.Command{ one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.StartCommands, "START", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Start failed") - } - } + getAndExecute("START", conts, func(c *container.Container) command.Commands { return c.StartCommands() }) }, } diff --git a/cmd/stop.go b/cmd/stop.go index 4b42ad5..007ff4e 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -36,11 +38,7 @@ var stopCmd = &cobra.Command{ one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.StopCommands, "STOP", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Stop failed") - } - } + getAndExecute("STOP", conts, func(c *container.Container) command.Commands { return c.StopCommands() }) }, } diff --git a/cmd/update.go b/cmd/update.go index 5acb3f8..3bccbef 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" "github.com/spf13/cobra" ) @@ -36,11 +38,7 @@ var updateCmd = &cobra.Command{ one or more container names or categories. If empty, "all" is assumed.`, Run: func(_ *cobra.Command, args []string) { conts := config.Union(args, contMask) - for _, cont := range conts { - if err := getAndExecute(cont.UpdateCommands, "UPDATE", cont.Name, output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Update failed") - } - } + getAndExecute("UPDATE", conts, func(c *container.Container) command.Commands { return c.UpdateCommands() }) }, }