-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.go
160 lines (125 loc) · 2.9 KB
/
packages.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package apk
import (
"bufio"
"os"
"reflect"
"strconv"
"strings"
"time"
)
// Packages holds a series of packages, as described in an APKINDEX file
type Packages []*Package
// ReadPackages takes an APKINDEX files and parses the individual files contained
// therein
func ReadPackages(fn string) (p Packages, err error) {
var (
sb strings.Builder
pkg *Package
)
p = make(Packages, 0)
f, err := os.Open(fn) // #nosec: G304
if err != nil {
return
}
defer f.Close() // #nosec: G307
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
pkg, err = DecodePackage(sb.String())
if err != nil {
return
}
p = append(p, pkg)
sb.Reset()
continue
}
sb.WriteString(line)
sb.WriteString("\n")
}
// decode final
pkg, err = DecodePackage(sb.String())
if err != nil {
return
}
p = append(p, pkg)
err = scanner.Err()
return
}
// Package represents a package from the Index file
// Keys and names and stuff are taken from
// https://github.com/alpinelinux/apk-tools/blob/da8d83338b7daa2bbd2e940f94d6515d69568800/src/apk_adb.c#L56-L76
type Package struct {
ID string `apk:"C"`
Name string `apk:"P"`
Version string `apk:"V"`
Description string `apk:"T"`
URL string `apk:"U"`
InstalledSize int `apk:"I"`
FileSize int `apk:"S"`
Licence string `apk:"L"`
Arch string `apk:"A"`
Dependencies []string `apk:"D"`
InstallIf string `apk:"i"`
Provides []string `apk:"p"`
Origin string `apk:"o"`
Maintainer string `apk:"m"`
BuildTime time.Time `apk:"t"`
RepoCommit string `apk:"c"`
Replaces string `apk:"r"`
Priority int `apk:"k"`
}
// DecodePackage takes a block of text and tries to decode a
// package from it
func DecodePackage(in string) (*Package, error) {
p := &Package{}
kvs := kv(in)
ev := reflect.Indirect(reflect.ValueOf(p))
et := ev.Type()
for i := 0; i < et.NumField(); i++ {
field, val := et.Field(i), ev.Field(i)
key, ok := field.Tag.Lookup("apk")
if !ok {
continue
}
setVal, ok := kvs[key]
if !ok {
continue
}
switch tt := field.Type.String(); tt {
case "string":
val.SetString(setVal)
case "[]string":
val.Set(reflect.ValueOf(strings.Fields(setVal)))
case "time.Time", "Time":
i, err := strconv.Atoi(setVal)
if err != nil {
return p, err
}
val.Set(reflect.ValueOf(time.Unix(int64(i), 0)))
case "int":
i, err := strconv.Atoi(setVal)
if err != nil {
return p, err
}
val.SetInt(int64(i))
default:
continue
}
}
return p, nil
}
func kv(in string) (out map[string]string) {
out = make(map[string]string)
for _, line := range strings.Split(in, "\n") {
elems := strings.SplitN(line, ":", 2)
if elems == nil {
continue
}
if len(elems) != 2 {
continue
}
out[elems[0]] = elems[1]
}
return
}