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:26 UTC

[camel-k] branch 2189 created (now 7c18c33)

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

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


      at 7c18c33  Kamel dump: make the command smarter

This branch includes the following new commits:

     new cfff11e  Smarter dump
     new 7c18c33  Kamel dump: make the command smarter

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[camel-k] 01/02: Smarter dump

Posted by ac...@apache.org.
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 cfff11e375ef1f9dcc8ca96517674b2d3ce87e6d
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Mon Jan 24 18:41:55 2022 +0100

    Smarter dump
---
 pkg/cmd/dump.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 73 insertions(+), 1 deletion(-)

diff --git a/pkg/cmd/dump.go b/pkg/cmd/dump.go
index 902a167..62055c6 100644
--- a/pkg/cmd/dump.go
+++ b/pkg/cmd/dump.go
@@ -18,10 +18,13 @@ limitations under the License.
 package cmd
 
 import (
+	"archive/tar"
 	"bufio"
+	"compress/gzip"
 	"context"
 	"fmt"
 	"io"
+	"log"
 	"os"
 
 	"github.com/apache/camel-k/pkg/util"
@@ -48,12 +51,14 @@ func newCmdDump(rootCmdOptions *RootCmdOptions) (*cobra.Command, *dumpCmdOptions
 	}
 
 	cmd.Flags().Int("logLines", 100, "Number of log lines to dump")
+	cmd.Flags().Bool("compressed", false, "If the log file must be compressed in a tar.")
 	return &cmd, &options
 }
 
 type dumpCmdOptions struct {
 	*RootCmdOptions
 	LogLines int `mapstructure:"logLines"`
+	Compressed bool `mapstructure:"compressed" yaml:",omitempty"`
 }
 
 func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) (err error) {
@@ -64,7 +69,25 @@ func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) (err error) {
 
 	if len(args) == 1 {
 		err = util.WithFile(args[0], os.O_RDWR|os.O_CREATE, 0o644, func(file *os.File) error {
-			return dumpNamespace(o.Context, c, o.Namespace, file, o.LogLines)
+			if (!o.Compressed) {
+				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)
+				}
+				return nil
+			}
 		})
 	} else {
 		err = dumpNamespace(o.Context, c, o.Namespace, cmd.OutOrStdout(), o.LogLines)
@@ -200,3 +223,52 @@ 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
+}

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

Posted by ac...@apache.org.
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
+}