Skip to content

Commit

Permalink
refactor: remove use message.Fatal in cmd package
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Jun 26, 2024
1 parent 2ecaaa0 commit ec0d0e5
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 287 deletions.
18 changes: 0 additions & 18 deletions src/cmd/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,10 @@
// Package common handles command configuration across all commands
package common

import (
"context"

"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
)

// SetBaseDirectory sets the base directory. This is a directory with a zarf.yaml.
func SetBaseDirectory(args []string) string {
if len(args) > 0 {
return args[0]
}
return "."
}

// NewClusterOrDie creates a new Cluster instance and waits for the cluster to be ready or throws a fatal error.
func NewClusterOrDie(ctx context.Context) *cluster.Cluster {
timeoutCtx, cancel := context.WithTimeout(ctx, cluster.DefaultTimeout)
defer cancel()
c, err := cluster.NewClusterWithWait(timeoutCtx)
if err != nil {
message.Fatalf(err, "Failed to connect to cluster")
}
return c
}
29 changes: 18 additions & 11 deletions src/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package cmd

import (
"context"
"fmt"
"os"

"github.com/defenseunicorns/zarf/src/cmd/common"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
Expand All @@ -29,15 +28,16 @@ var (
Aliases: []string{"c"},
Short: lang.CmdConnectShort,
Long: lang.CmdConnectLong,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
var target string
if len(args) > 0 {
target = args[0]
}
spinner := message.NewProgressSpinner(lang.CmdConnectPreparingTunnel, target)
defer spinner.Stop()
c, err := cluster.NewCluster()
if err != nil {
spinner.Fatalf(err, lang.CmdConnectErrCluster, err.Error())
return err
}

ctx := cmd.Context()
Expand All @@ -50,7 +50,7 @@ var (
tunnel, err = c.Connect(ctx, target)
}
if err != nil {
spinner.Fatalf(err, lang.CmdConnectErrService, err.Error())
return fmt.Errorf("unable to connect to the service: %w", err)
}

defer tunnel.Close()
Expand All @@ -74,21 +74,28 @@ var (
case <-ctx.Done():
spinner.Successf(lang.CmdConnectTunnelClosed, url)
case err = <-tunnel.ErrChan():
spinner.Fatalf(err, lang.CmdConnectErrService, err.Error())
return fmt.Errorf("unable to connect to the service: %w", err)
}
os.Exit(0)
return nil
},
}

connectListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: lang.CmdConnectListShort,
Run: func(cmd *cobra.Command, _ []string) {
ctx := cmd.Context()
if err := common.NewClusterOrDie(ctx).PrintConnectTable(ctx); err != nil {
message.Fatal(err, err.Error())
RunE: func(cmd *cobra.Command, _ []string) error {
timeoutCtx, cancel := context.WithTimeout(cmd.Context(), cluster.DefaultTimeout)
defer cancel()
c, err := cluster.NewClusterWithWait(timeoutCtx)
if err != nil {
return err
}
err = c.PrintConnectTable(cmd.Context())
if err != nil {
return err
}
return nil
},
}
)
Expand Down
18 changes: 13 additions & 5 deletions src/cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
"regexp"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/zarf/src/cmd/common"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/internal/packager/helm"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"

Expand All @@ -28,9 +30,14 @@ var destroyCmd = &cobra.Command{
Aliases: []string{"d"},
Short: lang.CmdDestroyShort,
Long: lang.CmdDestroyLong,
Run: func(cmd *cobra.Command, _ []string) {
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
c := common.NewClusterOrDie(ctx)
timeoutCtx, cancel := context.WithTimeout(cmd.Context(), cluster.DefaultTimeout)
defer cancel()
c, err := cluster.NewClusterWithWait(timeoutCtx)
if err != nil {
return err
}

// NOTE: If 'zarf init' failed to deploy the k3s component (or if we're looking at the wrong kubeconfig)
// there will be no zarf-state to load and the struct will be empty. In these cases, if we can find
Expand All @@ -45,7 +52,7 @@ var destroyCmd = &cobra.Command{
// Check if we have the scripts to destroy everything
fileInfo, err := os.Stat(config.ZarfCleanupScriptsPath)
if errors.Is(err, os.ErrNotExist) || !fileInfo.IsDir() {
message.Fatalf(lang.CmdDestroyErrNoScriptPath, config.ZarfCleanupScriptsPath)
return fmt.Errorf("unable to find the folder %s which has the scripts to cleanup the cluster. Please double-check you have the right kube-context", config.ZarfCleanupScriptsPath)
}

// Run all the scripts!
Expand Down Expand Up @@ -73,12 +80,13 @@ var destroyCmd = &cobra.Command{

// If Zarf didn't deploy the cluster, only delete the ZarfNamespace
if err := c.DeleteZarfNamespace(ctx); err != nil {
message.Fatal(err, err.Error())
return err
}

// Remove zarf agent labels and secrets from namespaces Zarf doesn't manage
c.StripZarfLabelsAndSecretsFromNamespaces(ctx)
}
return nil
},
}

Expand Down
Loading

0 comments on commit ec0d0e5

Please sign in to comment.