Skip to content

Commit

Permalink
update main logic
Browse files Browse the repository at this point in the history
Signed-off-by: AnaisUrlichs <[email protected]>
  • Loading branch information
AnaisUrlichs committed Feb 20, 2024
1 parent fc1757c commit 5d9448e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 758 deletions.
29 changes: 21 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func difference(a, b types.Result) {
vulnOne := a.Vulnerabilities
vulnTwo := b.Vulnerabilities

// Loop two times, first to find slice1 strings not in slice2,
// second loop to find slice2 strings not in slice1
// Loop two times, first to find Report 1 Vulnerabilities not in Report 2,
// second loop to find Report 2 Vulnerabilities strings not in Report 1
for i := 0; i < 2; i++ {
for _, s1 := range vulnOne {
found := false
Expand All @@ -36,12 +36,12 @@ func difference(a, b types.Result) {
break
}
}
// String not found. We add it to return slice
// String not found. We add it to return Report
if !found {
diffResult.Vulnerabilities = append(diffResult.Vulnerabilities, s1)
}
}
// Swap the slices, only if it was the first loop
// Swap the two Vulnerability Reports, only if it was the first loop
if i == 0 {
vulnOne, vulnTwo = vulnTwo, vulnOne
}
Expand All @@ -51,45 +51,58 @@ func difference(a, b types.Result) {

func main() {

// Access the files paths provided
filePathOne := os.Args[1]
filePathTwo := os.Args[2]

// Open each file
openFileOne, err := os.Open(filePathOne)
openFileTwo, err := os.Open(filePathTwo)

if err != nil {
fmt.Println("Error one", err)
fmt.Println("Error: Could not open file(s)", err)
}

// Read everything in the file
fileOneBytes, err := io.ReadAll(openFileOne)
fileTwoBytes, err := io.ReadAll(openFileTwo)

if err != nil {
fmt.Println("Error two", err)
}

// Don't close the file yet
defer openFileOne.Close()
defer openFileTwo.Close()

// Convert the json/bytes from the files into a valid Go struct
var resultsOne Report
json.Unmarshal(fileOneBytes, &resultsOne)

var resultsTwo Report
json.Unmarshal(fileTwoBytes, &resultsTwo)

// Even though there is only one Results.Result array, these loops go through each array
// and looks up the difference between the Result arrays
for _, a := range resultsOne.Results {
for _, b := range resultsTwo.Results {
difference(a, b)
}
}

saveResult(resultsOne, resultsTwo)
}

// The second report needs to be updated with the difference between both reports
func saveResult(resultsOne, resultsTwo Report) {
arrlen := len(resultsTwo.Results)

for i := 0; i < arrlen; i++ {
resultsTwo.Results[i] = diffResult
resultsTwo.Results[i].Vulnerabilities = diffResult.Vulnerabilities
resultsTwo.Results[i].Target = "This is the difference between image one " + resultsTwo.Results[i].Target + " and two " + resultsOne.Results[i].Target
}

o, _ := json.MarshalIndent(resultsTwo, "", " ")
_ = os.WriteFile("test.json", o, 0644)
fmt.Printf(string(o))
_ = os.WriteFile("diff.json", o, 0644)

}
237 changes: 0 additions & 237 deletions one.json

This file was deleted.

Loading

0 comments on commit 5d9448e

Please sign in to comment.