diff --git a/src/cmd/destroy.go b/src/cmd/destroy.go index 28ceb1c632..6951efd96b 100644 --- a/src/cmd/destroy.go +++ b/src/cmd/destroy.go @@ -78,7 +78,7 @@ var destroyCmd = &cobra.Command{ // Try to remove the script, but ignore any errors and debug log them err = os.Remove(script) if err != nil { - message.Debug("Unable to remove script", "script", script, "error", err) + message.WarnErr(err, fmt.Sprintf("Unable to remove script. script=%s", script)) } } } else { diff --git a/src/cmd/dev.go b/src/cmd/dev.go index f4283522ed..ab0a262589 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -223,7 +223,7 @@ var devSha256SumCmd = &cobra.Command{ return errors.Join(hashErr, err) } fmt.Println(hash) - return err // Must return err for defer errors.Join + return nil }, } diff --git a/src/cmd/initialize.go b/src/cmd/initialize.go index 3b9f10c1f6..7316183e5d 100644 --- a/src/cmd/initialize.go +++ b/src/cmd/initialize.go @@ -129,7 +129,6 @@ func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, er return "", lang.ErrInitNotFound } - var confirmDownload bool url := zoci.GetInitPackageURL(config.CLIVersion) // Give the user the choice to download the init-package and note that this does require an internet connection @@ -137,15 +136,12 @@ func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, er message.Note(lang.CmdInitPullNote) - // Prompt the user if --confirm not specified - // REVIEW(mkcp): It looks like this condition can't be met - maybe --confirm was removed at some point? - if !confirmDownload { - prompt := &survey.Confirm{ - Message: lang.CmdInitPullConfirm, - } - if err := survey.AskOne(prompt, &confirmDownload); err != nil { - return "", fmt.Errorf("confirm download canceled: %w", err) - } + var confirmDownload bool + prompt := &survey.Confirm{ + Message: lang.CmdInitPullConfirm, + } + if err := survey.AskOne(prompt, &confirmDownload); err != nil { + return "", fmt.Errorf("confirm download canceled: %w", err) } // If the user wants to download the init-package, download it diff --git a/src/internal/gitea/gitea.go b/src/internal/gitea/gitea.go index e354399e55..48bfdfaae1 100644 --- a/src/internal/gitea/gitea.go +++ b/src/internal/gitea/gitea.go @@ -73,7 +73,7 @@ func (g *Client) DoRequest(ctx context.Context, method string, path string, body if err != nil { return nil, 0, err } - return b, resp.StatusCode, err // must return err for defer errors.Join + return b, resp.StatusCode, nil } // CreateReadOnlyUser creates a non-admin Zarf user. diff --git a/src/pkg/layout/package.go b/src/pkg/layout/package.go index 48d999e55a..7824d34ba9 100644 --- a/src/pkg/layout/package.go +++ b/src/pkg/layout/package.go @@ -161,7 +161,7 @@ func (pp *PackagePaths) MigrateLegacy() (err error) { } } - return err // must return err here for defer errors.Join + return nil } // IsLegacyLayout returns true if the package is using the legacy layout. diff --git a/src/pkg/layout/split.go b/src/pkg/layout/split.go index c8bd64274b..6afc82d398 100644 --- a/src/pkg/layout/split.go +++ b/src/pkg/layout/split.go @@ -23,6 +23,11 @@ func splitFile(srcPath string, chunkSize int) (err error) { if err != nil { return err } + defer func() { + err2 := srcFile.Close() + errors.Join(err, err2) + }() + fi, err := srcFile.Stat() if err != nil { return err @@ -66,10 +71,6 @@ func splitFile(srcPath string, chunkSize int) (err error) { if err != nil { return err } - err = dstFile.Close() - if err != nil { - return err - } // EOF error could be returned on 0 bytes written. if written == 0 { @@ -86,11 +87,7 @@ func splitFile(srcPath string, chunkSize int) (err error) { } } - // Remove original file - err = srcFile.Close() - if err != nil { - return err - } + // Done, lets cleanup the src err = os.Remove(srcPath) if err != nil { return err @@ -112,5 +109,5 @@ func splitFile(srcPath string, chunkSize int) (err error) { } progressBar.Successf("Package split across %d files", fileCount+1) - return err // must return err for defer errors.Join + return nil } diff --git a/src/pkg/utils/auth.go b/src/pkg/utils/auth.go index 93ca4f5f2d..04e4a7302b 100644 --- a/src/pkg/utils/auth.go +++ b/src/pkg/utils/auth.go @@ -55,7 +55,7 @@ func FindAuthForHost(baseURL string) (*Credential, error) { } // credentialParser parses a user's .git-credentials file to find git creds for hosts. -func credentialParser(path string) ([]Credential, error) { +func credentialParser(path string) (_ []Credential, err error) { file, err := os.Open(path) if errors.Is(err, os.ErrNotExist) { return nil, nil @@ -63,6 +63,10 @@ func credentialParser(path string) ([]Credential, error) { if err != nil { return nil, err } + defer func() { + err2 := file.Close() + errors.Join(err, err2) + }() var credentials []Credential scanner := bufio.NewScanner(file) @@ -81,10 +85,6 @@ func credentialParser(path string) ([]Credential, error) { } credentials = append(credentials, credential) } - err = file.Close() - if err != nil { - return nil, err - } return credentials, nil } diff --git a/src/pkg/utils/network.go b/src/pkg/utils/network.go index 78c3cd1e5c..5858431430 100644 --- a/src/pkg/utils/network.go +++ b/src/pkg/utils/network.go @@ -122,5 +122,5 @@ func httpGetFile(url string, destinationFile *os.File) (err error) { title = fmt.Sprintf("Downloaded %s", url) progressBar.Successf("%s", title) - return err // Must return err for defer errors.Join + return nil } diff --git a/src/pkg/variables/templates.go b/src/pkg/variables/templates.go index 74129c6c84..5f8c9b940c 100644 --- a/src/pkg/variables/templates.go +++ b/src/pkg/variables/templates.go @@ -139,5 +139,5 @@ func (vc *VariableConfig) ReplaceTextTemplate(path string) (err error) { if err != nil { return err } - return err // must return err for defer errors.Join + return nil } diff --git a/src/pkg/zoci/copier.go b/src/pkg/zoci/copier.go index 627843626c..597da7776e 100644 --- a/src/pkg/zoci/copier.go +++ b/src/pkg/zoci/copier.go @@ -58,5 +58,5 @@ func CopyPackage(ctx context.Context, src *Remote, dst *Remote, concurrency int) } src.Log().Info(fmt.Sprintf("Published %s to %s", src.Repo().Reference, dst.Repo().Reference)) - return err // must return err for defer errors.Join + return nil } diff --git a/src/pkg/zoci/pull.go b/src/pkg/zoci/pull.go index 270c0300d3..1984f87ffd 100644 --- a/src/pkg/zoci/pull.go +++ b/src/pkg/zoci/pull.go @@ -77,7 +77,7 @@ func (r *Remote) PullPackage(ctx context.Context, destinationDir string, concurr if err != nil { return nil, err } - return layersToPull, err // must return err for defer errors.Join + return layersToPull, nil } // LayersFromRequestedComponents returns the descriptors for the given components from the root manifest. diff --git a/src/pkg/zoci/push.go b/src/pkg/zoci/push.go index 609aa56bd9..5727849ae2 100644 --- a/src/pkg/zoci/push.go +++ b/src/pkg/zoci/push.go @@ -92,7 +92,7 @@ func (r *Remote) PublishPackage(ctx context.Context, pkg *v1alpha1.ZarfPackage, } progressBar.Successf("Published %s [%s]", r.Repo().Reference, ZarfLayerMediaTypeBlob) - return err // must return err for defer errors.Join + return nil } func annotationsFromMetadata(metadata *v1alpha1.ZarfMetadata) map[string]string {