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

Commit 0dcfe7a

Browse files
author
priyawadhwa
authored
Merge pull request #175 from priyawadhwa/quiet-flag
Add quiet flag to suppress stderr output
2 parents 6c0e468 + 80ad2ab commit 0dcfe7a

File tree

9 files changed

+63
-7
lines changed

9 files changed

+63
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ To order files and packages by size (in descending order) when performing file s
111111
container-diff analyze remote://gcr.io/gcp-runtimes/multi-modified --type=pip --order
112112
```
113113

114+
To suppress output to stderr, add a `-q` or `--quiet` flag.
115+
```shell
116+
container-diff analyze file1.tar --type=file --quiet
117+
```
114118

115119
## Analysis Result Format
116120

cmd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
importpath = "github.com/GoogleCloudPlatform/container-diff/cmd",
1212
visibility = ["//visibility:public"],
1313
deps = [
14+
"//cmd/util/output:go_default_library",
1415
"//differs:go_default_library",
1516
"//pkg/cache:go_default_library",
1617
"//pkg/util:go_default_library",

cmd/analyze.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323

24+
"github.com/GoogleCloudPlatform/container-diff/cmd/util/output"
2425
"github.com/GoogleCloudPlatform/container-diff/differs"
2526
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2627
"github.com/sirupsen/logrus"
@@ -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+
output.PrintToStdErr("Retrieving analyses")
9091
outputResults(analyses)
9192

9293
if save {
@@ -99,4 +100,5 @@ func analyzeImage(imageName string, analyzerArgs []string) error {
99100
func init() {
100101
RootCmd.AddCommand(analyzeCmd)
101102
addSharedFlags(analyzeCmd)
103+
output.AddFlags(analyzeCmd)
102104
}

cmd/diff.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"sync"
2424

25+
"github.com/GoogleCloudPlatform/container-diff/cmd/util/output"
2526
"github.com/GoogleCloudPlatform/container-diff/differs"
2627
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2728
"github.com/GoogleCloudPlatform/container-diff/util"
@@ -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+
output.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+
output.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+
output.PrintToStdErr("Computing filename diffs")
128129
err := diffFile(imageMap[image1Arg], imageMap[image2Arg])
129130
if err != nil {
130131
return err
@@ -151,4 +152,5 @@ func init() {
151152
diffCmd.Flags().StringVarP(&filename, "filename", "f", "", "Set this flag to the path of a file in both containers to view the diff of the file. Must be used with --types=file flag.")
152153
RootCmd.AddCommand(diffCmd)
153154
addSharedFlags(diffCmd)
155+
output.AddFlags(diffCmd)
154156
}

cmd/util/output/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["output.go"],
6+
importpath = "github.com/GoogleCloudPlatform/container-diff/cmd/util/output",
7+
visibility = ["//visibility:public"],
8+
deps = ["//vendor/github.com/spf13/cobra:go_default_library"],
9+
)

cmd/util/output/output.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 output
18+
19+
import (
20+
"fmt"
21+
"github.com/spf13/cobra"
22+
"os"
23+
)
24+
25+
var quiet bool
26+
27+
// PrintToStdErr prints to stderr if quiet flag isn't enabled
28+
func PrintToStdErr(output string, vars ...interface{}) {
29+
if !quiet {
30+
fmt.Fprintf(os.Stderr, output, vars...)
31+
}
32+
}
33+
34+
// AddFlags adds quiet flag to suppress output to stderr
35+
func AddFlags(cmd *cobra.Command) {
36+
cmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "Suppress output to stderr.")
37+
}

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/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
importpath = "github.com/GoogleCloudPlatform/container-diff/pkg/util",
1616
visibility = ["//visibility:public"],
1717
deps = [
18+
"//cmd/util/output:go_default_library",
1819
"//pkg/cache:go_default_library",
1920
"//vendor/github.com/containers/image/docker:go_default_library",
2021
"//vendor/github.com/containers/image/docker/daemon:go_default_library",

pkg/util/image_prep_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
"archive/tar"
2121
"encoding/json"
2222
"errors"
23-
"fmt"
2423
"io"
2524
"io/ioutil"
2625
"os"
2726
"strings"
2827

28+
"github.com/GoogleCloudPlatform/container-diff/cmd/util/output"
2929
"github.com/GoogleCloudPlatform/container-diff/pkg/cache"
3030
"github.com/containers/image/docker"
3131
"github.com/containers/image/manifest"
@@ -100,7 +100,7 @@ type ConfigSchema struct {
100100
}
101101

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

0 commit comments

Comments
 (0)