Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting applications #93

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions internal/hajimari/customapps/customapps.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package customapps

import (
"strings"

"github.com/toboshii/hajimari/internal/config"
"github.com/toboshii/hajimari/internal/models"
utilStrings "github.com/toboshii/hajimari/internal/util/strings"
)

// List struct is used for listing hajimari apps
Expand All @@ -27,7 +26,7 @@ func (al *List) Populate() *List {
var customApps []models.AppGroup

for _, group := range al.appConfig.CustomApps {
group.Group = strings.ToLower(group.Group)
group.Group = utilStrings.NormalizeString(group.Group)
customApps = append(customApps, group)
}

Expand Down
7 changes: 7 additions & 0 deletions internal/handlers/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package handlers

import (
"net/http"
"sort"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/toboshii/hajimari/internal/config"
"github.com/toboshii/hajimari/internal/hajimari/customapps"
"github.com/toboshii/hajimari/internal/models"
"github.com/toboshii/hajimari/internal/services"
utilStrings "github.com/toboshii/hajimari/internal/util/strings"
)

type appResource struct {
Expand Down Expand Up @@ -57,6 +59,11 @@ func (rs *appResource) ListApps(w http.ResponseWriter, r *http.Request) {
customApps = append(customApps[:x], customApps[x+1:]...)
}
}

// Sort ingressApps[i].Apps alphabetically
sort.Slice(ingressApps[i].Apps, func(j, k int) bool {
return utilStrings.CompareNormalized(ingressApps[i].Apps[j].Name, ingressApps[i].Apps[k].Name) == -1
})
}

apps = append(ingressApps, customApps...)
Expand Down
4 changes: 2 additions & 2 deletions internal/kube/wrappers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func (iw *IngressWrapper) GetNamespace() string {
// GetGroup func extracts group name from the ingress
func (iw *IngressWrapper) GetGroup() string {
if groupFromAnnotation := iw.GetAnnotationValue(annotations.HajimariGroupAnnotation); groupFromAnnotation != "" {
return groupFromAnnotation
return utilStrings.NormalizeString(groupFromAnnotation)
}
return iw.GetNamespace()
return utilStrings.NormalizeString(iw.GetNamespace())
}

// GetGroup func extracts group name from the ingress
Expand Down
10 changes: 10 additions & 0 deletions internal/util/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ func ContainsBetweenDelimiter(fullString string, search string, delimiter string
}
return false
}

// NormalizeString trims extra spaces and changes the string to lower-case
func NormalizeString(str string) string {
return strings.TrimSpace(strings.ToLower(str))
}

// CompareNormalized compares two strings after normalizing them
func CompareNormalized(a string, b string) int {
return strings.Compare(NormalizeString(a), NormalizeString(b))
}