This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push: new 76f8797 Kamel dump: make the command smarter 76f8797 is described below commit 76f87978d1563bbdb77c49da26e199703bd2838d Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Mon Jan 24 18:41:55 2022 +0100 Kamel dump: make the command smarter --- pkg/cmd/dump.go | 21 ++++++++--- pkg/util/tar/util_compress.go | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/dump.go b/pkg/cmd/dump.go index 902a167..a92f7f4 100644 --- a/pkg/cmd/dump.go +++ b/pkg/cmd/dump.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "os" + "time" "github.com/apache/camel-k/pkg/util" @@ -33,6 +34,7 @@ import ( "github.com/apache/camel-k/pkg/client" "github.com/apache/camel-k/pkg/client/camel/clientset/versioned" "github.com/apache/camel-k/pkg/util/kubernetes" + "github.com/apache/camel-k/pkg/util/tar" ) func newCmdDump(rootCmdOptions *RootCmdOptions) (*cobra.Command, *dumpCmdOptions) { @@ -48,12 +50,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"` + LogLines int `mapstructure:"logLines"` + Compressed bool `mapstructure:"compressed" yaml:",omitempty"` } func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) (err error) { @@ -64,13 +68,20 @@ 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) + } + err = dumpNamespace(o.Context, c, o.Namespace, file, o.LogLines) + if err != nil { + return err + } + tar.CreateTarFile([]string{file.Name()}, "dump."+file.Name()+"."+time.Now().Format(time.RFC3339)+".tar.gz") + return nil }) } else { - err = dumpNamespace(o.Context, c, o.Namespace, cmd.OutOrStdout(), o.LogLines) + return dumpNamespace(o.Context, c, o.Namespace, cmd.OutOrStdout(), o.LogLines) } - - return + return nil } func dumpNamespace(ctx context.Context, c client.Client, ns string, out io.Writer, logLines int) error { diff --git a/pkg/util/tar/util_compress.go b/pkg/util/tar/util_compress.go new file mode 100644 index 0000000..c3bcbc0 --- /dev/null +++ b/pkg/util/tar/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 tar + +import ( + "archive/tar" + "compress/gzip" + "fmt" + "io" + "os" +) + +func CreateTarFile(fileNames []string, archiveName string) { + out, err := os.Create(archiveName) + if err != nil { + fmt.Printf("Error writing archive: %v", err) + } + defer out.Close() + + err = createArchiveFile(fileNames, out) + if err != nil { + fmt.Printf("Error writing archive: %v", 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 +}