-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg.go
111 lines (96 loc) · 2.67 KB
/
pkg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"net/http"
"golang.org/x/text/language"
"lure.sh/lure/pkg/search"
)
type Package struct {
Name string `json:"name"`
Repository string `json:"repository"`
Version string `json:"version"`
Release int `json:"release"`
Epoch uint `json:"epoch"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Maintainer string `json:"maintainer"`
Architectures []string `json:"architectures"`
Licenses []string `json:"licenses"`
Provides []string `json:"provides"`
Conflicts []string `json:"conflicts"`
Replaces []string `json:"replaces"`
Depends map[string][]string `json:"depends"`
BuildDepends map[string][]string `json:"build_depends"`
}
func (p Package) FullVersion() string {
ver := fmt.Sprintf("%s-%d", p.Version, p.Release)
if p.Epoch != 0 {
ver = fmt.Sprintf("%d:%s", p.Epoch, ver)
}
return ver
}
func translatePkgs(langs []string, pkgs []search.Package) []Package {
out := make([]Package, len(pkgs))
for i, pkg := range pkgs {
out[i] = translatePkg(langs, pkg)
}
return out
}
func translatePkg(langs []string, pkg search.Package) Package {
return Package{
Name: pkg.Name,
Repository: pkg.Repository,
Version: pkg.Version,
Release: pkg.Release,
Epoch: pkg.Epoch,
Description: performTranslation(langs, pkg.Description),
Homepage: performTranslation(langs, pkg.Homepage),
Maintainer: performTranslation(langs, pkg.Maintainer),
Architectures: pkg.Architectures,
Licenses: pkg.Licenses,
Provides: pkg.Provides,
Conflicts: pkg.Conflicts,
Depends: pkg.Depends,
BuildDepends: pkg.BuildDepends,
}
}
func getLanguages(req *http.Request) []string {
al := req.Header.Get("Accept-Language")
lang := req.URL.Query().Get("lang")
if al == "" && lang == "" {
return nil
}
tags, _, _ := language.ParseAcceptLanguage(al)
if lang != "" {
langTag, err := language.Parse(lang)
if err == nil {
tags = append(tags, langTag)
}
}
return getLangBases(tags)
}
func getLangBases(langs []language.Tag) []string {
out := make([]string, len(langs)+1)
for i, lang := range langs {
base, _ := lang.Base()
out[i] = base.String()
}
return out
}
func performTranslation(langs []string, v map[string]string) string {
if len(langs) == 0 {
val, ok := v[""]
if !ok {
return "<unknown>"
}
return val
}
for _, name := range langs {
val, ok := v[name]
if !ok {
continue
}
return val
}
return "<unknown>"
}