1package parser
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 ptr[T any](v T) *T {
33 return &v
34}
35
36func split(line string) (string, string) {
37 parts := strings.SplitN(line, ":", 2)
38 return parts[0], parts[1]
39}
40
41func toInt(v string) int {
42 i, _ := strconv.Atoi(v)
43 return i
44}
45
46func Parse(lines []string) *Entry {
47 entry := &Entry{}
48 for _, line := range lines {
49 r, c := split(line)
50 switch r {
51 case "C":
52 entry.Checksum = c
53 case "V":
54 entry.Version = c
55 case "P":
56 entry.Name = c
57 case "A":
58 entry.Architecture = &c
59 case "S":
60 entry.PackageSize = toInt(c)
61 case "I":
62 entry.InstalledSize = toInt(c)
63 case "T":
64 entry.Description = c
65 case "U":
66 entry.Url = c
67 case "L":
68 entry.License = c
69 case "o":
70 entry.Origin = &c
71 case "m":
72 entry.Maintainer = &c
73 case "t":
74 entry.BuildTime = ptr(time.Unix(int64(toInt(c)), 0))
75 case "c":
76 entry.Commit = &c
77 case "k":
78 entry.ProviderPriority = ptr(toInt(c))
79 case "D":
80 entry.Dependencies = strings.Split(c, " ")
81 case "p":
82 entry.Dependencies = strings.Split(c, " ")
83 case "i":
84 entry.Dependencies = strings.Split(c, " ")
85 }
86 }
87 return entry
88}