You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2024/05/09 10:08:57 UTC

(camel-k) 02/02: chore: cleanup pkg/util/tar package

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 74139ed8bbb271534c871b4ab9c77fff511cfecc
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Wed May 8 16:07:46 2024 +0200

    chore: cleanup pkg/util/tar package
---
 pkg/cmd/dump.go               |  2 +-
 pkg/util/tar/util_compress.go | 24 +++++++++++++++---------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/pkg/cmd/dump.go b/pkg/cmd/dump.go
index d9351d7f0..187b73f1a 100644
--- a/pkg/cmd/dump.go
+++ b/pkg/cmd/dump.go
@@ -75,7 +75,7 @@ func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) error {
 			if err != nil {
 				return err
 			}
-			tar.CreateTarFile([]string{file.Name()}, "dump."+file.Name()+"."+time.Now().Format(time.RFC3339)+".tar.gz", cmd)
+			tar.CreateTarFile([]string{file.Name()}, "dump."+file.Name()+"."+time.Now().Format(time.RFC3339)+".tar.gz", cmd.OutOrStdout())
 			return nil
 		})
 	} else {
diff --git a/pkg/util/tar/util_compress.go b/pkg/util/tar/util_compress.go
index 3388d124a..9d97e4e50 100644
--- a/pkg/util/tar/util_compress.go
+++ b/pkg/util/tar/util_compress.go
@@ -24,27 +24,27 @@ import (
 	"io"
 	"os"
 
-	"github.com/spf13/cobra"
+	"github.com/apache/camel-k/v2/pkg/util"
 )
 
-func CreateTarFile(fileNames []string, archiveName string, cmd *cobra.Command) {
+func CreateTarFile(fileNames []string, archiveName string, buf io.Writer) {
 	out, err := os.Create(archiveName)
 	if err != nil {
-		fmt.Fprintln(cmd.ErrOrStderr(), "Error writing archive:", err.Error())
+		_, _ = fmt.Fprintln(buf, "Error writing archive:", err.Error())
 	}
-	defer out.Close()
+	defer util.CloseQuietly(out)
 
 	err = createArchiveFile(fileNames, out)
 	if err != nil {
-		fmt.Fprintln(cmd.ErrOrStderr(), "Error writing archive:", err.Error())
+		_, _ = fmt.Fprintln(buf, "Error writing archive:", err.Error())
 	}
 }
 
 func createArchiveFile(files []string, buf io.Writer) error {
 	gw := gzip.NewWriter(buf)
-	defer gw.Close()
+	defer util.CloseQuietly(gw)
 	tw := tar.NewWriter(gw)
-	defer tw.Close()
+	defer util.CloseQuietly(tw)
 
 	// Iterate over files and add them to the tar archive
 	for _, file := range files {
@@ -61,7 +61,8 @@ func addEntryToArchive(tw *tar.Writer, filename string) error {
 	if err != nil {
 		return err
 	}
-	defer file.Close()
+	defer util.CloseQuietly(file)
+
 	info, err := file.Stat()
 	if err != nil {
 		return err
@@ -79,6 +80,11 @@ func addEntryToArchive(tw *tar.Writer, filename string) error {
 	if err != nil {
 		return err
 	}
-	defer os.Remove(filename)
+
+	err = os.Remove(filename)
+	if err != nil {
+		return err
+	}
+
 	return nil
 }