1package parser
2
3import (
4 "fmt"
5 "strconv"
6 "strings"
7 "time"
8)
9
10type (
11 // https://wiki.alpinelinux.org/wiki/Apk_spec
12 Entry struct {
13 Checksum string // C
14 Version string // V
15 Name string // P
16 Architecture *string // A
17 PackageSize int // S
18 InstalledSize int // I
19 Description string // T
20 Url string // U
21 License string // L
22 Origin *string // o
23 Maintainer *string // m
24 BuildTime *time.Time // t
25 Commit *string // c
26 ProviderPriority *int // k
27 Dependencies []string // D
28 Provides []string // p
29 InstallIf []string // i
30 }
31)
32
33func (e *Entry) FomartLink(format string) string {
34 c := strings.Replace(*e.Commit, "-dirty", "", -1)
35 return fmt.Sprintf(format, c, *e.Origin)
36}
37
38func ptr[T any](v T) *T {
39 return &v
40}
41
42func split(line string) (string, string) {
43 parts := strings.SplitN(line, ":", 2)
44 return parts[0], parts[1]
45}
46
47func toInt(v string) int {
48 i, _ := strconv.Atoi(v)
49 return i
50}
51
52func Parse(lines []string) *Entry {
53 entry := &Entry{}
54 for _, line := range lines {
55 r, c := split(line)
56 switch r {
57 case "C":
58 entry.Checksum = c
59 case "V":
60 entry.Version = c
61 case "P":
62 entry.Name = c
63 case "A":
64 entry.Architecture = &c
65 case "S":
66 entry.PackageSize = toInt(c)
67 case "I":
68 entry.InstalledSize = toInt(c)
69 case "T":
70 entry.Description = c
71 case "U":
72 entry.Url = c
73 case "L":
74 entry.License = c
75 case "o":
76 entry.Origin = &c
77 case "m":
78 entry.Maintainer = &c
79 case "t":
80 entry.BuildTime = ptr(time.Unix(int64(toInt(c)), 0))
81 case "c":
82 entry.Commit = &c
83 case "k":
84 entry.ProviderPriority = ptr(toInt(c))
85 case "D":
86 entry.Dependencies = strings.Split(c, " ")
87 case "p":
88 entry.Dependencies = strings.Split(c, " ")
89 case "i":
90 entry.Dependencies = strings.Split(c, " ")
91 }
92 }
93 return entry
94}