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:27 UTC
[camel-k] 01/02: Smarter dump
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
+}