From 9bda2b5851b8f9822e0deb979a61ad38f1d0436d Mon Sep 17 00:00:00 2001 From: LUC DUZAN Date: Tue, 25 Jun 2024 15:33:01 +0200 Subject: [PATCH] use order from default catalog if catalog don't have any (#46) * use order from default catalog if catalog don't have any * s/failback/fallback --- schema/sort.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/schema/sort.go b/schema/sort.go index dab511c..e8d3c3f 100644 --- a/schema/sort.go +++ b/schema/sort.go @@ -9,11 +9,11 @@ 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 } @@ -21,17 +21,27 @@ func resourcePriority(catalog KindCatalog, resource resource.Resource, debug boo 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) }) }