-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added sorting feature for node & nodepool commands (#20)
- Loading branch information
1 parent
a1cbdb1
commit 7f7defb
Showing
7 changed files
with
55 additions
and
28 deletions.
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package outputs | ||
|
||
import ( | ||
"k8s.io/utils/strings/slices" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// getSortKeyIdxFromHeader retrieves the index of the sortKey in the header slice | ||
func getSortKeyIdxFromHeader(headers []string, sortKey string) int { | ||
// defaults to first column always (usually node/nodepool name) | ||
idx := 0 | ||
if slices.Contains(headers, strings.ToUpper(sortKey)) { | ||
idx = slices.Index(headers, strings.ToUpper(sortKey)) | ||
} | ||
|
||
return idx | ||
} | ||
|
||
// SortOutputBasedOnHeader sorts output based on the Header key provided in the flags | ||
func SortOutputBasedOnHeader(headers []string, sortSlices [][]string, sortKey string) { | ||
sortByHeaderIndex := getSortKeyIdxFromHeader(headers, sortKey) | ||
|
||
sort.SliceStable(sortSlices, func(i, j int) bool { | ||
return sortSlices[i][sortByHeaderIndex] < sortSlices[j][sortByHeaderIndex] | ||
}) | ||
} |
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