diff --git a/cmd/create.go b/cmd/create.go index a9fcde5..73e55c4 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -39,11 +37,8 @@ names or categories. Multiple arguments are supported.`, Run: func(_ *cobra.Command, args []string) { conts := config.Union(args, contMask) for _, cont := range conts { - fmt.Fprintln(output, "CREATE", cont.Name) - for _, c := range cont.CreateCommands() { - if err := c.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Create failed") - } + if err := getAndExecute(cont.CreateCommands, "CREATE", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Create failed") } } }, diff --git a/cmd/execute.go b/cmd/execute.go new file mode 100644 index 0000000..a8d284a --- /dev/null +++ b/cmd/execute.go @@ -0,0 +1,37 @@ +/* +Copyright © 2022 Joel D. Elkins + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "fmt" + "io" + + "gitea.elkins.co/Networking/ccl/internal/pkg/command" +) + +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 + } + return nil +} diff --git a/cmd/pull.go b/cmd/pull.go index 3f58801..b8e9a25 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -40,11 +38,8 @@ will still use it.`, Run: func(cmd *cobra.Command, args []string) { conts := config.Union(args, contMask) for _, cont := range conts { - fmt.Fprintln(output, "PULL", cont.Name) - for _, cmd := range cont.PullCommands() { - if err := cmd.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Pull failed") - } + if err := getAndExecute(cont.PullCommands, "PULL", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Pull failed") } } }, diff --git a/cmd/recreate.go b/cmd/recreate.go index ec012d7..a9acc11 100644 --- a/cmd/recreate.go +++ b/cmd/recreate.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -39,11 +37,8 @@ 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 { - fmt.Fprintln(output, "RECREATE", cont.Name) - for _, c := range cont.RecreateCommands() { - if err := c.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Recreation failed") - } + if err := getAndExecute(cont.RecreateCommands, "RECREATE", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Recreation failed") } } }, diff --git a/cmd/restart.go b/cmd/restart.go index a63655d..90bbc22 100644 --- a/cmd/restart.go +++ b/cmd/restart.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -39,11 +37,8 @@ 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 { - fmt.Fprintln(output, "RESTART", cont.Name) - for _, c := range cont.RestartCommands() { - if err := c.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Restart failed") - } + if err := getAndExecute(cont.RestartCommands, "RESTART", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Restart failed") } } }, diff --git a/cmd/rm.go b/cmd/rm.go index 4222684..95dd2c8 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -40,11 +38,8 @@ If running, they will first be stopped.`, Run: func(cmd *cobra.Command, args []string) { conts := config.Union(args, contMask) for _, cont := range conts { - fmt.Fprintln(output, "REMOVE", cont.Name) - for _, cmd := range cont.DestroyCommands() { - if err := cmd.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Remove failed") - } + if err := getAndExecute(cont.DestroyCommands, "REMOVE", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Remove failed") } } }, diff --git a/cmd/start.go b/cmd/start.go index 50fe819..e0502d6 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -39,11 +37,8 @@ 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 { - fmt.Fprintln(output, "START", cont.Name) - for _, c := range cont.StartCommands() { - if err := c.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Start failed") - } + if err := getAndExecute(cont.StartCommands, "START", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Start failed") } } }, diff --git a/cmd/stop.go b/cmd/stop.go index 72bc2c9..4b42ad5 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -39,11 +37,8 @@ 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 { - fmt.Fprintln(output, "STOP", cont.Name) - for _, c := range cont.StopCommands() { - if err := c.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Stop failed") - } + if err := getAndExecute(cont.StopCommands, "STOP", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Stop failed") } } }, diff --git a/cmd/update.go b/cmd/update.go index f4474ba..5acb3f8 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -22,8 +22,6 @@ THE SOFTWARE. package cmd import ( - "fmt" - "gitea.elkins.co/Networking/ccl/internal/pkg/config" "github.com/spf13/cobra" ) @@ -39,11 +37,8 @@ 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 { - fmt.Fprintln(output, "UPDATE", cont.Name) - for _, c := range cont.UpdateCommands() { - if err := c.Execute(output, fake); err != nil { - cont.LogEntry().WithField("error", err).Errorln("Update failed") - } + if err := getAndExecute(cont.UpdateCommands, "UPDATE", cont.Name, output, fake); err != nil { + cont.LogEntry().WithField("error", err).Errorln("Update failed") } } }, diff --git a/internal/pkg/command/command.go b/internal/pkg/command/command.go index f6b97d3..3b90f5d 100644 --- a/internal/pkg/command/command.go +++ b/internal/pkg/command/command.go @@ -66,6 +66,7 @@ type Command struct { Type CommandType Command interface{} } +type Commands []Command type errFunc struct { Name string @@ -120,6 +121,15 @@ func (c Command) GetShell() (string, error) { return s, fmt.Errorf("Type error. Requested = %s, Type = %s", CT_SH, c.Type) } +func (cmds Commands) Execute(output io.Writer, fake bool) error { + for _, c := range cmds { + if err := c.Execute(output, fake); err != nil { + return err + } + } + return nil +} + func (c Command) Execute(output io.Writer, fake bool) error { switch c.Type { case CT_NOP: diff --git a/internal/pkg/container/container.go b/internal/pkg/container/container.go index 86a4d09..ac2f6ba 100644 --- a/internal/pkg/container/container.go +++ b/internal/pkg/container/container.go @@ -111,17 +111,17 @@ func (c *Container) pull() error { return err } -func (c *Container) PullCommands() []command.Command { - return []command.Command{ +func (c *Container) PullCommands() command.Commands { + return command.Commands{ command.NewFunc("do_pull", func() error { return c.pull() }), } } -func (c *Container) CreateCommands() []command.Command { +func (c *Container) CreateCommands() command.Commands { if c.Image == "" { - return []command.Command{ + return command.Commands{ command.NewFunc("image_error", func() error { return fmt.Errorf("Image not configured") }), @@ -173,7 +173,7 @@ func (c *Container) CreateCommands() []command.Command { }, } - return []command.Command{ + return command.Commands{ command.NewFunc("bail_if_exists", func() error { if ex, err := containers.Exists(c.conn, c.Name, &containers.ExistsOptions{}); err != nil || ex { if err != nil { @@ -203,9 +203,9 @@ func (c *Container) CreateCommands() []command.Command { } } -func (c *Container) RecreateCommands() []command.Command { +func (c *Container) RecreateCommands() command.Commands { wasRunning := false - return []command.Command{ + return command.Commands{ command.NewFunc("stash_run_state", func() error { wasRunning = c.IsRunning() return nil @@ -220,7 +220,7 @@ func (c *Container) RecreateCommands() []command.Command { } } -func (c *Container) DestroyCommands() []command.Command { +func (c *Container) DestroyCommands() command.Commands { cmds := c.StopCommands() cmds = append(cmds, command.NewFunc("remove_if_exists", func() error { if c.cdata.ID == "" { @@ -233,8 +233,8 @@ func (c *Container) DestroyCommands() []command.Command { return cmds } -func (c *Container) StartCommands() []command.Command { - return []command.Command{ +func (c *Container) StartCommands() command.Commands { + return command.Commands{ command.NewFunc("start_container", func() error { if c.cdata.State != nil && c.cdata.State.Running { return nil @@ -260,8 +260,8 @@ func (c *Container) StartCommands() []command.Command { } } -func (c *Container) RestartCommands() []command.Command { - return []command.Command{ +func (c *Container) RestartCommands() command.Commands { + return command.Commands{ command.NewSet(c.StopCommands()), command.NewSet(c.StartCommands()), } @@ -281,9 +281,9 @@ func (c *Container) IsCreated() bool { return true } -func (c *Container) UpdateCommands() []command.Command { +func (c *Container) UpdateCommands() command.Commands { wasRunning := false - return []command.Command{ + return command.Commands{ command.NewFunc("do_update_and_stop", func() error { err := c.pull() if err != nil { @@ -313,8 +313,8 @@ func (c *Container) UpdateCommands() []command.Command { } } -func (c *Container) StopCommands() []command.Command { - return []command.Command{ +func (c *Container) StopCommands() command.Commands { + return command.Commands{ command.NewFunc("stop_if_running", func() error { if c.IsRunning() { var timeout uint = 10