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 Jul 1, 2024
1 parent 601a282 commit 26aad35
Show file tree
Hide file tree
Showing 15 changed files with 294 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 {

Check warning on line 31 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L31

Added line #L31 was not covered by tests
var target string
if len(args) > 0 {
target = args[0]
}
spinner := message.NewProgressSpinner(lang.CmdConnectPreparingTunnel, target)
defer spinner.Stop()

Check warning on line 37 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L37

Added line #L37 was not covered by tests
c, err := cluster.NewCluster()
if err != nil {
spinner.Fatalf(err, lang.CmdConnectErrCluster, err.Error())
return err

Check warning on line 40 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L40

Added line #L40 was not covered by tests
}

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)

Check warning on line 53 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L53

Added line #L53 was not covered by tests
}

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("lost connection to the service: %w", err)

Check warning on line 77 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L77

Added line #L77 was not covered by tests
}
os.Exit(0)
return nil

Check warning on line 79 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L79

Added line #L79 was not covered by tests
},
}

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

Check warning on line 92 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L87-L92

Added lines #L87 - L92 were not covered by tests
}
err = c.PrintConnectTable(cmd.Context())
if err != nil {
return err

Check warning on line 96 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L94-L96

Added lines #L94 - L96 were not covered by tests
}
return nil

Check warning on line 98 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L98

Added line #L98 was not covered by tests
},
}
)
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 {

Check warning on line 33 in src/cmd/destroy.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/destroy.go#L33

Added line #L33 was not covered by tests
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

Check warning on line 39 in src/cmd/destroy.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/destroy.go#L35-L39

Added lines #L35 - L39 were not covered by tests
}

// 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)

Check warning on line 55 in src/cmd/destroy.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/destroy.go#L55

Added line #L55 was not covered by tests
}

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

Check warning on line 83 in src/cmd/destroy.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/destroy.go#L83

Added line #L83 was not covered by tests
}

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

Check warning on line 89 in src/cmd/destroy.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/destroy.go#L89

Added line #L89 was not covered by tests
},
}

Expand Down
Loading

0 comments on commit 26aad35

Please sign in to comment.