@@ -42,11 +42,13 @@ var diffCmd = &cobra.Command{
42
42
if err := checkIfValidAnalyzer (types ); err != nil {
43
43
return err
44
44
}
45
+ if err := checkFilenameFlag (types ); err != nil {
46
+ return err
47
+ }
45
48
return nil
46
49
},
47
50
Run : func (cmd * cobra.Command , args []string ) {
48
- typesFlagSet := checkIfTypesFlagSet (cmd )
49
- if err := diffImages (args [0 ], args [1 ], strings .Split (types , "," ), typesFlagSet ); err != nil {
51
+ if err := diffImages (args [0 ], args [1 ], strings .Split (types , "," )); err != nil {
50
52
logrus .Error (err )
51
53
os .Exit (1 )
52
54
}
@@ -60,7 +62,16 @@ func checkDiffArgNum(args []string) error {
60
62
return nil
61
63
}
62
64
63
- func diffImages (image1Arg , image2Arg string , diffArgs []string , typesFlagSet bool ) error {
65
+ func checkFilenameFlag (types string ) error {
66
+ if filename != "" {
67
+ if ! strings .Contains (types , "file" ) {
68
+ return errors .New ("Please include --types=file with the --filename flag" )
69
+ }
70
+ }
71
+ return nil
72
+ }
73
+
74
+ func diffImages (image1Arg , image2Arg string , diffArgs []string ) error {
64
75
diffTypes , err := differs .GetAnalyzers (diffArgs )
65
76
if err != nil {
66
77
return err
@@ -74,9 +85,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
74
85
var wg sync.WaitGroup
75
86
wg .Add (2 )
76
87
77
- if typesFlagSet || filename == "" {
78
- fmt .Fprintf (os .Stderr , "Starting diff on images %s and %s, using differs: %s\n " , image1Arg , image2Arg , diffArgs )
79
- }
88
+ fmt .Fprintf (os .Stderr , "Starting diff on images %s and %s, using differs: %s\n " , image1Arg , image2Arg , diffArgs )
80
89
81
90
imageMap := map [string ]* pkgutil.Image {
82
91
image1Arg : {},
@@ -106,17 +115,6 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
106
115
defer pkgutil .CleanupImage (* imageMap [image2Arg ])
107
116
}
108
117
109
- if filename != "" {
110
- fmt .Fprintln (os .Stderr , "Computing filename diffs" )
111
- err := diffFile (imageMap [image1Arg ], imageMap [image2Arg ])
112
- if err != nil {
113
- return err
114
- }
115
- if ! typesFlagSet {
116
- return nil
117
- }
118
- }
119
-
120
118
fmt .Fprintln (os .Stderr , "Computing diffs" )
121
119
req := differs.DiffRequest {* imageMap [image1Arg ], * imageMap [image2Arg ], diffTypes }
122
120
diffs , err := req .GetDiff ()
@@ -125,6 +123,14 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
125
123
}
126
124
outputResults (diffs )
127
125
126
+ if filename != "" {
127
+ fmt .Fprintln (os .Stderr , "Computing filename diffs" )
128
+ err := diffFile (imageMap [image1Arg ], imageMap [image2Arg ])
129
+ if err != nil {
130
+ return err
131
+ }
132
+ }
133
+
128
134
if save {
129
135
logrus .Infof ("Images were saved at %s and %s" , imageMap [image1Arg ].FSPath ,
130
136
imageMap [image2Arg ].FSPath )
@@ -142,7 +148,7 @@ func diffFile(image1, image2 *pkgutil.Image) error {
142
148
}
143
149
144
150
func init () {
145
- 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" )
151
+ 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. " )
146
152
RootCmd .AddCommand (diffCmd )
147
153
addSharedFlags (diffCmd )
148
154
}
0 commit comments