|
| 1 | +/* |
| 2 | +Copyright 2021 VMware Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package app |
| 18 | + |
| 19 | +import ( |
| 20 | + "archive/tar" |
| 21 | + "compress/gzip" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + |
| 26 | + "github.com/vmware-tanzu/sonobuoy/pkg/errlog" |
| 27 | + |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +func NewCmdSplat() *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "splat AGGREGATOR_RESULTS_PATH", |
| 34 | + Short: "Reads all tarballs in the specified directory and prints the content to STDOUT (for internal use)", |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + if err := runSplat(args[0]); err != nil { |
| 37 | + errlog.LogError(err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + }, |
| 41 | + Hidden: true, |
| 42 | + Args: cobra.ExactArgs(1), |
| 43 | + } |
| 44 | + return cmd |
| 45 | +} |
| 46 | + |
| 47 | +func runSplat(dirPath string) error { |
| 48 | + sonobuoyResults, err := filepath.Glob(filepath.Join(dirPath, "*.tar.gz")) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + if err = loadResults(os.Stdout, sonobuoyResults); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func loadResults(w *os.File, filenames []string) error { |
| 61 | + gzipWriter := gzip.NewWriter(w) |
| 62 | + defer gzipWriter.Close() |
| 63 | + |
| 64 | + tarWriter := tar.NewWriter(gzipWriter) |
| 65 | + defer tarWriter.Close() |
| 66 | + |
| 67 | + for _, filename := range filenames { |
| 68 | + if err := addFileToTarball(tarWriter, filename); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func addFileToTarball(tarWriter *tar.Writer, filename string) error { |
| 77 | + file, err := os.Open(filename) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + defer file.Close() |
| 82 | + |
| 83 | + infoHeader, err := file.Stat() |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + // archive path of content as basename instead of full path |
| 89 | + // so un-archiving will result in the working directory, not AGGREGATOR_RESULTS_PATH |
| 90 | + header, err := tar.FileInfoHeader(infoHeader, infoHeader.Name()) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + if err = tarWriter.WriteHeader(header); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + fileContent, err := ioutil.ReadAll(file) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + if _, err := tarWriter.Write(fileContent); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
0 commit comments