Skip to content

Commit

Permalink
generate diff matrix as a smiple file rather than a csv
Browse files Browse the repository at this point in the history
  • Loading branch information
shirmoran committed Jul 30, 2024
1 parent dab3df3 commit 41eb22e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
9 changes: 6 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/openshift-kni/commatrix/commatrix"
"github.com/openshift-kni/commatrix/types"
)

var (
Expand Down Expand Up @@ -83,10 +83,13 @@ func main() {
panic(fmt.Sprintf("Error while writing ss matrix to file :%v", err))
}
// generate the diff matrix between the ss and the enpointslice matrix
diffCommatrix := commatrix.GenerateMatrixDiff(*mat, *ssMat)
diff, err := commatrix.GenerateMatrixDiff(*mat, *ssMat)
if err != nil {
panic(fmt.Sprintf("Error while writing matrix diff file :%v", err))
}

// write the diff matrix between the ss and the enpointslice matrix to a csv file
err = commatrix.WriteMatrixToFileByType(diffCommatrix, "matrix-diff-ss", types.FormatCSV, deployment, destDir)
err = os.WriteFile(filepath.Join(destDir, "matrix-diff-ss"), []byte(diff), 0644)
if err != nil {
panic(fmt.Sprintf("Error writing the diff matrix :%v", err))
}
Expand Down
39 changes: 29 additions & 10 deletions commatrix/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"strings"
"sync"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -165,18 +167,22 @@ func writeMatrixToFile(matrix types.ComMatrix, fileName, format string, printFn
}

// GenerateMatrixDiff generates a new matrix demonstrating the diff between mat2 to mat1.
func GenerateMatrixDiff(mat1 types.ComMatrix, mat2 types.ComMatrix) types.ComMatrix {
diffMatrix := []types.ComDetails{}
func GenerateMatrixDiff(mat1 types.ComMatrix, mat2 types.ComMatrix) (string, error) {
colNames := getComMatrixColTagsByFormat(types.FormatCSV)
if colNames == "" {
return "", fmt.Errorf("error getting commatrix CSV tags")
}

diff := colNames + "\n"

for _, cd := range mat1.Matrix {
if mat2.Contains(cd) {
diffMatrix = append(diffMatrix, cd)
diff += fmt.Sprintf("%s\n", cd)
continue
}

// add "+" before cd's mat1 contains but mat2 doesn't
newCd := cd
newCd.Direction = fmt.Sprintf("+ %s", newCd.Direction)
diffMatrix = append(diffMatrix, newCd)
diff += fmt.Sprintf("+ %s\n", cd)
}

for _, cd := range mat2.Matrix {
Expand All @@ -188,13 +194,26 @@ func GenerateMatrixDiff(mat1 types.ComMatrix, mat2 types.ComMatrix) types.ComMat

if !mat1.Contains(cd) {
// add "-" before cd's mat1 doesn't contain but mat2 does
missingCd := cd
missingCd.Direction = fmt.Sprintf("- %s", missingCd.Direction)
diffMatrix = append(diffMatrix, missingCd)
diff += fmt.Sprintf("- %s\n", cd)
}
}

return types.ComMatrix{Matrix: diffMatrix}
return diff, nil
}

func getComMatrixColTagsByFormat(format string) string {
typ := reflect.TypeOf(types.ComDetails{})

var tagsList []string
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag.Get(format)
if tag != "" {
tagsList = append(tagsList, tag)
}
}

return strings.Join(tagsList, ",")
}

func separateMatrixByRole(matrix types.ComMatrix) (types.ComMatrix, types.ComMatrix) {
Expand Down

0 comments on commit 41eb22e

Please sign in to comment.