From e138eeee716e09ca262b26f5131cacdda1921047 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Mon, 4 Mar 2024 12:56:42 -0600 Subject: [PATCH] chore(util): Move job deletion to cobra finalizer to simplify cmd workflows --- cmd/dump/dump.go | 3 --- cmd/exec/exec.go | 3 --- cmd/port_forward/port_forward.go | 4 ---- cmd/restore/restore.go | 4 ---- cmd/status/status.go | 1 - internal/util/cmd_setup.go | 4 +++- internal/util/cmd_teardown.go | 3 +-- 7 files changed, 4 insertions(+), 18 deletions(-) diff --git a/cmd/dump/dump.go b/cmd/dump/dump.go index 0619d3cb..5a1e879d 100644 --- a/cmd/dump/dump.go +++ b/cmd/dump/dump.go @@ -146,8 +146,5 @@ func preRun(cmd *cobra.Command, args []string) (err error) { } func run(cmd *cobra.Command, args []string) (err error) { - defer func() { - util.Teardown(cmd, &action.Global) - }() return action.Run(cmd.Context()) } diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 4ebe20d3..2ba7433d 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -48,8 +48,5 @@ func preRun(cmd *cobra.Command, args []string) error { } func run(cmd *cobra.Command, args []string) (err error) { - defer func() { - util.Teardown(cmd, &action.Global) - }() return action.Run(cmd.Context()) } diff --git a/cmd/port_forward/port_forward.go b/cmd/port_forward/port_forward.go index dcc48fbf..0f9d0321 100644 --- a/cmd/port_forward/port_forward.go +++ b/cmd/port_forward/port_forward.go @@ -76,7 +76,6 @@ func preRun(cmd *cobra.Command, args []string) error { } action.LocalPort, err = cmd.Flags().GetUint16(consts.ListenPortFlag) if err != nil { - util.Teardown(cmd, &action.Global) panic(err) } if action.LocalPort == 0 { @@ -86,8 +85,5 @@ func preRun(cmd *cobra.Command, args []string) error { } func run(cmd *cobra.Command, args []string) (err error) { - defer func() { - util.Teardown(cmd, &action.Global) - }() return action.Run(cmd.Context()) } diff --git a/cmd/restore/restore.go b/cmd/restore/restore.go index a9b234b4..14663eff 100644 --- a/cmd/restore/restore.go +++ b/cmd/restore/restore.go @@ -135,9 +135,5 @@ func preRun(cmd *cobra.Command, args []string) (err error) { } func run(cmd *cobra.Command, args []string) (err error) { - defer func() { - util.Teardown(cmd, &action.Global) - }() - return action.Run(cmd.Context()) } diff --git a/cmd/status/status.go b/cmd/status/status.go index 6d127e9c..3ea074f3 100644 --- a/cmd/status/status.go +++ b/cmd/status/status.go @@ -103,7 +103,6 @@ func run(cmd *cobra.Command, args []string) error { fmt.Println(prefixErr, "Job creation failed:", err.Error()) os.Exit(1) } - defer util.Teardown(cmd, &conf) var buf strings.Builder listTablesCmd := conf.Dialect.ExecCommand(config.Exec{ diff --git a/internal/util/cmd_setup.go b/internal/util/cmd_setup.go index fb64c0f4..5579727a 100644 --- a/internal/util/cmd_setup.go +++ b/internal/util/cmd_setup.go @@ -229,9 +229,11 @@ func CreateJob(cmd *cobra.Command, conf *config.Global, opts SetupOptions) error if err := createJob(cmd, conf, opts.Name); err != nil { return err } + cobra.OnFinalize(func() { + Teardown(conf) + }) if err := watchJobPod(cmd, conf); err != nil { - Teardown(cmd, conf) return err } } diff --git a/internal/util/cmd_teardown.go b/internal/util/cmd_teardown.go index eaadfa25..e9bf6a7f 100644 --- a/internal/util/cmd_teardown.go +++ b/internal/util/cmd_teardown.go @@ -6,11 +6,10 @@ import ( "github.com/clevyr/kubedb/internal/config" log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func Teardown(cmd *cobra.Command, conf *config.Global) { +func Teardown(conf *config.Global) { if conf.Job != nil { log.WithField("name", conf.Job.ObjectMeta.Name).Info("cleaning up job") policy := metav1.DeletePropagationForeground