Skip to content

Commit

Permalink
Show unkown categories
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoBrigitte committed Dec 2, 2024
1 parent 6bb6673 commit 5b5218e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 36 deletions.
86 changes: 50 additions & 36 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,55 +71,69 @@ func runner(cmd *cobra.Command, args []string) error {
fmt.Fprintln(w, "planCode\tcategory\tname\tprice\tstatus\tdatacenters")
fmt.Fprintln(w, "--------\t--------\t----\t-----\t------\t-----------")

// Sort plans by price
// Sort plans by category and price
sort.Slice(catalog.Plans, func(i, j int) bool {
if catalog.Plans[i].Blobs.Commercial.Range != catalog.Plans[j].Blobs.Commercial.Range {
// Group plans by category first
a := pkgcategory.GetDisplayName(catalog.Plans[i].Blobs.Commercial.Range)
if a == "" {
a = catalog.Plans[i].Blobs.Commercial.Range
}

b := pkgcategory.GetDisplayName(catalog.Plans[j].Blobs.Commercial.Range)
if b == "" {
b = catalog.Plans[j].Blobs.Commercial.Range
}

return a < b
}

// Then sort by price
return catalog.Plans[i].GetFirstPrice().Price < catalog.Plans[j].GetFirstPrice().Price
})

// Display servers plans
nothingAvailable := true
for _, planCategory := range pkgcategory.Categories {
for _, plan := range catalog.Plans {
// Filter plans by plan code code
if planCode != "" && plan.PlanCode != planCode {
continue
}

// Filter plans by category
if category != "" && category != planCategory.Name {
continue
}
for _, plan := range catalog.Plans {
// Filter plans by plan code code
if planCode != "" && plan.PlanCode != planCode {
continue
}

// Group plans by category
if plan.Blobs.Commercial.Range != planCategory.Name {
continue
}
// Filter plans by category
if category != "" && category != plan.Blobs.Commercial.Range {
continue
}

// Format price
var price float64
planPrice := plan.GetFirstPrice()
if !reflect.DeepEqual(planPrice, kimsuficatalog.PlanPricing{}) {
price = planPrice.GetPrice()
}
// Format price
var price float64
planPrice := plan.GetFirstPrice()
if !reflect.DeepEqual(planPrice, kimsuficatalog.PlanPricing{}) {
price = planPrice.GetPrice()
}

// Format availability status
datacenters := availabilities.GetPlanCodeAvailableDatacenters(plan.PlanCode)
// Format availability status
datacenters := availabilities.GetPlanCodeAvailableDatacenters(plan.PlanCode)

var datacenterNames []string
if humanLevel > 0 {
datacenterNames = datacenters.ToFullNamesOrCodes()
} else {
datacenterNames = datacenters.Codes()
}
var datacenterNames []string
if humanLevel > 0 {
datacenterNames = datacenters.ToFullNamesOrCodes()
} else {
datacenterNames = datacenters.Codes()
}

status := datacenters.Status()
if status == kimsufiavailability.StatusAvailable {
nothingAvailable = false
}
status := datacenters.Status()
if status == kimsufiavailability.StatusAvailable {
nothingAvailable = false
}

// Display plan
fmt.Fprintf(w, "%s\t%s\t%s\t%.2f %s\t%s\t%s\n", plan.PlanCode, planCategory.DisplayName, plan.InvoiceName, price, catalog.Locale.CurrencyCode, status, strings.Join(datacenterNames, ", "))
categoryDisplay := pkgcategory.GetDisplayName(plan.Blobs.Commercial.Range)
if categoryDisplay == "" {
categoryDisplay = plan.Blobs.Commercial.Range
}

// Display plan
fmt.Fprintf(w, "%s\t%s\t%s\t%.2f %s\t%s\t%s\n", plan.PlanCode, categoryDisplay, plan.InvoiceName, price, catalog.Locale.CurrencyCode, status, strings.Join(datacenterNames, ", "))
}
w.Flush()

Expand Down
10 changes: 10 additions & 0 deletions pkg/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ type category struct {
DisplayName string
}

func GetDisplayName(name string) string {
for _, category := range Categories {
if category.Name == name {
return category.DisplayName
}
}

return ""
}

// Contains checks if a category name is in the list of known categories.
func Contains(name string) bool {
for _, category := range Categories {
Expand Down

0 comments on commit 5b5218e

Please sign in to comment.