Skip to content

Commit

Permalink
feat: add option to sort alphabetically within kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
rbjorklin committed Sep 17, 2024
1 parent 6aa0c36 commit 6537a4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 9 additions & 3 deletions kind_sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ var UninstallOrder KindSortOrder = []string{
// sort manifests by kind.
//
// Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []releaseutil.Manifest, ordering KindSortOrder) []releaseutil.Manifest {
func sortManifestsByKind(manifests []releaseutil.Manifest, ordering KindSortOrder, alphabetical bool) []releaseutil.Manifest {
sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering)
return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering, alphabetical)
})

return manifests
}

func lessByKind(a interface{}, b interface{}, kindA string, kindB string, o KindSortOrder) bool {
func lessByKind(a interface{}, b interface{}, kindA string, kindB string, o KindSortOrder, alphabetical bool) bool {
ordering := make(map[string]int, len(o))
for v, k := range o {
ordering[k] = v
Expand All @@ -144,6 +144,12 @@ func lessByKind(a interface{}, b interface{}, kindA string, kindB string, o Kind
if !bok {
return true
}
if alphabetical && kindA == kindB {
// sort alphabetically only if kind is equal
manifestA, _ := a.(releaseutil.Manifest)
manifestB, _ := b.(releaseutil.Manifest)
return manifestA.Name < manifestB.Name
}
// sort different kinds, keep original order if same priority
return first < second
}
6 changes: 4 additions & 2 deletions ksort.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type options struct {
filenameOptions resource.FilenameOptions
delete bool
nameOnly bool
alphabetical bool

genericclioptions.IOStreams
}
Expand Down Expand Up @@ -108,6 +109,7 @@ func NewCommand(streams genericclioptions.IOStreams) *cobra.Command {
o.filenameFlags.AddFlags(cmd.Flags())
cmd.Flags().BoolVarP(&o.delete, "delete", "d", o.delete, "Sort manifests in uninstall order")
cmd.Flags().BoolVarP(&o.nameOnly, "name-only", "n", o.nameOnly, "Print only the name of the sorted manifests")
cmd.Flags().BoolVarP(&o.alphabetical, "alphabetical", "a", o.alphabetical, "Sort manifests alphabetically within kinds")
cmd.Flags().BoolVar(&printVersion, "version", printVersion, "Print the version and exit")
cmd.Flags().AddGoFlagSet(flag.CommandLine)

Expand Down Expand Up @@ -215,7 +217,7 @@ func (o *options) run() error {
}

a := make([]string, len(manifests))
for i, m := range sortManifestsByKind(manifests, sortOrder) {
for i, m := range sortManifestsByKind(manifests, sortOrder, o.alphabetical) {
// If manifest data is read from stdin, m.Name is empty
if m.Name != "" {
a[i] += fmt.Sprintf("# Source: %s\n", m.Name)
Expand All @@ -230,7 +232,7 @@ func (o *options) run() error {
}

if o.nameOnly {
for _, m := range sortManifestsByKind(manifests, sortOrder) {
for _, m := range sortManifestsByKind(manifests, sortOrder, o.alphabetical) {
fmt.Println(m.Name)
}
} else {
Expand Down

0 comments on commit 6537a4d

Please sign in to comment.