-
Notifications
You must be signed in to change notification settings - Fork 237
Diff within files #121
Diff within files #121
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Just a few comments
cmd/diff.go
Outdated
@@ -116,6 +130,20 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error { | |||
return nil | |||
} | |||
|
|||
func diffFile(imageMap map[string]*pkgutil.Image, image1Arg, image2Arg string) error { | |||
|
|||
image1FilePath := imageMap[image1Arg].FSPath + filename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should probably use something like path.Join
https://golang.org/pkg/path/#Join
pkg/util/fs_utils.go
Outdated
@@ -53,6 +54,24 @@ func GetSize(path string) int64 { | |||
return stat.Size() | |||
} | |||
|
|||
//GetFileContents returns the contents of a file at the specified path | |||
func GetFileContents(path string) ([]string, error) { | |||
stat, err := os.Stat(path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this checking for the existence of a file?
It should be
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it does check for existence, but also checks that the path isn't a directory after, which is why I thought it might be easier to split it up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess what is the expected behavior when the file doesn't exist? I think you wouldn't want to throw an error?
Different scenarios:
A: ""
B: no file
output: ?
A: "something"
B: no file
output: ?
A: no file
B: no file
output: ?
pkg/util/fs_utils.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
return []string{string(contents)}, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't this return a normal string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the diff library I'm using wanted this format, but it probably makes more sense to move that to DiffFile
util/diff_utils.go
Outdated
@@ -118,6 +123,36 @@ func DiffDirectory(d1, d2 pkgutil.Directory) (DirDiff, bool) { | |||
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same | |||
} | |||
|
|||
//DiffFile diffs within a file | |||
func DiffFile(path1, path2, image1, image2 string) (FileNameDiff, error) { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove newline
util/diff_utils.go
Outdated
|
||
image1Contents, err := pkgutil.GetFileContents(path1) | ||
if err != nil { | ||
return result, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you encounter an error, you should return nil, err
cmd/root.go
Outdated
@@ -151,4 +157,5 @@ func addSharedFlags(cmd *cobra.Command) { | |||
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.") | |||
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.") | |||
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.") | |||
cmd.Flags().StringVarP(&filename, "filename", "f", "", "View the diff of a file in both containers (can only be used with container-diff diff)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add this flag to the diff subcommand specific flags instead of have it be at the root level. That will let you remove a lot of the flag validation.
util/diff_utils.go
Outdated
@@ -118,6 +123,36 @@ func DiffDirectory(d1, d2 pkgutil.Directory) (DirDiff, bool) { | |||
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same | |||
} | |||
|
|||
//DiffFile diffs within a file | |||
func DiffFile(path1, path2, image1, image2 string) (FileNameDiff, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't satisfy any interface right? In that case, you should probably pass the filename parameter into here too and set it on the FileNameDiff
cmd/diff.go
Outdated
if err != nil { | ||
return err | ||
} | ||
diff.Filename = filename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you pass filename into util.DiffFile
so you don't have to set this after the fact?
cmd/diff.go
Outdated
image1FilePath := imageMap[image1Arg].FSPath + filename | ||
image2FilePath := imageMap[image2Arg].FSPath + filename | ||
|
||
diff, err := util.DiffFile(image1FilePath, image2FilePath, image1Arg, image2Arg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think util.DiffFile can do the path joining actually.
You could make util.DiffFile
look like
func DiffFile(image1, image2, filename string) (FileNameDiff, error) {
// join paths
// run diff
// return full FileNameDiff object
}
cmd/analyze.go
Outdated
@@ -56,6 +59,13 @@ func checkAnalyzeArgNum(args []string) error { | |||
return nil | |||
} | |||
|
|||
func checkAnalyzeFilenameFlag() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this once you've added the flag under container-diff diff
only
6314a0a
to
553d639
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
can you add some documentation about this in the README? |
To diff within files, --types must include the file analyzer and the --filename flag must be set.
--filename points to the path of the file in both images.
ex. container-diff diff file1.tar file2.tar --types=file --filename=/app/file.txt