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

Diff within files #121

Merged
merged 4 commits into from
Oct 19, 2017
Merged

Conversation

priyawadhwa
Copy link
Contributor

@priyawadhwa priyawadhwa commented Oct 17, 2017

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

Copy link
Contributor

@r2d4 r2d4 left a 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
Copy link
Contributor

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

@@ -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)
Copy link
Contributor

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
}

Copy link
Contributor Author

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?

Copy link
Contributor

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: ?

if err != nil {
return nil, err
}
return []string{string(contents)}, nil
Copy link
Contributor

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?

Copy link
Contributor Author

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

@@ -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) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline


image1Contents, err := pkgutil.GetFileContents(path1)
if err != nil {
return result, err
Copy link
Contributor

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)")
Copy link
Contributor

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.

@@ -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) {
Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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 {
Copy link
Contributor

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

Copy link
Contributor

@dlorenc dlorenc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@dlorenc dlorenc merged commit cf169f6 into GoogleContainerTools:master Oct 19, 2017
@nkubala
Copy link
Contributor

nkubala commented Oct 19, 2017

can you add some documentation about this in the README?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants