mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-09 12:41:40 -05:00
Refactor command execution
This commit is contained in:
parent
fc40b5c4a3
commit
04e108100a
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -39,11 +37,8 @@ names or categories. Multiple arguments are supported.`,
|
|||||||
Run: func(_ *cobra.Command, args []string) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "CREATE", cont.Name)
|
if err := getAndExecute(cont.CreateCommands, "CREATE", cont.Name, output, fake); err != nil {
|
||||||
for _, c := range cont.CreateCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Create failed")
|
||||||
if err := c.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Create failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
37
cmd/execute.go
Normal file
37
cmd/execute.go
Normal 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
|
||||||
|
}
|
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -40,11 +38,8 @@ will still use it.`,
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "PULL", cont.Name)
|
if err := getAndExecute(cont.PullCommands, "PULL", cont.Name, output, fake); err != nil {
|
||||||
for _, cmd := range cont.PullCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Pull failed")
|
||||||
if err := cmd.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Pull failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"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) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "RECREATE", cont.Name)
|
if err := getAndExecute(cont.RecreateCommands, "RECREATE", cont.Name, output, fake); err != nil {
|
||||||
for _, c := range cont.RecreateCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Recreation failed")
|
||||||
if err := c.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Recreation failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"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) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "RESTART", cont.Name)
|
if err := getAndExecute(cont.RestartCommands, "RESTART", cont.Name, output, fake); err != nil {
|
||||||
for _, c := range cont.RestartCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Restart failed")
|
||||||
if err := c.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Restart failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -40,11 +38,8 @@ If running, they will first be stopped.`,
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "REMOVE", cont.Name)
|
if err := getAndExecute(cont.DestroyCommands, "REMOVE", cont.Name, output, fake); err != nil {
|
||||||
for _, cmd := range cont.DestroyCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Remove failed")
|
||||||
if err := cmd.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Remove failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"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) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "START", cont.Name)
|
if err := getAndExecute(cont.StartCommands, "START", cont.Name, output, fake); err != nil {
|
||||||
for _, c := range cont.StartCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Start failed")
|
||||||
if err := c.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Start failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"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) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "STOP", cont.Name)
|
if err := getAndExecute(cont.StopCommands, "STOP", cont.Name, output, fake); err != nil {
|
||||||
for _, c := range cont.StopCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Stop failed")
|
||||||
if err := c.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Stop failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
|
||||||
"github.com/spf13/cobra"
|
"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) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
conts := config.Union(args, contMask)
|
conts := config.Union(args, contMask)
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
fmt.Fprintln(output, "UPDATE", cont.Name)
|
if err := getAndExecute(cont.UpdateCommands, "UPDATE", cont.Name, output, fake); err != nil {
|
||||||
for _, c := range cont.UpdateCommands() {
|
cont.LogEntry().WithField("error", err).Errorln("Update failed")
|
||||||
if err := c.Execute(output, fake); err != nil {
|
|
||||||
cont.LogEntry().WithField("error", err).Errorln("Update failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -66,6 +66,7 @@ type Command struct {
|
|||||||
Type CommandType
|
Type CommandType
|
||||||
Command interface{}
|
Command interface{}
|
||||||
}
|
}
|
||||||
|
type Commands []Command
|
||||||
|
|
||||||
type errFunc struct {
|
type errFunc struct {
|
||||||
Name string
|
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)
|
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 {
|
func (c Command) Execute(output io.Writer, fake bool) error {
|
||||||
switch c.Type {
|
switch c.Type {
|
||||||
case CT_NOP:
|
case CT_NOP:
|
||||||
|
@ -111,17 +111,17 @@ func (c *Container) pull() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) PullCommands() []command.Command {
|
func (c *Container) PullCommands() command.Commands {
|
||||||
return []command.Command{
|
return command.Commands{
|
||||||
command.NewFunc("do_pull", func() error {
|
command.NewFunc("do_pull", func() error {
|
||||||
return c.pull()
|
return c.pull()
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) CreateCommands() []command.Command {
|
func (c *Container) CreateCommands() command.Commands {
|
||||||
if c.Image == "" {
|
if c.Image == "" {
|
||||||
return []command.Command{
|
return command.Commands{
|
||||||
command.NewFunc("image_error", func() error {
|
command.NewFunc("image_error", func() error {
|
||||||
return fmt.Errorf("Image not configured")
|
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 {
|
command.NewFunc("bail_if_exists", func() error {
|
||||||
if ex, err := containers.Exists(c.conn, c.Name, &containers.ExistsOptions{}); err != nil || ex {
|
if ex, err := containers.Exists(c.conn, c.Name, &containers.ExistsOptions{}); err != nil || ex {
|
||||||
if err != nil {
|
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
|
wasRunning := false
|
||||||
return []command.Command{
|
return command.Commands{
|
||||||
command.NewFunc("stash_run_state", func() error {
|
command.NewFunc("stash_run_state", func() error {
|
||||||
wasRunning = c.IsRunning()
|
wasRunning = c.IsRunning()
|
||||||
return nil
|
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 := c.StopCommands()
|
||||||
cmds = append(cmds, command.NewFunc("remove_if_exists", func() error {
|
cmds = append(cmds, command.NewFunc("remove_if_exists", func() error {
|
||||||
if c.cdata.ID == "" {
|
if c.cdata.ID == "" {
|
||||||
@ -233,8 +233,8 @@ func (c *Container) DestroyCommands() []command.Command {
|
|||||||
return cmds
|
return cmds
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) StartCommands() []command.Command {
|
func (c *Container) StartCommands() command.Commands {
|
||||||
return []command.Command{
|
return command.Commands{
|
||||||
command.NewFunc("start_container", func() error {
|
command.NewFunc("start_container", func() error {
|
||||||
if c.cdata.State != nil && c.cdata.State.Running {
|
if c.cdata.State != nil && c.cdata.State.Running {
|
||||||
return nil
|
return nil
|
||||||
@ -260,8 +260,8 @@ func (c *Container) StartCommands() []command.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) RestartCommands() []command.Command {
|
func (c *Container) RestartCommands() command.Commands {
|
||||||
return []command.Command{
|
return command.Commands{
|
||||||
command.NewSet(c.StopCommands()),
|
command.NewSet(c.StopCommands()),
|
||||||
command.NewSet(c.StartCommands()),
|
command.NewSet(c.StartCommands()),
|
||||||
}
|
}
|
||||||
@ -281,9 +281,9 @@ func (c *Container) IsCreated() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) UpdateCommands() []command.Command {
|
func (c *Container) UpdateCommands() command.Commands {
|
||||||
wasRunning := false
|
wasRunning := false
|
||||||
return []command.Command{
|
return command.Commands{
|
||||||
command.NewFunc("do_update_and_stop", func() error {
|
command.NewFunc("do_update_and_stop", func() error {
|
||||||
err := c.pull()
|
err := c.pull()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -313,8 +313,8 @@ func (c *Container) UpdateCommands() []command.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) StopCommands() []command.Command {
|
func (c *Container) StopCommands() command.Commands {
|
||||||
return []command.Command{
|
return command.Commands{
|
||||||
command.NewFunc("stop_if_running", func() error {
|
command.NewFunc("stop_if_running", func() error {
|
||||||
if c.IsRunning() {
|
if c.IsRunning() {
|
||||||
var timeout uint = 10
|
var timeout uint = 10
|
||||||
|
Loading…
x
Reference in New Issue
Block a user