From d216a0a29ec0ee5a0f52c3ec52df8e27c987a526 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Mon, 30 Sep 2024 18:14:49 +0530 Subject: [PATCH 1/2] utility function to get latest Version w/signoff Signed-off-by: Jougan-0 --- utils/utils.go | 128 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index 77b2dd64..737ca1c0 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -17,8 +17,10 @@ import ( "reflect" "regexp" "runtime" + "sort" "strconv" "strings" + "unicode" "github.com/layer5io/meshkit/models/meshmodel/entity" log "github.com/sirupsen/logrus" @@ -586,3 +588,129 @@ func Compress(src string, buf io.Writer) error { return nil }) } + +// Check if a string is purely numeric +func isNumeric(s string) bool { + for _, r := range s { + if !unicode.IsDigit(r) { + return false + } + } + return true +} + +// Split version into components (numeric and non-numeric) using both '.' and '-' +func splitVersion(version string) []string { + version = strings.ReplaceAll(version, "-", ".") + return strings.Split(version, ".") +} + +// Compare two version strings +func compareVersions(v1, v2 string) int { + v1Components := splitVersion(v1) + v2Components := splitVersion(v2) + + maxLen := len(v1Components) + if len(v2Components) > maxLen { + maxLen = len(v2Components) + } + + for i := 0; i < maxLen; i++ { + var part1, part2 string + if i < len(v1Components) { + part1 = v1Components[i] + } + if i < len(v2Components) { + part2 = v2Components[i] + } + + if isNumeric(part1) && isNumeric(part2) { + num1, _ := strconv.Atoi(part1) + num2, _ := strconv.Atoi(part2) + if num1 != num2 { + return num1 - num2 + } + } else if isNumeric(part1) && !isNumeric(part2) { + return -1 + } else if !isNumeric(part1) && isNumeric(part2) { + return 1 + } else { + if part1 != part2 { + return strings.Compare(part1, part2) + } + } + } + + return 0 +} + +// Function to get all version directories sorted in descending order +func GetAllVersionDirsSortedDesc(modelVersionsDirPath string) ([]string, error) { + type versionInfo struct { + original string + dirPath string + } + entries, err := os.ReadDir(modelVersionsDirPath) + if err != nil { + return nil, fmt.Errorf("failed to read versions directory '%s': %w", modelVersionsDirPath, err) + } + + if len(entries) == 0 { + return nil, fmt.Errorf("no version directories found in '%s'", modelVersionsDirPath) + } + + versions := []versionInfo{} + for _, entry := range entries { + if !entry.IsDir() { + continue + } + + versionDirPath := filepath.Join(modelVersionsDirPath, entry.Name()) + versionStr := entry.Name() + + // Optionally remove leading 'v' + versionStr = strings.TrimPrefix(versionStr, "v") + + if versionStr == "" { + continue + } + + versions = append(versions, versionInfo{ + original: versionStr, + dirPath: versionDirPath, + }) + } + + if len(versions) == 0 { + return nil, fmt.Errorf("no valid version directories found in '%s'", modelVersionsDirPath) + } + + sort.Slice(versions, func(i, j int) bool { + return compareVersions(versions[i].original, versions[j].original) > 0 + }) + + sortedDirPaths := make([]string, len(versions)) + for i, v := range versions { + sortedDirPaths[i] = v.dirPath + } + + return sortedDirPaths, nil +} + +// isDirectoryNonEmpty checks if a directory exists and is non-empty +func IsDirectoryNonEmpty(dirPath string) bool { + fi, err := os.Stat(dirPath) + if err != nil { + return false + } + if !fi.IsDir() { + return false + } + + entries, err := os.ReadDir(dirPath) + if err != nil { + return false + } + + return len(entries) > 0 +} From b4716964441f4b66eaaa8fad564d19190fdba4dc Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Fri, 4 Oct 2024 22:43:30 +0530 Subject: [PATCH 2/2] check and bypass on basis of status w/signoff Signed-off-by: Jougan-0 --- models/registration/register.go | 11 ++++++++++- utils/utils.go | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/models/registration/register.go b/models/registration/register.go index 02dde7f5..14005b7f 100644 --- a/models/registration/register.go +++ b/models/registration/register.go @@ -51,9 +51,13 @@ func (rh *RegistrationHelper) register(pkg PackagingUnit) { //silently exit if the model does not conatin any components or relationships return } + ignored := model.ModelDefinitionStatusIgnored // 1. Register the model model := pkg.Model - + modelstatus := model.Status + if modelstatus == ignored { + return + } // Don't register anything else if registrant is not there if model.Registrant.Kind == "" { err := ErrMissingRegistrant(model.Name) @@ -100,6 +104,11 @@ func (rh *RegistrationHelper) register(pkg PackagingUnit) { var registeredRelationships []relationship.RelationshipDefinition // 2. Register components for _, comp := range pkg.Components { + status := *comp.Status + if status == component.Ignored { + continue + } + comp.Model = model if comp.Styles != nil { diff --git a/utils/utils.go b/utils/utils.go index 737ca1c0..25a7338a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -402,7 +402,9 @@ func CreateDirectory(path string) error { func ReplaceSpacesAndConvertToLowercase(s string) string { return strings.ToLower(strings.ReplaceAll(s, " ", "")) } - +func ReplaceSpacesWithHyphenAndConvertToLowercase(s string) string { + return strings.ToLower(strings.ReplaceAll(s, " ", "-")) +} func ExtractDomainFromURL(location string) string { parsedURL, err := url.Parse(location) // If unable to extract domain return the location as is.