Skip to content

Commit

Permalink
fix: error formatting and comparison and enable errorlint (zarf-dev#2771
Browse files Browse the repository at this point in the history
)

Signed-off-by: Philip Laine <philip.laine@gmail.com>
Signed-off-by: Tim Seagren <timseagren@defenseunicorns.com>
  • Loading branch information
phillebaba authored and chaospuppy committed Aug 5, 2024
1 parent 14fc355 commit b3eb66b
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 33 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ linters:
- nolintlint
- testifylint
- whitespace
- errorlint
linters-settings:
govet:
enable-all: true
Expand Down
14 changes: 6 additions & 8 deletions src/cmd/common/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package common

import (
"errors"
"os"
"strings"

Expand Down Expand Up @@ -166,16 +167,13 @@ func printViperConfigUsed() {
if !vInitialized {
return
}

// Optional, so ignore file not found errors
if vConfigError != nil {
// Config file not found; ignore
if _, ok := vConfigError.(viper.ConfigFileNotFoundError); !ok {
message.WarnErrf(vConfigError, lang.CmdViperErrLoadingConfigFile, vConfigError.Error())
}
} else {
message.Notef(lang.CmdViperInfoUsingConfigFile, v.ConfigFileUsed())
var notFoundErr *viper.ConfigFileNotFoundError
if vConfigError != nil && !errors.As(vConfigError, &notFoundErr) {
message.WarnErrf(vConfigError, lang.CmdViperErrLoadingConfigFile, vConfigError.Error())
return
}
message.Notef(lang.CmdViperInfoUsingConfigFile, v.ConfigFileUsed())
}

func setDefaults() {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c type
validVersionResponse, err := isValidVersion(cfg.Version)

if err != nil {
return c, fmt.Errorf("invalid Big Bang version: %s, parsing issue %s", cfg.Version, err)
return c, fmt.Errorf("could not parse the Big Bang version %s: %w", cfg.Version, err)
}

// Make sure the version is valid.
if !validVersionResponse {
return c, fmt.Errorf("invalid Big Bang version: %s, must be at least %s", cfg.Version, bbMinRequiredVersion)
return c, fmt.Errorf("Big Bang version %s must be at least %s", cfg.Version, bbMinRequiredVersion)
}

// Print the banner for Big Bang.
Expand Down
6 changes: 3 additions & 3 deletions src/internal/packager/helm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (h *Helm) TemplateChart(ctx context.Context) (manifest string, chartValues
if h.kubeVersion != "" {
parsedKubeVersion, err := chartutil.ParseKubeVersion(h.kubeVersion)
if err != nil {
return "", nil, fmt.Errorf("invalid kube version '%s': %s", h.kubeVersion, err)
return "", nil, fmt.Errorf("invalid kube version %s: %w", h.kubeVersion, err)
}
client.KubeVersion = parsedKubeVersion
}
Expand Down Expand Up @@ -392,13 +392,13 @@ func (h *Helm) migrateDeprecatedAPIs(latestRelease *release.Release) error {
// parse to unstructured to have access to more data than just the name
rawData := &unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(resource.Content), rawData); err != nil {
return fmt.Errorf("failed to unmarshal manifest: %#v", err)
return fmt.Errorf("failed to unmarshal manifest: %w", err)
}

rawData, manifestModified, _ := handleDeprecations(rawData, *kubeGitVersion)
manifestContent, err := yaml.Marshal(rawData)
if err != nil {
return fmt.Errorf("failed to marshal raw manifest after deprecation check: %#v", err)
return fmt.Errorf("failed to marshal raw manifest after deprecation check: %w", err)
}

// If this is not a bad object, place it back into the manifest
Expand Down
2 changes: 1 addition & 1 deletion src/internal/packager/helm/post-render.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti
// parse to unstructured to have access to more data than just the name
rawData := &unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(resource.Content), rawData); err != nil {
return fmt.Errorf("failed to unmarshal manifest: %#v", err)
return fmt.Errorf("failed to unmarshal manifest: %w", err)
}

switch rawData.GetKind() {
Expand Down
15 changes: 9 additions & 6 deletions src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package helm

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -307,18 +308,20 @@ func (h *Helm) buildChartDependencies() error {

// Build the deps from the helm chart
err = man.Build()
if e, ok := err.(downloader.ErrRepoNotFound); ok {
var notFoundErr *downloader.ErrRepoNotFound
if errors.As(err, &notFoundErr) {
// If we encounter a repo not found error point the user to `zarf tools helm repo add`
message.Warnf("%s. Please add the missing repo(s) via the following:", e.Error())
for _, repository := range e.Repos {
message.Warnf("%s. Please add the missing repo(s) via the following:", notFoundErr.Error())
for _, repository := range notFoundErr.Repos {
message.ZarfCommand(fmt.Sprintf("tools helm repo add <your-repo-name> %s", repository))
}
} else if err != nil {
// Warn the user of any issues but don't fail - any actual issues will cause a fail during packaging (e.g. the charts we are building may exist already, we just can't get updates)
return err
}
if err != nil {
message.ZarfCommand("tools helm dependency build --verify")
message.Warnf("Unable to perform a rebuild of Helm dependencies: %s", err.Error())
return err
}

return nil
}

Expand Down
6 changes: 0 additions & 6 deletions src/pkg/layout/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ type Components struct {
// ErrNotLoaded is returned when a path is not loaded.
var ErrNotLoaded = fmt.Errorf("not loaded")

// IsNotLoaded checks if an error is ErrNotLoaded.
func IsNotLoaded(err error) bool {
u, ok := err.(*fs.PathError)
return ok && u.Unwrap() == ErrNotLoaded
}

// Archive archives a component.
func (c *Components) Archive(component types.ZarfComponent, cleanupTemp bool) (err error) {
name := component.Name
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/sources/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s *OCISource) LoadPackage(ctx context.Context, dst *layout.PackagePaths, f
if unarchiveAll {
for _, component := range pkg.Components {
if err := dst.Components.Unarchive(component); err != nil {
if layout.IsNotLoaded(err) {
if errors.Is(err, layout.ErrNotLoaded) {
_, err := dst.Components.Create(component)
if err != nil {
return pkg, nil, err
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/sources/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *SplitTarballSource) Collect(_ context.Context, dir string) (string, err
pattern := strings.Replace(s.PackageSource, ".part000", ".part*", 1)
fileList, err := filepath.Glob(pattern)
if err != nil {
return "", fmt.Errorf("unable to find split tarball files: %s", err)
return "", fmt.Errorf("unable to find split tarball files: %w", err)
}

// Ensure the files are in order so they are appended in the correct order
Expand All @@ -46,7 +46,7 @@ func (s *SplitTarballSource) Collect(_ context.Context, dir string) (string, err
// Create the new package
pkgFile, err := os.Create(reassembled)
if err != nil {
return "", fmt.Errorf("unable to create new package file: %s", err)
return "", fmt.Errorf("unable to create new package file: %w", err)
}
defer pkgFile.Close()

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/sources/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *TarballSource) LoadPackage(_ context.Context, dst *layout.PackagePaths,
if unarchiveAll {
for _, component := range pkg.Components {
if err := dst.Components.Unarchive(component); err != nil {
if layout.IsNotLoaded(err) {
if errors.Is(err, layout.ErrNotLoaded) {
_, err := dst.Components.Create(component)
if err != nil {
return pkg, nil, err
Expand Down
7 changes: 4 additions & 3 deletions src/pkg/utils/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package utils

import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -196,7 +197,7 @@ func SplitYAML(yamlData []byte) ([]*unstructured.Unstructured, error) {
for _, yml := range ymls {
u := &unstructured.Unstructured{}
if err := k8syaml.Unmarshal([]byte(yml), u); err != nil {
return objs, fmt.Errorf("failed to unmarshal manifest: %#v", err)
return objs, fmt.Errorf("failed to unmarshal manifest: %w", err)
}
objs = append(objs, u)
}
Expand All @@ -216,10 +217,10 @@ func SplitYAMLToString(yamlData []byte) ([]string, error) {
for {
ext := runtime.RawExtension{}
if err := d.Decode(&ext); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return objs, fmt.Errorf("failed to unmarshal manifest: %#v", err)
return objs, fmt.Errorf("failed to unmarshal manifest: %w", err)
}
ext.Raw = bytes.TrimSpace(ext.Raw)
if len(ext.Raw) == 0 || bytes.Equal(ext.Raw, []byte("null")) {
Expand Down

0 comments on commit b3eb66b

Please sign in to comment.