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

Commit 4a0b8fe

Browse files
author
Priya Wadhwa
committed
Moved quiet flag into output
1 parent 3a8f6e0 commit 4a0b8fe

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
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/analyze.go

Lines changed: 3 additions & 2 deletions
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"
@@ -38,7 +39,6 @@ var analyzeCmd = &cobra.Command{
3839
return nil
3940
},
4041
Run: func(cmd *cobra.Command, args []string) {
41-
pkgutil.SetQuiet(quiet)
4242
if err := analyzeImage(args[0], types); err != nil {
4343
logrus.Error(err)
4444
os.Exit(1)
@@ -87,7 +87,7 @@ func analyzeImage(imageName string, analyzerArgs []string) error {
8787
return fmt.Errorf("Error performing image analysis: %s", err)
8888
}
8989

90-
pkgutil.PrintToStdErr("Retrieving analyses")
90+
output.PrintToStdErr("Retrieving analyses")
9191
outputResults(analyses)
9292

9393
if save {
@@ -100,4 +100,5 @@ func analyzeImage(imageName string, analyzerArgs []string) error {
100100
func init() {
101101
RootCmd.AddCommand(analyzeCmd)
102102
addSharedFlags(analyzeCmd)
103+
output.AddFlags(analyzeCmd)
103104
}

cmd/diff.go

Lines changed: 5 additions & 4 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"
@@ -42,7 +43,6 @@ var diffCmd = &cobra.Command{
4243
return nil
4344
},
4445
Run: func(cmd *cobra.Command, args []string) {
45-
pkgutil.SetQuiet(quiet)
4646
if err := diffImages(args[0], args[1], types); err != nil {
4747
logrus.Error(err)
4848
os.Exit(1)
@@ -83,7 +83,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
8383
var wg sync.WaitGroup
8484
wg.Add(2)
8585

86-
pkgutil.PrintToStdErr("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)
8787

8888
imageMap := map[string]*pkgutil.Image{
8989
image1Arg: {},
@@ -113,7 +113,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
113113
defer pkgutil.CleanupImage(*imageMap[image2Arg])
114114
}
115115

116-
pkgutil.PrintToStdErr("Computing diffs")
116+
output.PrintToStdErr("Computing diffs")
117117
req := differs.DiffRequest{
118118
Image1: *imageMap[image1Arg],
119119
Image2: *imageMap[image2Arg],
@@ -125,7 +125,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
125125
outputResults(diffs)
126126

127127
if filename != "" {
128-
pkgutil.PrintToStdErr("Computing filename diffs")
128+
output.PrintToStdErr("Computing filename diffs")
129129
err := diffFile(imageMap[image1Arg], imageMap[image2Arg])
130130
if err != nil {
131131
return err
@@ -152,4 +152,5 @@ func init() {
152152
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.")
153153
RootCmd.AddCommand(diffCmd)
154154
addSharedFlags(diffCmd)
155+
output.AddFlags(diffCmd)
155156
}

cmd/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ var json bool
3939
var save bool
4040
var types diffTypes
4141
var noCache bool
42-
var quiet bool
4342

4443
var LogLevel string
4544

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

211210
func addSharedFlags(cmd *cobra.Command) {
212211
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.")
214212
cmd.Flags().VarP(&types, "type", "t", "This flag sets the list of analyzer types to use. Set it repeatedly to use multiple analyzers.")
215213
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
216214
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.")

pkg/util/cmd_utils.go renamed to cmd/util/output/output.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util
17+
package output
1818

1919
import (
2020
"fmt"
21+
"github.com/spf13/cobra"
2122
"os"
2223
)
2324

24-
var quiet = false
25+
var quiet bool
2526

27+
// PrintToStdErr prints to stderr if quiet flag isn't enabled
2628
func PrintToStdErr(output string, vars ...interface{}) {
2729
if !quiet {
2830
fmt.Fprintf(os.Stderr, output, vars...)
2931
}
3032
}
3133

32-
func SetQuiet(q bool) {
33-
quiet = q
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.")
3437
}

pkg/util/image_prep_utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"strings"
2727

28+
"github.com/GoogleCloudPlatform/container-diff/cmd/util/output"
2829
"github.com/GoogleCloudPlatform/container-diff/pkg/cache"
2930
"github.com/containers/image/docker"
3031
"github.com/containers/image/manifest"
@@ -99,7 +100,7 @@ type ConfigSchema struct {
99100
}
100101

101102
func getImage(p Prepper) (Image, error) {
102-
PrintToStdErr("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())
103104
imgPath, err := p.GetFileSystem()
104105
if err != nil {
105106
return Image{}, err

0 commit comments

Comments
 (0)