You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/01/27 10:28:28 UTC

[camel-k] 02/02: Kamel dump: make the command smarter

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

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

commit 7c18c3303f97ad38f71c4fe1144008fbe58bbc87
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Jan 27 11:27:04 2022 +0100

    Kamel dump: make the command smarter
---
 pkg/cmd/dump.go          | 67 ++-------------------------------------
 pkg/cmd/util_compress.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 65 deletions(-)

diff --git a/pkg/cmd/dump.go b/pkg/cmd/dump.go
index 62055c6..04c53df 100644
--- a/pkg/cmd/dump.go
+++ b/pkg/cmd/dump.go
@@ -18,14 +18,12 @@ limitations under the License.
 package cmd
 
 import (
-	"archive/tar"
 	"bufio"
-	"compress/gzip"
 	"context"
 	"fmt"
 	"io"
-	"log"
 	"os"
+	"time"
 
 	"github.com/apache/camel-k/pkg/util"
 
@@ -73,19 +71,7 @@ func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) (err error) {
 				return dumpNamespace(o.Context, c, o.Namespace, file, o.LogLines)
 			} else {
 				dumpNamespace(o.Context, c, o.Namespace, file, o.LogLines)
-				// Create output file
-				out, err := os.Create("output.tar.gz")
-				if err != nil {
-					log.Fatalln("Error writing archive:", err)
-				}
-				defer out.Close()
-
-				// Create the archive and write the output to the "out" Writer
-				files := []string{file.Name()}
-				err = createArchive(files, out)
-				if err != nil {
-					log.Fatalln("Error creating archive:", err)
-				}
+				createTarFile([]string{file.Name()}, "dump." + file.Name() + "." + time.Now().Format(time.RFC3339) + ".tar.gz")
 				return nil
 			}
 		})
@@ -223,52 +209,3 @@ func dumpLogs(ctx context.Context, c client.Client, prefix string, ns string, na
 	}
 	return stream.Close()
 }
-
-func createArchive(files []string, buf io.Writer) error {
-	gw := gzip.NewWriter(buf)
-	defer gw.Close()
-	tw := tar.NewWriter(gw)
-	defer tw.Close()
-
-	// Iterate over files and add them to the tar archive
-	for _, file := range files {
-		err := addToArchive(tw, file)
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
-}
-
-func addToArchive(tw *tar.Writer, filename string) error {
-	file, err := os.Open(filename)
-	if err != nil {
-		return err
-	}
-	defer file.Close()
-
-	info, err := file.Stat()
-	if err != nil {
-		return err
-	}
-
-	header, err := tar.FileInfoHeader(info, info.Name())
-	if err != nil {
-		return err
-	}
-
-	header.Name = filename
-
-	err = tw.WriteHeader(header)
-	if err != nil {
-		return err
-	}
-
-	_, err = io.Copy(tw, file)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
diff --git a/pkg/cmd/util_compress.go b/pkg/cmd/util_compress.go
new file mode 100644
index 0000000..c4ae9f5
--- /dev/null
+++ b/pkg/cmd/util_compress.go
@@ -0,0 +1,82 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package cmd
+
+import (
+	"archive/tar"
+	"compress/gzip"
+	"io"
+	"log"
+	"os"
+)
+
+func createTarFile(fileNames []string, archiveName string) {
+	out, err := os.Create(archiveName)
+	if err != nil {
+		log.Fatalln("Error writing archive:", err)
+	}
+	defer out.Close()
+
+	err = createArchiveFile(fileNames, out)
+	if err != nil {
+		log.Fatalln("Error creating archive:", err)
+	}
+}
+
+func createArchiveFile(files []string, buf io.Writer) error {
+	gw := gzip.NewWriter(buf)
+	defer gw.Close()
+	tw := tar.NewWriter(gw)
+	defer tw.Close()
+
+	// Iterate over files and add them to the tar archive
+	for _, file := range files {
+		err := addEntryToArchive(tw, file)
+		if err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func addEntryToArchive(tw *tar.Writer, filename string) error {
+	file, err := os.Open(filename)
+	if err != nil {
+		return err
+	}
+	defer file.Close()
+	info, err := file.Stat()
+	if err != nil {
+		return err
+	}
+	header, err := tar.FileInfoHeader(info, info.Name())
+	if err != nil {
+		return err
+	}
+	header.Name = filename
+	err = tw.WriteHeader(header)
+	if err != nil {
+		return err
+	}
+	_, err = io.Copy(tw, file)
+	if err != nil {
+		return err
+	}
+	defer os.Remove(filename)
+	return nil
+}