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

Commit f376e3a

Browse files
author
Priya Wadhwa
committed
filename flag must be used with --types=file flag
1 parent 553d639 commit f376e3a

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

cmd/diff.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ var diffCmd = &cobra.Command{
4242
if err := checkIfValidAnalyzer(types); err != nil {
4343
return err
4444
}
45+
if err := checkFilenameFlag(types); err != nil {
46+
return err
47+
}
4548
return nil
4649
},
4750
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 {
5052
logrus.Error(err)
5153
os.Exit(1)
5254
}
@@ -60,7 +62,16 @@ func checkDiffArgNum(args []string) error {
6062
return nil
6163
}
6264

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 {
6475
diffTypes, err := differs.GetAnalyzers(diffArgs)
6576
if err != nil {
6677
return err
@@ -74,9 +85,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
7485
var wg sync.WaitGroup
7586
wg.Add(2)
7687

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)
8089

8190
imageMap := map[string]*pkgutil.Image{
8291
image1Arg: {},
@@ -106,17 +115,6 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
106115
defer pkgutil.CleanupImage(*imageMap[image2Arg])
107116
}
108117

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-
120118
fmt.Fprintln(os.Stderr, "Computing diffs")
121119
req := differs.DiffRequest{*imageMap[image1Arg], *imageMap[image2Arg], diffTypes}
122120
diffs, err := req.GetDiff()
@@ -125,6 +123,14 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
125123
}
126124
outputResults(diffs)
127125

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+
128134
if save {
129135
logrus.Infof("Images were saved at %s and %s", imageMap[image1Arg].FSPath,
130136
imageMap[image2Arg].FSPath)
@@ -142,7 +148,7 @@ func diffFile(image1, image2 *pkgutil.Image) error {
142148
}
143149

144150
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.")
146152
RootCmd.AddCommand(diffCmd)
147153
addSharedFlags(diffCmd)
148154
}

cmd/root.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ func checkIfValidAnalyzer(flagtypes string) error {
117117
return nil
118118
}
119119

120-
func checkIfTypesFlagSet(cmd *cobra.Command) bool {
121-
flag := cmd.Flags().Lookup("types")
122-
return flag.Changed
123-
}
124-
125120
func getPrepperForImage(image string) (pkgutil.Prepper, error) {
126121
cli, err := client.NewEnvClient()
127122
if err != nil {

0 commit comments

Comments
 (0)