apkdoc @ dd2a1c38a5a9f561d58b58da56d3230fd715fcde

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