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

Commit 3a8f6e0

Browse files
author
Priya Wadhwa
committed
Merge branch 'quiet-flag' of https://github.com/priyawadhwa/container-diff into quiet-flag
2 parents f92bed7 + 812447c commit 3a8f6e0

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var json bool
3939
var save bool
4040
var types diffTypes
4141
var noCache bool
42+
var quiet bool
4243

4344
var LogLevel string
4445

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

210211
func addSharedFlags(cmd *cobra.Command) {
211212
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.")
212214
cmd.Flags().VarP(&types, "type", "t", "This flag sets the list of analyzer types to use. Set it repeatedly to use multiple analyzers.")
213215
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
214216
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
@@ -112,7 +112,7 @@ func rpmDataFromContainer(image pkgutil.Image) (map[string]util.PackageInfo, err
112112

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

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"
@@ -100,7 +99,7 @@ type ConfigSchema struct {
10099
}
101100

102101
func getImage(p Prepper) (Image, error) {
103-
fmt.Fprintf(os.Stderr, "Retrieving image %s from source %s\n", p.GetSource(), p.Name())
102+
PrintToStdErr("Retrieving image %s from source %s\n", p.GetSource(), p.Name())
104103
imgPath, err := p.GetFileSystem()
105104
if err != nil {
106105
return Image{}, err

0 commit comments

Comments
 (0)