1package main
2
3import (
4 "strconv"
5 "strings"
6 "time"
7)
8
9type (
10 // https://wiki.alpinelinux.org/wiki/Apk_spec
11 Entry struct {
12 Checksum string // C
13 Version string // V
14 Name string // P
15 Architecture *string // A
16 PackageSize int // S
17 InstalledSize int // I
18 Description string // T
19 Url string // U
20 License string // L
21 Origin *string // o
22 Maintainer *string // m
23 BuildTime *time.Time // t
24 Commit *string // c
25 ProviderPriority *int // k
26 Dependencies []string // D
27 Provides []string // p
28 InstallIf []string // i
29 }
30)
31
32func (e *Entry) Properties() map[string]string {
33 p := make(map[string]string)
34
35 p["checksum"] = e.Checksum
36 p["version"] = e.Version
37 p["name"] = e.Name
38 p["package size"] = strconv.Itoa(e.PackageSize)
39 p["installed size"] = strconv.Itoa(e.InstalledSize)
40 p["description"] = e.Description
41 p["url"] = e.Url
42 p["license"] = e.License
43
44 if e.Architecture != nil {
45 p["architecture"] = *e.Architecture
46 }
47
48 if e.Origin != nil {
49 p["origin"] = *e.Origin
50 }
51
52 if e.Maintainer != nil {
53 p["maintainer"] = *e.Maintainer
54 }
55
56 if e.BuildTime != nil {
57 p["build time"] = e.BuildTime.String()
58 }
59
60 if e.Commit != nil {
61 p["commit"] = *e.Commit
62 }
63
64 if e.ProviderPriority != nil {
65 p["provider priority"] = strconv.Itoa(*e.ProviderPriority)
66 }
67
68 if len(e.Dependencies) > 0 {
69 p["dependencies"] = strings.Join(e.Dependencies, " ")
70 }
71
72 if len(e.Provides) > 0 {
73 p["provides"] = strings.Join(e.Provides, " ")
74 }
75
76 if len(e.InstallIf) > 0 {
77 p["install if"] = strings.Join(e.InstallIf, " ")
78 }
79
80 return p
81}
82
83func ptr[T any](v T) *T {
84 return &v
85}
86
87func split(line string) (string, string) {
88 parts := strings.SplitN(line, ":", 2)
89 return parts[0], parts[1]
90}
91
92func toInt(v string) int {
93 i, _ := strconv.Atoi(v)
94 return i
95}
96
97func Parse(lines []string) *Entry {
98 entry := &Entry{}
99 for _, line := range lines {
100 r, c := split(line)
101 switch r {
102 case "C":
103 entry.Checksum = c
104 case "V":
105 entry.Version = c
106 case "P":
107 entry.Name = c
108 case "A":
109 entry.Architecture = &c
110 case "S":
111 entry.PackageSize = toInt(c)
112 case "I":
113 entry.InstalledSize = toInt(c)
114 case "T":
115 entry.Description = c
116 case "U":
117 entry.Url = c
118 case "L":
119 entry.License = c
120 case "o":
121 entry.Origin = &c
122 case "m":
123 entry.Maintainer = &c
124 case "t":
125 entry.BuildTime = ptr(time.Unix(int64(toInt(c)), 0))
126 case "c":
127 entry.Commit = &c
128 case "k":
129 entry.ProviderPriority = ptr(toInt(c))
130 case "D":
131 entry.Dependencies = strings.Split(c, " ")
132 case "p":
133 entry.Dependencies = strings.Split(c, " ")
134 case "i":
135 entry.Dependencies = strings.Split(c, " ")
136 }
137 }
138 return entry
139}