Refactor command execution

This commit is contained in:
Joel Elkins 2022-07-29 19:42:46 -05:00
parent fc40b5c4a3
commit 04e108100a
No known key found for this signature in database
GPG Key ID: 133589DC38921AE2
11 changed files with 79 additions and 72 deletions

View File

@ -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")
}
}
},

37
cmd/execute.go Normal file
View File

@ -0,0 +1,37 @@
/*
Copyright © 2022 Joel D. Elkins <joel@elkins.co>
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
}

View File

@ -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")
}
}
},

View File

@ -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")
}
}
},

View File

@ -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")
}
}
},

View File

@ -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")
}
}
},

View File

@ -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")
}
}
},

View File

@ -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")
}
}
},

View File

@ -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")
}
}
},

View File

@ -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:

View File

@ -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