Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove message.Fatal and spinner.Fatal #2676

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)

pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}
defer pkgClient.ClearTempPaths()

if err := pkgClient.DevDeploy(cmd.Context()); err != nil {
Expand All @@ -72,10 +75,13 @@
pkgConfig.CreateOpts.BaseDir = "."
pkgConfig.FindImagesOpts.RepoHelmChartPath = pkgConfig.GenerateOpts.GitPath

pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

Check warning on line 80 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L78-L80

Added lines #L78 - L80 were not covered by tests
}
defer pkgClient.ClearTempPaths()

err := pkgClient.Generate()
err = pkgClient.Generate()

Check warning on line 84 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L84

Added line #L84 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -217,7 +223,10 @@
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)
pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

Check warning on line 228 in src/cmd/dev.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/dev.go#L226-L228

Added lines #L226 - L228 were not covered by tests
}
defer pkgClient.ClearTempPaths()

if _, err := pkgClient.FindImages(cmd.Context()); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)

pkgClient := packager.NewOrDie(&pkgConfig, packager.WithSource(src))
pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))
if err != nil {
return err

Check warning on line 68 in src/cmd/initialize.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/initialize.go#L66-L68

Added lines #L66 - L68 were not covered by tests
}
defer pkgClient.ClearTempPaths()

err = pkgClient.Deploy(cmd.Context())
Expand Down
35 changes: 28 additions & 7 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)

pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

Check warning on line 59 in src/cmd/package.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/package.go#L57-L59

Added lines #L57 - L59 were not covered by tests
}
defer pkgClient.ClearTempPaths()

if err := pkgClient.Create(cmd.Context()); err != nil {
Expand All @@ -81,7 +84,10 @@
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)

pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

src/cmd/package.go#L87-L89

Added lines #L87 - L89 were not covered by tests
}
defer pkgClient.ClearTempPaths()

ctx := cmd.Context()
Expand All @@ -106,7 +112,10 @@
return err
}
pkgConfig.PkgOpts.PackageSource = packageSource
pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

Check warning on line 117 in src/cmd/package.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/package.go#L115-L117

Added lines #L115 - L117 were not covered by tests
}
defer pkgClient.ClearTempPaths()
if err := pkgClient.Mirror(cmd.Context()); err != nil {
return fmt.Errorf("failed to mirror package: %w", err)
Expand All @@ -131,7 +140,10 @@
if err != nil {
return err
}
pkgClient := packager.NewOrDie(&pkgConfig, packager.WithSource(src))
pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))
if err != nil {
return err

Check warning on line 145 in src/cmd/package.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/package.go#L143-L145

Added lines #L143 - L145 were not covered by tests
}
defer pkgClient.ClearTempPaths()
if err := pkgClient.Inspect(cmd.Context()); err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
Expand Down Expand Up @@ -200,7 +212,10 @@
if err != nil {
return err
}
pkgClient := packager.NewOrDie(&pkgConfig, packager.WithSource(src))
pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))
if err != nil {
return err

Check warning on line 217 in src/cmd/package.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/package.go#L215-L217

Added lines #L215 - L217 were not covered by tests
}
defer pkgClient.ClearTempPaths()
if err := pkgClient.Remove(cmd.Context()); err != nil {
return fmt.Errorf("unable to remove the package with an error of: %w", err)
Expand Down Expand Up @@ -238,7 +253,10 @@

pkgConfig.PublishOpts.PackageDestination = ref.String()

pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

Check warning on line 258 in src/cmd/package.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/package.go#L256-L258

Added lines #L256 - L258 were not covered by tests
}
defer pkgClient.ClearTempPaths()

if err := pkgClient.Publish(cmd.Context()); err != nil {
Expand All @@ -255,7 +273,10 @@
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pkgConfig.PkgOpts.PackageSource = args[0]
pkgClient := packager.NewOrDie(&pkgConfig)
pkgClient, err := packager.New(&pkgConfig)
if err != nil {
return err

Check warning on line 278 in src/cmd/package.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/package.go#L276-L278

Added lines #L276 - L278 were not covered by tests
}
defer pkgClient.ClearTempPaths()
if err := pkgClient.Pull(cmd.Context()); err != nil {
return fmt.Errorf("failed to pull package: %w", err)
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@
Short: lang.CmdToolsGenPkiShort,
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
pki := pki.GeneratePKI(args[0], subAltNames...)
pki, err := pki.GeneratePKI(args[0], subAltNames...)
if err != nil {
return err

Check warning on line 238 in src/cmd/tools/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/tools/zarf.go#L236-L238

Added lines #L236 - L238 were not covered by tests
}
if err := os.WriteFile("tls.ca", pki.CA, helpers.ReadAllWriteUser); err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions src/internal/agent/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
)

// NewAdmissionServer creates a http.Server for the mutating webhook admission handler.
func NewAdmissionServer(ctx context.Context, port string) *http.Server {
func NewAdmissionServer(ctx context.Context, port string) (*http.Server, error) {

Check warning on line 21 in src/internal/agent/http/server.go

View check run for this annotation

Codecov / codecov/patch

src/internal/agent/http/server.go#L21

Added line #L21 was not covered by tests
message.Debugf("http.NewAdmissionServer(%s)", port)

c, err := cluster.NewCluster()
if err != nil {
message.Fatalf(err, err.Error())
return nil, err

Check warning on line 26 in src/internal/agent/http/server.go

View check run for this annotation

Codecov / codecov/patch

src/internal/agent/http/server.go#L26

Added line #L26 was not covered by tests
}

// Routers
Expand All @@ -46,11 +46,12 @@
mux.Handle("/mutate/argocd-repository", admissionHandler.Serve(argocdRepositoryMutation))
mux.Handle("/metrics", promhttp.Handler())

return &http.Server{
srv := &http.Server{

Check warning on line 49 in src/internal/agent/http/server.go

View check run for this annotation

Codecov / codecov/patch

src/internal/agent/http/server.go#L49

Added line #L49 was not covered by tests
Addr: fmt.Sprintf(":%s", port),
Handler: mux,
ReadHeaderTimeout: 5 * time.Second, // Set ReadHeaderTimeout to avoid Slowloris attacks
}
return srv, nil

Check warning on line 54 in src/internal/agent/http/server.go

View check run for this annotation

Codecov / codecov/patch

src/internal/agent/http/server.go#L54

Added line #L54 was not covered by tests
}

// NewProxyServer creates and returns an http proxy server.
Expand Down
6 changes: 5 additions & 1 deletion src/internal/agent/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
// StartWebhook launches the Zarf agent mutating webhook in the cluster.
func StartWebhook(ctx context.Context) error {
message.Debug("agent.StartWebhook()")
return startServer(ctx, agentHttp.NewAdmissionServer(ctx, httpPort))
srv, err := agentHttp.NewAdmissionServer(ctx, httpPort)
if err != nil {
return err

Check warning on line 35 in src/internal/agent/start.go

View check run for this annotation

Codecov / codecov/patch

src/internal/agent/start.go#L33-L35

Added lines #L33 - L35 were not covered by tests
}
return startServer(ctx, srv)

Check warning on line 37 in src/internal/agent/start.go

View check run for this annotation

Codecov / codecov/patch

src/internal/agent/start.go#L37

Added line #L37 was not covered by tests
}

// StartHTTPProxy launches the zarf agent proxy in the cluster.
Expand Down
8 changes: 4 additions & 4 deletions src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
var saved string
temp := filepath.Join(h.chartPath, "temp")
if _, ok := cl.(loader.DirLoader); ok {
err = h.buildChartDependencies(spinner)
err = h.buildChartDependencies()

Check warning on line 85 in src/internal/packager/helm/repo.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/repo.go#L85

Added line #L85 was not covered by tests
if err != nil {
return fmt.Errorf("unable to build dependencies for the chart: %w", err)
}
Expand Down Expand Up @@ -157,7 +157,7 @@
if registry.IsOCI(h.chart.URL) {
regClient, err = registry.NewClient(registry.ClientOptEnableCache(true))
if err != nil {
spinner.Fatalf(err, "Unable to create a new registry client")
return fmt.Errorf("unable to create the new registry client: %w", err)

Check warning on line 160 in src/internal/packager/helm/repo.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/repo.go#L160

Added line #L160 was not covered by tests
}
chartURL = h.chart.URL
// Explicitly set the pull version for OCI
Expand Down Expand Up @@ -279,11 +279,11 @@
}

// buildChartDependencies builds the helm chart dependencies
func (h *Helm) buildChartDependencies(spinner *message.Spinner) error {
func (h *Helm) buildChartDependencies() error {

Check warning on line 282 in src/internal/packager/helm/repo.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/repo.go#L282

Added line #L282 was not covered by tests
// Download and build the specified dependencies
regClient, err := registry.NewClient(registry.ClientOptEnableCache(true))
if err != nil {
spinner.Fatalf(err, "Unable to create a new registry client")
return fmt.Errorf("unable to create a new registry client: %w", err)

Check warning on line 286 in src/internal/packager/helm/repo.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/repo.go#L286

Added line #L286 was not covered by tests
}

h.settings = cli.New()
Expand Down
12 changes: 10 additions & 2 deletions src/pkg/cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
state.Distro = distro

// Setup zarf agent PKI
state.AgentTLS = pki.GeneratePKI(config.ZarfAgentHost)
agentTLS, err := pki.GeneratePKI(config.ZarfAgentHost)
if err != nil {
return err

Check warning on line 83 in src/pkg/cluster/state.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/state.go#L83

Added line #L83 was not covered by tests
}
state.AgentTLS = agentTLS

namespaceList, err := c.Clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
Expand Down Expand Up @@ -359,7 +363,11 @@
}
}
if slices.Contains(services, message.AgentKey) {
newState.AgentTLS = pki.GeneratePKI(config.ZarfAgentHost)
agentTLS, err := pki.GeneratePKI(config.ZarfAgentHost)
if err != nil {
return nil, err

Check warning on line 368 in src/pkg/cluster/state.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/state.go#L368

Added line #L368 was not covered by tests
}
newState.AgentTLS = agentTLS
}

return &newState, nil
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/cluster/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,10 @@ func TestMergeZarfStateArtifact(t *testing.T) {
func TestMergeZarfStateAgent(t *testing.T) {
t.Parallel()

agentTLS, err := pki.GeneratePKI("example.com")
require.NoError(t, err)
oldState := &types.ZarfState{
AgentTLS: pki.GeneratePKI("example.com"),
AgentTLS: agentTLS,
}
newState, err := MergeZarfState(oldState, types.ZarfInitOptions{}, []string{message.AgentKey})
require.NoError(t, err)
Expand Down
19 changes: 0 additions & 19 deletions src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"net/http"
"os"
"runtime/debug"
"strings"
"time"

Expand Down Expand Up @@ -162,20 +161,6 @@ func WarnErrf(err any, format string, a ...any) {
Warnf(format, a...)
}

// Fatal prints a fatal error message and exits with a 1.
func Fatal(err any, message string) {
debugPrinter(2, err)
errorPrinter(2).Println(message)
debugPrinter(2, string(debug.Stack()))
os.Exit(1)
}

// Fatalf prints a fatal error message and exits with a 1 with a given format.
func Fatalf(err any, format string, a ...any) {
message := Paragraph(format, a...)
Fatal(err, message)
}

// Info prints an info message.
func Info(message string) {
Infof("%s", message)
Expand Down Expand Up @@ -351,7 +336,3 @@ func debugPrinter(offset int, a ...any) {
Println(a...)
}
}

func errorPrinter(offset int) *pterm.PrefixPrinter {
return pterm.Error.WithShowLineNumber(logLevel > 2).WithLineNumberOffset(offset)
}
15 changes: 0 additions & 15 deletions src/pkg/message/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,3 @@ func (p *Spinner) Errorf(err error, format string, a ...any) {
Warnf(format, a...)
debugPrinter(2, err)
}

// Fatal calls message.Fatalf with the given error.
func (p *Spinner) Fatal(err error) {
p.Fatalf(err, p.startText)
}

// Fatalf calls message.Fatalf with the given error and format.
func (p *Spinner) Fatalf(err error, format string, a ...any) {
if p.spinner != nil {
p.spinner.RemoveWhenDone = true
_ = p.spinner.Stop()
activeSpinner = nil
}
Fatalf(err, format, a...)
}
18 changes: 0 additions & 18 deletions src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,6 @@ func New(cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) {
return pkgr, nil
}

/*
NewOrDie creates a new package instance with the provided config or throws a fatal error.

Note: This function creates a tmp directory that should be cleaned up with p.ClearTempPaths().
*/
func NewOrDie(config *types.PackagerConfig, mods ...Modifier) *Packager {
var (
err error
pkgr *Packager
)

if pkgr, err = New(config, mods...); err != nil {
message.Fatalf(err, "Unable to setup the package config: %s", err.Error())
}

return pkgr
}

// setTempDirectory sets the temp directory for the packager.
func (p *Packager) setTempDirectory(path string) error {
dir, err := utils.MakeTempDir(path)
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
return nil
}()
if err != nil {
spinner.Fatalf(err, "Unable to create the zarf namespace")
return fmt.Errorf("unable to create the Zarf namespace: %w", err)

Check warning on line 475 in src/pkg/packager/deploy.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/deploy.go#L475

Added line #L475 was not covered by tests
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/pkg/packager/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ func TestGenerateValuesOverrides(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
p := NewOrDie(&types.PackagerConfig{DeployOpts: tt.deployOpts}, WithSource(&sources.TarballSource{}))
p, err := New(&types.PackagerConfig{DeployOpts: tt.deployOpts}, WithSource(&sources.TarballSource{}))
require.NoError(t, err)
for k, v := range tt.setVariables {
p.variableConfig.SetVariable(k, v, false, false, variables.RawVariableType)
}
Expand Down
Loading
Loading