Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
jobs:

build:
name: Go tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
[![Tests](https://github.com/DI-Tony-Reed/JSONDiff/actions/workflows/tests.yaml/badge.svg)](https://github.com/DI-Tony-Reed/JSONDiff/actions/workflows/tests.yaml)

# What is this
This tool simply accepts two JSON files and returns the difference between them. It utilizes https://github.com/go-test/deep for the comparison.
This tool accepts two JSON Snyk scans and returns the difference between them. It utilizes a slightly modified version of https://github.com/hezro/snyk-code-pr-diff for the comparison.

# How do I use this?
Locally, you can clone this repository, build it via the Makefile, and run it by feeding it two JSON files:
Locally, you can clone this repository, build it via the Makefile, and run it by feeding it two JSON Snyk scan files:
```bash
make build
./bin/jsonDiff-darwin json1.json json2.json
```
Make sure to use the appropriate binary for your OS. The above example is assuming you are on a Mac.

## Additional CLI flags
You can also use the `--byteskip` to skip the comparison if the second file has fewer bytes than the first.
```bash
./bin/jsonDiff-darwin json1.json json2.json --byteskip
```


# Viewing test coverage
```bash
make tests-coverage && open coverage.html
Expand Down
32 changes: 11 additions & 21 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,31 @@ package main

import (
"bytes"
"fmt"
"github.com/go-test/deep"
"errors"
)

type JSONDiff struct {
File1 File
File2 File
ByteSkip bool
File1 File
File2 File
}

func (j JSONDiff) FindDifferences() string {
func (j JSONDiff) FindDifferences() (string, error) {
if j.File1.Bytes == nil || j.File2.Bytes == nil {
return "No bytes defined for File1 and/or File2."
return "No bytes defined for File1 and/or File2.", nil
}

if j.File1.Map == nil || j.File2.Map == nil {
return "No map defined for File1 and/or File2."
return "No map defined for File1 and/or File2.", nil
}

if bytes.Equal(j.File1.Bytes, j.File2.Bytes) {
return "No differences found."
return "No differences found.", nil
}

if j.ByteSkip && len(j.File2.Bytes) < len(j.File1.Bytes) {
return "Second file smaller than first and byteskip enabled"
output, err := check(j.File1.Map, j.File2.Map)
if err != nil {
return "", err
}

if diff := deep.Equal(j.File1.Map, j.File2.Map); diff != nil {
differences := "Differences found:"
for _, d := range diff {
differences += fmt.Sprintf("\n%v", d)
}

return differences
}

return "No differences found."
return output, errors.New("new vulnerability(s) found")
}
Loading