From 7f5e0f72e47f74f53928c57e2907d7b2503b1bf7 Mon Sep 17 00:00:00 2001 From: Shir Moran Date: Tue, 16 Jul 2024 11:38:38 +0300 Subject: [PATCH] Added ComMatrix method; Changed private func to public --- commatrix/commatrix.go | 4 ++-- types/types.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/commatrix/commatrix.go b/commatrix/commatrix.go index ed392c94..4f60e82d 100644 --- a/commatrix/commatrix.go +++ b/commatrix/commatrix.go @@ -68,7 +68,7 @@ func New(kubeconfigPath string, customEntriesPath string, customEntriesFormat st if err != nil { return nil, fmt.Errorf("failed adding custom entries: %s", err) } - customComDetails, err := addFromFile(customEntriesPath, inputFormat) + customComDetails, err := AddFromFile(customEntriesPath, inputFormat) if err != nil { return nil, fmt.Errorf("failed adding custom entries: %s", err) } @@ -81,7 +81,7 @@ func New(kubeconfigPath string, customEntriesPath string, customEntriesFormat st return &types.ComMatrix{Matrix: cleanedComDetails}, nil } -func addFromFile(fp string, format types.Format) ([]types.ComDetails, error) { +func AddFromFile(fp string, format types.Format) ([]types.ComDetails, error) { var res []types.ComDetails f, err := os.Open(filepath.Clean(fp)) if err != nil { diff --git a/types/types.go b/types/types.go index 344a0559..a6fce226 100644 --- a/types/types.go +++ b/types/types.go @@ -160,3 +160,36 @@ func (m ComMatrix) Contains(cd ComDetails) bool { return false } + +// Returns True if both ComMatrixes have exactly the same ComMatrix details +func (m ComMatrix) Equals(other ComMatrix) bool { + // Check if all of "m" commatrix details are in "other" commartix + for _, cd1 := range m.Matrix { + found := false + for _, cd2 := range other.Matrix { + if cd1.Equals(cd2) { + found = true + break + } + } + if !found { + return false + } + } + + // Check if all of "other" commatrix details are in "m" commartix + for _, cd2 := range other.Matrix { + found := false + for _, cd1 := range m.Matrix { + if cd2.Equals(cd1) { + found = true + break + } + } + if !found { + return false + } + } + + return true +}