This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
package.go
70 lines (56 loc) · 1.53 KB
/
package.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
package debianupdate
import (
"errors"
"strings"
)
/*
* Implement a Debian Package
*/
type Package struct {
Name string
Version string
Hash string
}
type PackageSlice []*Package
// Len is part of sort.Interface.
func (d PackageSlice) Len() int {
return len(d)
}
// Swap is part of sort.Interface.
func (d PackageSlice) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
// Less is part of sort.Interface. We use count as the value to sort by
func (d PackageSlice) Less(i, j int) bool {
return d[i].Name < d[j].Name
}
// NewPackage takes an input string of the form
// Package: name
// Version: 1.0+blabla
// SHA256: SOMEHASH
// and some other fields
func NewPackage(packageString string) (*Package, error) {
lines := strings.Split(packageString, "\n")
if len(lines) < 3 {
return nil, errors.New("Should have at least 3 lines " + packageString)
}
p := &Package{}
for _, line := range lines {
if strings.Contains(line, "Package:") {
p.Name = strings.Replace(line, "Package: ", "", 1)
} else if strings.Contains(line, "Version:") {
p.Version = strings.Replace(line, "Version: ", "", 1)
} else if strings.Contains(line, "SHA256:") {
p.Hash = strings.Replace(line, "SHA256: ", "", 1)
}
// For this example, we only verify if these fields are valid
// and we end the parsing as soon as all of them are filled.
if p.Name != "" && p.Version != "" && p.Hash != "" {
return p, nil
}
}
if p.Name == "" || p.Version == "" || p.Hash == "" {
return nil, errors.New("Invalid package \n" + packageString)
}
return p, nil
}