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

Commit 553d639

Browse files
author
Priya Wadhwa
committed
Moved filename flag to diff, rewrote DiffFile
1 parent 2326e06 commit 553d639

File tree

7 files changed

+61
-45
lines changed

7 files changed

+61
-45
lines changed

cmd/analyze.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ var analyzeCmd = &cobra.Command{
3636
if err := validateArgs(args, checkAnalyzeArgNum); err != nil {
3737
return err
3838
}
39-
if err := checkAnalyzeFilenameFlag(); err != nil {
40-
return err
41-
}
4239
if err := checkIfValidAnalyzer(types); err != nil {
4340
return err
4441
}
@@ -59,13 +56,6 @@ func checkAnalyzeArgNum(args []string) error {
5956
return nil
6057
}
6158

62-
func checkAnalyzeFilenameFlag() error {
63-
if filename != "" {
64-
return errors.New("please remove --filename flag, incompatible with analyze")
65-
}
66-
return nil
67-
}
68-
6959
func analyzeImage(imageName string, analyzerArgs []string) error {
7060
analyzeTypes, err := differs.GetAnalyzers(analyzerArgs)
7161
if err != nil {

cmd/diff.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"sync"
3030
)
3131

32+
var filename string
33+
3234
var diffCmd = &cobra.Command{
3335
Use: "diff",
3436
Short: "Compare two images: [image1] [image2]",
@@ -106,7 +108,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
106108

107109
if filename != "" {
108110
fmt.Fprintln(os.Stderr, "Computing filename diffs")
109-
err := diffFile(imageMap, image1Arg, image2Arg)
111+
err := diffFile(imageMap[image1Arg], imageMap[image2Arg])
110112
if err != nil {
111113
return err
112114
}
@@ -130,21 +132,17 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlagSet boo
130132
return nil
131133
}
132134

133-
func diffFile(imageMap map[string]*pkgutil.Image, image1Arg, image2Arg string) error {
134-
135-
image1FilePath := imageMap[image1Arg].FSPath + filename
136-
image2FilePath := imageMap[image2Arg].FSPath + filename
137-
138-
diff, err := util.DiffFile(image1FilePath, image2FilePath, image1Arg, image2Arg)
135+
func diffFile(image1, image2 *pkgutil.Image) error {
136+
diff, err := util.DiffFile(image1, image2, filename)
139137
if err != nil {
140138
return err
141139
}
142-
diff.Filename = filename
143-
util.TemplateOutput(diff, "FileNameDiff")
140+
util.TemplateOutput(diff, "FilenameDiff")
144141
return nil
145142
}
146143

147144
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")
148146
RootCmd.AddCommand(diffCmd)
149147
addSharedFlags(diffCmd)
150148
}

cmd/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
var json bool
3737
var save bool
3838
var types string
39-
var filename string
4039

4140
var LogLevel string
4241

@@ -157,5 +156,4 @@ func addSharedFlags(cmd *cobra.Command) {
157156
cmd.Flags().StringVarP(&types, "types", "t", "apt", "This flag sets the list of analyzer types to use. It expects a comma separated list of supported analyzers.")
158157
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
159158
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.")
160-
cmd.Flags().StringVarP(&filename, "filename", "f", "", "View the diff of a file in both containers (can only be used with container-diff diff)")
161159
}

pkg/util/fs_utils.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package util
1818

1919
import (
2020
"bytes"
21-
"errors"
2221
"io/ioutil"
2322
"os"
2423
"path/filepath"
@@ -55,21 +54,22 @@ func GetSize(path string) int64 {
5554
}
5655

5756
//GetFileContents returns the contents of a file at the specified path
58-
func GetFileContents(path string) ([]string, error) {
59-
stat, err := os.Stat(path)
60-
if err != nil {
57+
func GetFileContents(path string) (*string, error) {
58+
if _, err := os.Stat(path); os.IsNotExist(err) {
6159
return nil, err
6260
}
6361

64-
if stat.IsDir() {
65-
return nil, errors.New("--filename is a directory, not a file")
66-
}
6762
contents, err := ioutil.ReadFile(path)
68-
6963
if err != nil {
7064
return nil, err
7165
}
72-
return []string{string(contents)}, nil
66+
67+
strContents := string(contents)
68+
//If file is empty, return nil
69+
if strContents == "" {
70+
return nil, nil
71+
}
72+
return &strContents, nil
7373
}
7474

7575
func getDirectorySize(path string) (int64, error) {

util/diff_utils.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ type DirDiff struct {
3535
}
3636

3737
type FileNameDiff struct {
38-
Filename string
39-
Diff string
38+
Filename string
39+
Description string
40+
Diff string
4041
}
4142

4243
type EntryDiff struct {
@@ -123,34 +124,62 @@ func DiffDirectory(d1, d2 pkgutil.Directory) (DirDiff, bool) {
123124
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same
124125
}
125126

126-
//DiffFile diffs within a file
127-
func DiffFile(path1, path2, image1, image2 string) (FileNameDiff, error) {
127+
func DiffFile(image1, image2 *pkgutil.Image, filename string) (*FileNameDiff, error) {
128+
//Join paths
129+
image1FilePath := filepath.Join(image1.FSPath, filename)
130+
image2FilePath := filepath.Join(image2.FSPath, filename)
128131

129-
var result FileNameDiff
130-
131-
image1Contents, err := pkgutil.GetFileContents(path1)
132+
//Get contents of files
133+
image1FileContents, err := pkgutil.GetFileContents(image1FilePath)
132134
if err != nil {
133-
return result, err
135+
return nil, err
134136
}
135-
image2Contents, err := pkgutil.GetFileContents(path2)
136137

138+
image2FileContents, err := pkgutil.GetFileContents(image2FilePath)
137139
if err != nil {
138-
return result, err
140+
return nil, err
141+
}
142+
143+
description := ""
144+
//Check if file contents are empty or if they are the same
145+
if image1FileContents == nil && image2FileContents == nil {
146+
description := "Both files are empty"
147+
return &FileNameDiff{filename, description, ""}, nil
148+
}
149+
150+
if image1FileContents == nil {
151+
description := fmt.Sprintf("%s contains an empty file, the contents of %s are:", image1.Source, image2.Source)
152+
return &FileNameDiff{filename, description, *image2FileContents}, nil
139153
}
140154

155+
if image2FileContents == nil {
156+
description := fmt.Sprintf("%s contains an empty file, the contents of %s are:", image2.Source, image1.Source)
157+
return &FileNameDiff{filename, description, *image1FileContents}, nil
158+
}
159+
160+
if *image1FileContents == *image2FileContents {
161+
description := "Both files are the same, the contents are:"
162+
return &FileNameDiff{filename, description, *image1FileContents}, nil
163+
}
164+
165+
//Carry on with diffing, make string array for difflib requirements
166+
image1Contents := []string{string(*image1FileContents)}
167+
image2Contents := []string{string(*image2FileContents)}
168+
169+
//Run diff
141170
diff := difflib.UnifiedDiff{
142171
A: image1Contents,
143172
B: image2Contents,
144-
FromFile: image1,
145-
ToFile: image2,
173+
FromFile: image1.Source,
174+
ToFile: image2.Source,
146175
}
147176

148177
text, err := difflib.GetUnifiedDiffString(diff)
149178

150179
if err != nil {
151-
return result, err
180+
return nil, err
152181
}
153-
return FileNameDiff{Diff: text}, nil
182+
return &FileNameDiff{filename, description, text}, nil
154183
}
155184

156185
// Checks for content differences between files of the same name from different directories

util/format_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var templates = map[string]string{
3333
"MultiVersionPackageDiff": MultiVersionDiffOutput,
3434
"HistDiff": HistoryDiffOutput,
3535
"DirDiff": FSDiffOutput,
36-
"FileNameDiff": FilenameDiffOutput,
36+
"FilenameDiff": FilenameDiffOutput,
3737
"ListAnalyze": ListAnalysisOutput,
3838
"FileAnalyze": FileAnalysisOutput,
3939
"MultiVersionPackageAnalyze": MultiVersionPackageOutput,

util/template_utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Docker history lines found only in {{.Image2}}:{{if not .Diff.Dels}} None{{else}
6767
`
6868
const FilenameDiffOutput = `
6969
-----Diff of {{.Filename}}-----
70+
{{.Description}}
7071
7172
{{.Diff}}
7273
`

0 commit comments

Comments
 (0)