From 9b04f9f7236563e1bf05711f6c82bb896327c434 Mon Sep 17 00:00:00 2001 From: Arthur Zamarin Date: Fri, 2 Aug 2024 16:02:12 +0300 Subject: [PATCH] package/overview: mark default on useflags with + Resolves: https://github.com/gentoo/soko/issues/29 Closes: https://bugs.gentoo.org/935395 Signed-off-by: Arthur Zamarin --- pkg/app/handler/packages/overview.templ | 2 +- pkg/app/handler/packages/utils.go | 7 ++++++- pkg/models/package.go | 24 +++++++++++++++++------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pkg/app/handler/packages/overview.templ b/pkg/app/handler/packages/overview.templ index 72019aa..291fee8 100644 --- a/pkg/app/handler/packages/overview.templ +++ b/pkg/app/handler/packages/overview.templ @@ -140,7 +140,7 @@ templ useFlagsGroup(title string, useflags []*models.Useflag) { diff --git a/pkg/app/handler/packages/utils.go b/pkg/app/handler/packages/utils.go index 7b319e0..99dbf53 100644 --- a/pkg/app/handler/packages/utils.go +++ b/pkg/app/handler/packages/utils.go @@ -6,6 +6,7 @@ package packages import ( "log/slog" "net/http" + "slices" "soko/pkg/database" "soko/pkg/models" "sort" @@ -140,7 +141,7 @@ func getParameterValue(parameterName string, r *http.Request) string { // getPackageUseflags retrieves all local USE flags, global USE // flags and use expands for a given package func getPackageUseflags(gpackage *models.Package) (localUseflags []*models.Useflag, filteredGlobalUseflags []*models.Useflag, useExpands map[string][]*models.Useflag) { - rawUseFlags := gpackage.AllUseflags() + rawUseFlags, defaultOn := gpackage.AllUseflags() if len(rawUseFlags) == 0 { return } @@ -163,6 +164,7 @@ func getPackageUseflags(gpackage *models.Package) (localUseflags []*models.Usefl var allGlobalUseflags []*models.Useflag useExpands = make(map[string][]*models.Useflag) for _, useflag := range tmp_useflags { + isDefaultOn := slices.Contains(defaultOn, useflag.Name) if useflag.Scope == "global" { allGlobalUseflags = append(allGlobalUseflags, useflag) } else if useflag.Scope == "local" { @@ -173,6 +175,9 @@ func getPackageUseflags(gpackage *models.Package) (localUseflags []*models.Usefl useflag.Name = strings.TrimPrefix(useflag.Name, useflag.UseExpand+"_") useExpands[useflag.UseExpand] = append(useExpands[useflag.UseExpand], useflag) } + if isDefaultOn { + useflag.Name = "+" + useflag.Name + } } // Only add global useflags that are not present in the local useflags diff --git a/pkg/models/package.go b/pkg/models/package.go index e0a1ce3..16f1b61 100644 --- a/pkg/models/package.go +++ b/pkg/models/package.go @@ -145,17 +145,27 @@ func (p Package) AllBugs() []*Bug { return allBugsList } -func (p *Package) AllUseflags() []string { - useflags := make(map[string]struct{}) +func (p *Package) AllUseflags() (useflags []string, defaultOn []string) { + useflagsSet := make(map[string]struct{}) + defaultOnSet := make(map[string]struct{}) for _, version := range p.Versions { for _, useflag := range version.Useflags { - useflags[strings.TrimPrefix(useflag, "+")] = struct{}{} + if useflag[0] == '+' { + defaultOnSet[useflag[1:]] = struct{}{} + useflagsSet[useflag[1:]] = struct{}{} + } else { + useflagsSet[useflag] = struct{}{} + } } } - useflagsList := make([]string, 0, len(useflags)) - for useflag := range useflags { - useflagsList = append(useflagsList, useflag) + useflags = make([]string, 0, len(useflagsSet)) + for useflag := range useflagsSet { + useflags = append(useflags, useflag) + } + defaultOn = make([]string, 0, len(defaultOnSet)) + for useflag := range defaultOnSet { + defaultOn = append(defaultOn, useflag) } - return useflagsList + return }