generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
utility function to get latest Version w/signoff #599
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d216a0a
utility function to get latest Version w/signoff
Jougan-0 3a39262
Merge branch 'master' into latestVersionRegistration
Jougan-0 758a131
Merge branch 'master' into latestVersionRegistration
Jougan-0 b471696
check and bypass on basis of status w/signoff
Jougan-0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract the logic and "Less" function implementation as under sorted semver version / similar. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay |
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider to add unit test on this method ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. // @Jougan-0