aboutsummaryrefslogtreecommitdiff
path: root/parser.go
blob: 196dc94d5000f968230a1d9ac7f52bce36c25e5d (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main

import (
	"strconv"
	"strings"
	"time"
)

type (
	// https://wiki.alpinelinux.org/wiki/Apk_spec
	Entry struct {
		Checksum         string     // C
		Version          string     // V
		Name             string     // P
		Architecture     *string    // A
		PackageSize      int        // S
		InstalledSize    int        // I
		Description      string     // T
		Url              string     // U
		License          string     // L
		Origin           *string    // o
		Maintainer       *string    // m
		BuildTime        *time.Time // t
		Commit           *string    // c
		ProviderPriority *int       // k
		Dependencies     []string   // D
		Provides         []string   // p
		InstallIf        []string   // i
	}
)

func (e *Entry) Properties() map[string]string {
	p := make(map[string]string)

	p["checksum"] = e.Checksum
	p["version"] = e.Version
	p["name"] = e.Name
	p["package size"] = strconv.Itoa(e.PackageSize)
	p["installed size"] = strconv.Itoa(e.InstalledSize)
	p["description"] = e.Description
	p["url"] = e.Url
	p["license"] = e.License

	if e.Architecture != nil {
		p["architecture"] = *e.Architecture
	}

	if e.Origin != nil {
		p["origin"] = *e.Origin
	}

	if e.Maintainer != nil {
		p["maintainer"] = *e.Maintainer
	}

	if e.BuildTime != nil {
		p["build time"] = e.BuildTime.String()
	}

	if e.Commit != nil {
		p["commit"] = *e.Commit
	}

	if e.ProviderPriority != nil {
		p["provider priority"] = strconv.Itoa(*e.ProviderPriority)
	}

	if len(e.Dependencies) > 0 {
		p["dependencies"] = strings.Join(e.Dependencies, " ")
	}

	if len(e.Provides) > 0 {
		p["provides"] = strings.Join(e.Provides, " ")
	}

	if len(e.InstallIf) > 0 {
		p["install if"] = strings.Join(e.InstallIf, " ")
	}

	return p
}

func ptr[T any](v T) *T {
	return &v
}

func split(line string) (string, string) {
	parts := strings.SplitN(line, ":", 2)
	return parts[0], parts[1]
}

func toInt(v string) int {
	i, _ := strconv.Atoi(v)
	return i
}

func Parse(lines []string) *Entry {
	entry := &Entry{}
	for _, line := range lines {
		r, c := split(line)
		switch r {
		case "C":
			entry.Checksum = c
		case "V":
			entry.Version = c
		case "P":
			entry.Name = c
		case "A":
			entry.Architecture = &c
		case "S":
			entry.PackageSize = toInt(c)
		case "I":
			entry.InstalledSize = toInt(c)
		case "T":
			entry.Description = c
		case "U":
			entry.Url = c
		case "L":
			entry.License = c
		case "o":
			entry.Origin = &c
		case "m":
			entry.Maintainer = &c
		case "t":
			entry.BuildTime = ptr(time.Unix(int64(toInt(c)), 0))
		case "c":
			entry.Commit = &c
		case "k":
			entry.ProviderPriority = ptr(toInt(c))
		case "D":
			entry.Dependencies = strings.Split(c, " ")
		case "p":
			entry.Dependencies = strings.Split(c, " ")
		case "i":
			entry.Dependencies = strings.Split(c, " ")
		}
	}
	return entry
}