Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 812447c

Browse files
author
Priya Wadhwa
committed
Add quiet flag to suppress output to stderr
1 parent d29963e commit 812447c

File tree

9 files changed

+62
-48
lines changed

9 files changed

+62
-48
lines changed

cmd/analyze.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var analyzeCmd = &cobra.Command{
3838
return nil
3939
},
4040
Run: func(cmd *cobra.Command, args []string) {
41+
pkgutil.SetQuiet(quiet)
4142
if err := analyzeImage(args[0], types); err != nil {
4243
logrus.Error(err)
4344
os.Exit(1)
@@ -86,7 +87,7 @@ func analyzeImage(imageName string, analyzerArgs []string) error {
8687
return fmt.Errorf("Error performing image analysis: %s", err)
8788
}
8889

89-
fmt.Fprintln(os.Stderr, "Retrieving analyses")
90+
pkgutil.PrintToStdErr("Retrieving analyses")
9091
outputResults(analyses)
9192

9293
if save {

cmd/diff.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var diffCmd = &cobra.Command{
4242
return nil
4343
},
4444
Run: func(cmd *cobra.Command, args []string) {
45+
pkgutil.SetQuiet(quiet)
4546
if err := diffImages(args[0], args[1], types); err != nil {
4647
logrus.Error(err)
4748
os.Exit(1)
@@ -82,7 +83,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
8283
var wg sync.WaitGroup
8384
wg.Add(2)
8485

85-
fmt.Fprintf(os.Stderr, "Starting diff on images %s and %s, using differs: %s\n", image1Arg, image2Arg, diffArgs)
86+
pkgutil.PrintToStdErr("Starting diff on images %s and %s, using differs: %s\n", image1Arg, image2Arg, diffArgs)
8687

8788
imageMap := map[string]*pkgutil.Image{
8889
image1Arg: {},
@@ -112,7 +113,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
112113
defer pkgutil.CleanupImage(*imageMap[image2Arg])
113114
}
114115

115-
fmt.Fprintln(os.Stderr, "Computing diffs")
116+
pkgutil.PrintToStdErr("Computing diffs")
116117
req := differs.DiffRequest{
117118
Image1: *imageMap[image1Arg],
118119
Image2: *imageMap[image2Arg],
@@ -124,7 +125,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
124125
outputResults(diffs)
125126

126127
if filename != "" {
127-
fmt.Fprintln(os.Stderr, "Computing filename diffs")
128+
pkgutil.PrintToStdErr("Computing filename diffs")
128129
err := diffFile(imageMap[image1Arg], imageMap[image2Arg])
129130
if err != nil {
130131
return err

cmd/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var json bool
3939
var save bool
4040
var types diffTypes
4141
var noCache bool
42+
var quiet bool
4243

4344
var LogLevel string
44-
var format string
4545

4646
type validatefxn func(args []string) error
4747

@@ -85,7 +85,7 @@ func outputResults(resultMap map[string]util.Result) {
8585
if json {
8686
results[i] = result.OutputStruct()
8787
} else {
88-
err := result.OutputText(analyzerType, format)
88+
err := result.OutputText(analyzerType)
8989
if err != nil {
9090
logrus.Error(err)
9191
}
@@ -179,7 +179,6 @@ func cacheDir() (string, error) {
179179

180180
func init() {
181181
RootCmd.PersistentFlags().StringVarP(&LogLevel, "verbosity", "v", "warning", "This flag controls the verbosity of container-diff.")
182-
RootCmd.PersistentFlags().StringVarP(&format, "format", "", "", "Format to output diff in.")
183182
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
184183
}
185184

@@ -211,6 +210,7 @@ func (d *diffTypes) Type() string {
211210

212211
func addSharedFlags(cmd *cobra.Command) {
213212
cmd.Flags().BoolVarP(&json, "json", "j", false, "JSON Output defines if the diff should be returned in a human readable format (false) or a JSON (true).")
213+
cmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "Suppress output to stderr.")
214214
cmd.Flags().VarP(&types, "type", "t", "This flag sets the list of analyzer types to use. Set it repeatedly to use multiple analyzers.")
215215
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
216216
cmd.Flags().BoolVarP(&util.SortSize, "order", "o", false, "Set this flag to sort any file/package results by descending size. Otherwise, they will be sorted by name.")

differs/rpm_diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func rpmDataFromContainer(image pkgutil.Image) (map[string]util.PackageInfo, err
111111

112112
archive, err := generateNewArchive(imageName)
113113
if err != nil {
114-
fmt.Println(err.Error())
114+
logrus.Errorf(err.Error())
115115
}
116116
defer os.Remove(archive)
117117

pkg/util/cmd_utils.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2017 Google, Inc. All rights reserved.
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 util
18+
19+
import (
20+
"fmt"
21+
"os"
22+
)
23+
24+
var quiet = false
25+
26+
func PrintToStdErr(output string, vars ...interface{}) {
27+
if !quiet {
28+
fmt.Fprintf(os.Stderr, output, vars...)
29+
}
30+
}
31+
32+
func SetQuiet(q bool) {
33+
quiet = q
34+
}

pkg/util/image_prep_utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"archive/tar"
2121
"encoding/json"
2222
"errors"
23-
"fmt"
2423
"io"
2524
"io/ioutil"
2625
"os"
@@ -65,7 +64,7 @@ type ConfigSchema struct {
6564
}
6665

6766
func getImage(p Prepper) (Image, error) {
68-
fmt.Fprintf(os.Stderr, "Retrieving image %s from source %s\n", p.GetSource(), p.Name())
67+
PrintToStdErr("Retrieving image %s from source %s\n", p.GetSource(), p.Name())
6968
imgPath, err := p.GetFileSystem()
7069
if err != nil {
7170
return Image{}, err

util/analyze_output_utils.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
type Result interface {
2828
OutputStruct() interface{}
29-
OutputText(resultType string, format string) error
29+
OutputText(resultType string) error
3030
}
3131

3232
type AnalyzeResult struct {
@@ -41,15 +41,15 @@ func (r ListAnalyzeResult) OutputStruct() interface{} {
4141
return r
4242
}
4343

44-
func (r ListAnalyzeResult) OutputText(resultType string, format string) error {
44+
func (r ListAnalyzeResult) OutputText(resultType string) error {
4545
analysis, valid := r.Analysis.([]string)
4646
if !valid {
4747
logrus.Error("Unexpected structure of Analysis. Should be of type []string")
4848
return fmt.Errorf("Could not output %s analysis result", r.AnalyzeType)
4949
}
5050
r.Analysis = analysis
51-
return TemplateOutputFromFormat(r, "ListAnalyze", format)
5251

52+
return TemplateOutput(r, "ListAnalyze")
5353
}
5454

5555
type MultiVersionPackageAnalyzeResult AnalyzeResult
@@ -73,7 +73,7 @@ func (r MultiVersionPackageAnalyzeResult) OutputStruct() interface{} {
7373
return output
7474
}
7575

76-
func (r MultiVersionPackageAnalyzeResult) OutputText(resultType string, format string) error {
76+
func (r MultiVersionPackageAnalyzeResult) OutputText(resultType string) error {
7777
analysis, valid := r.Analysis.(map[string]map[string]PackageInfo)
7878
if !valid {
7979
logrus.Error("Unexpected structure of Analysis. Should be of type map[string]map[string]PackageInfo")
@@ -91,7 +91,7 @@ func (r MultiVersionPackageAnalyzeResult) OutputText(resultType string, format s
9191
AnalyzeType: r.AnalyzeType,
9292
Analysis: strAnalysis,
9393
}
94-
return TemplateOutputFromFormat(strResult, "MultiVersionPackageAnalyze", format)
94+
return TemplateOutput(strResult, "MultiVersionPackageAnalyze")
9595
}
9696

9797
type SingleVersionPackageAnalyzeResult AnalyzeResult
@@ -115,7 +115,7 @@ func (r SingleVersionPackageAnalyzeResult) OutputStruct() interface{} {
115115
return output
116116
}
117117

118-
func (r SingleVersionPackageAnalyzeResult) OutputText(diffType string, format string) error {
118+
func (r SingleVersionPackageAnalyzeResult) OutputText(diffType string) error {
119119
analysis, valid := r.Analysis.(map[string]PackageInfo)
120120
if !valid {
121121
logrus.Error("Unexpected structure of Analysis. Should be of type map[string]PackageInfo")
@@ -133,7 +133,7 @@ func (r SingleVersionPackageAnalyzeResult) OutputText(diffType string, format st
133133
AnalyzeType: r.AnalyzeType,
134134
Analysis: strAnalysis,
135135
}
136-
return TemplateOutputFromFormat(strResult, "SingleVersionPackageAnalyze", format)
136+
return TemplateOutput(strResult, "SingleVersionPackageAnalyze")
137137
}
138138

139139
type PackageOutput struct {
@@ -191,7 +191,7 @@ func (r FileAnalyzeResult) OutputStruct() interface{} {
191191
return r
192192
}
193193

194-
func (r FileAnalyzeResult) OutputText(analyzeType string, format string) error {
194+
func (r FileAnalyzeResult) OutputText(analyzeType string) error {
195195
analysis, valid := r.Analysis.([]util.DirectoryEntry)
196196
if !valid {
197197
logrus.Error("Unexpected structure of Analysis. Should be of type []DirectoryEntry")
@@ -214,5 +214,5 @@ func (r FileAnalyzeResult) OutputText(analyzeType string, format string) error {
214214
AnalyzeType: r.AnalyzeType,
215215
Analysis: strAnalysis,
216216
}
217-
return TemplateOutputFromFormat(strResult, "FileAnalyze", format)
217+
return TemplateOutput(strResult, "FileAnalyze")
218218
}

util/diff_output_utils.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (r MultiVersionPackageDiffResult) OutputStruct() interface{} {
5252
return r
5353
}
5454

55-
func (r MultiVersionPackageDiffResult) OutputText(diffType string, format string) error {
55+
func (r MultiVersionPackageDiffResult) OutputText(diffType string) error {
5656
diff, valid := r.Diff.(MultiVersionPackageDiff)
5757
if !valid {
5858
logrus.Error("Unexpected structure of Diff. Should follow the MultiVersionPackageDiff struct")
@@ -84,7 +84,7 @@ func (r MultiVersionPackageDiffResult) OutputText(diffType string, format string
8484
InfoDiff: strInfoDiff,
8585
},
8686
}
87-
return TemplateOutputFromFormat(strResult, "MultiVersionPackageDiff", format)
87+
return TemplateOutput(strResult, "MultiVersionPackageDiff")
8888
}
8989

9090
func getMultiVersionInfoDiffOutput(infoDiff []MultiVersionInfo) []MultiVersionInfo {
@@ -118,7 +118,7 @@ func (r SingleVersionPackageDiffResult) OutputStruct() interface{} {
118118
return r
119119
}
120120

121-
func (r SingleVersionPackageDiffResult) OutputText(diffType string, format string) error {
121+
func (r SingleVersionPackageDiffResult) OutputText(diffType string) error {
122122
diff, valid := r.Diff.(PackageDiff)
123123
if !valid {
124124
logrus.Error("Unexpected structure of Diff. Should follow the PackageDiff struct")
@@ -150,7 +150,7 @@ func (r SingleVersionPackageDiffResult) OutputText(diffType string, format strin
150150
InfoDiff: strInfoDiff,
151151
},
152152
}
153-
return TemplateOutputFromFormat(strResult, "SingleVersionPackageDiff", format)
153+
return TemplateOutput(strResult, "SingleVersionPackageDiff")
154154
}
155155

156156
func getSingleVersionInfoDiffOutput(infoDiff []Info) []Info {
@@ -168,8 +168,8 @@ func (r HistDiffResult) OutputStruct() interface{} {
168168
return r
169169
}
170170

171-
func (r HistDiffResult) OutputText(diffType string, format string) error {
172-
return TemplateOutputFromFormat(r, "HistDiff", format)
171+
func (r HistDiffResult) OutputText(diffType string) error {
172+
return TemplateOutput(r, "HistDiff")
173173
}
174174

175175
type DirDiffResult DiffResult
@@ -185,7 +185,7 @@ func (r DirDiffResult) OutputStruct() interface{} {
185185
return r
186186
}
187187

188-
func (r DirDiffResult) OutputText(diffType string, format string) error {
188+
func (r DirDiffResult) OutputText(diffType string) error {
189189
diff, valid := r.Diff.(DirDiff)
190190
if !valid {
191191
logrus.Error("Unexpected structure of Diff. Should follow the DirDiff struct")
@@ -218,5 +218,5 @@ func (r DirDiffResult) OutputText(diffType string, format string) error {
218218
Mods: strMods,
219219
},
220220
}
221-
return TemplateOutputFromFormat(strResult, "DirDiff", format)
221+
return TemplateOutput(strResult, "DirDiff")
222222
}

util/format_utils.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,3 @@ func TemplateOutput(diff interface{}, templateType string) error {
7979
w.Flush()
8080
return nil
8181
}
82-
83-
func TemplateOutputFromFormat(diff interface{}, templateType string, format string) error {
84-
if format == "" {
85-
return TemplateOutput(diff, templateType)
86-
}
87-
funcs := template.FuncMap{"join": strings.Join}
88-
tmpl, err := template.New("tmpl").Funcs(funcs).Parse(format)
89-
if err != nil {
90-
logrus.Info("User specified format resulted in error, printing default output.")
91-
logrus.Error(err)
92-
return TemplateOutput(diff, templateType)
93-
}
94-
w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0)
95-
err = tmpl.Execute(w, diff)
96-
if err != nil {
97-
logrus.Error(err)
98-
return err
99-
}
100-
w.Flush()
101-
return nil
102-
}

0 commit comments

Comments
 (0)