Skip to content

Commit

Permalink
use order from default catalog if catalog don't have any (#46)
Browse files Browse the repository at this point in the history
* use order from default catalog if catalog don't have any

* s/failback/fallback
  • Loading branch information
strokyl authored Jun 25, 2024
1 parent c7372a4 commit 9bda2b5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions schema/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,39 @@ import (
"github.com/conduktor/ctl/resource"
)

func resourcePriority(catalog KindCatalog, resource resource.Resource, debug bool) int {
func resourcePriority(catalog KindCatalog, resource resource.Resource, debug, fallbackToDefaultCatalog bool) int {
kind, ok := catalog[resource.Kind]
if !ok {
if debug {
fmt.Fprintf(os.Stderr, "Could not find kind: %s in catalog, default to DefaultPriority for resource ordering", resource.Kind)
fmt.Fprintf(os.Stderr, "Could not find kind: %s in catalog, default to DefaultPriority for resource ordering\n", resource.Kind)
}
return DefaultPriority
}
version := extractVersionFromApiVersion(resource.Version)
kindVersion, ok := kind.Versions[version]
if !ok {
if debug {
fmt.Fprintf(os.Stderr, "Could not find version: %d of kind %s in catalog, default to DefaultPriority for resource ordering", version, resource.Kind)
fmt.Fprintf(os.Stderr, "Could not find version: %d of kind %s in catalog, default to DefaultPriority for resource ordering\n", version, resource.Kind)
}
return DefaultPriority
} else {
return kindVersion.Order
order := kindVersion.Order
if order == DefaultPriority && fallbackToDefaultCatalog {
defaultCatalog := DefaultKind()
orderFromDefaultCatalog := resourcePriority(defaultCatalog, resource, false, false)
if orderFromDefaultCatalog != DefaultPriority && debug {
fmt.Fprintf(os.Stderr, "Could not find version: %d of kind %s in catalog, but find it in default catalog with priority %d\n", version, resource.Kind, orderFromDefaultCatalog)
}
return orderFromDefaultCatalog
} else {
return kindVersion.Order
}
}
}

func SortResources(catalog KindCatalog, resources []resource.Resource, debug bool) {
sort.SliceStable(resources, func(i, j int) bool {
return resourcePriority(catalog, resources[i], debug) < resourcePriority(catalog, resources[j], debug)
return resourcePriority(catalog, resources[i], debug, true) < resourcePriority(catalog, resources[j], debug, true)
})

}

0 comments on commit 9bda2b5

Please sign in to comment.