-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-apt-search.go
182 lines (171 loc) · 6.37 KB
/
go-apt-search.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package goaptsearch
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// absolute path to apt lists files
const aptListPath = "/var/lib/apt/lists/"
type APTPackages struct {
PackageName string `json:"Package"`
Version string `json:"Version"`
Architecture string `jsone:"Architecture"`
Depends []string `json:"Depends"`
Description string `json:"Description"`
Section string `json:"Section"`
Md5sum string `json:"MD5sum"`
Sha256 string `json:"SHA256"`
}
type RepoArchive struct {
Domain string `json:"Domain"`
Distribution string `json:"Distribution"`
Area string `json:"Area"`
Architecture string `json:"Architecture"`
ListFileName string `json:"ListFileName"`
}
// AptSearch: allows to perform a targeted search using the exact name of the package to be searched,
//
// or a keyword search that will result in all packages that include that string in the name
func AptSearch(searchPackage string, packagesList []APTPackages, searchExactName bool) ([]APTPackages, error) {
var filteredPackageList []APTPackages
for _, singlePackage := range packagesList {
if searchExactName {
if singlePackage.PackageName == searchPackage {
filteredPackageList = append(filteredPackageList, singlePackage)
}
} else {
if strings.Contains(singlePackage.PackageName, searchPackage) {
filteredPackageList = append(filteredPackageList, singlePackage)
}
}
}
if len(filteredPackageList) == 0 {
return nil, fmt.Errorf("package %s not found, try performing an apt update", searchPackage)
}
return filteredPackageList, nil
}
// AptListALL: scan the all source.list on the system and return the list of all available packages
func AptListAllPackages() ([]APTPackages, error) {
allPackagesFiles, errGetRepoFileList := getRepoFileList()
if errGetRepoFileList != nil {
return nil, errGetRepoFileList
}
allPackagesList, errBuildPackagesList := buildPackagesList(allPackagesFiles)
if errBuildPackagesList != nil {
return nil, errBuildPackagesList
}
return allPackagesList, nil
}
// AptListPackagesInRepo: scans only specific source.lists and returns the packages available into them
func AptListPackagesInRepo(selectedRepo []RepoArchive) ([]APTPackages, error) {
if len(selectedRepo) == 0 {
return nil, fmt.Errorf("please provide at least one repository")
}
var repoFileList []string
for _, selectedRepoFile := range selectedRepo {
repoFileList = append(repoFileList, selectedRepoFile.ListFileName)
}
filteredPackagesList, errBuildPackagesList := buildPackagesList(repoFileList)
if errBuildPackagesList != nil {
return nil, errBuildPackagesList
}
return filteredPackagesList, nil
}
// GetRepoDomain: returns a list of currently active repositories by distribution and area
func GetAvailableRepo() ([]RepoArchive, error) {
repoList, errGetRepoFileList := getRepoFileList()
if errGetRepoFileList != nil {
return nil, errGetRepoFileList
}
var repoDomainList []RepoArchive
for _, repo := range repoList {
repoDomain := strings.Split(repo, "_")
var extractedDistribution string
var extractedArea string
for i, repoFields := range repoDomain {
if repoFields == "dists" {
extractedDistribution = repoDomain[i+1]
extractedArea = repoDomain[i+2]
}
}
repoDomainList = append(repoDomainList, RepoArchive{
Domain: repoDomain[0],
Distribution: extractedDistribution,
Area: extractedArea,
Architecture: repoDomain[len(repoDomain)-2],
ListFileName: repo,
})
}
return repoDomainList, nil
}
// getRepoFileList: read files from /var/lib/apt/lists and return only packages
//
// I preferred to use os.ReadDir instead of filepath.Walk because I am not interested in the list of files in the partial directory
func getRepoFileList() ([]string, error) {
allPackagesFiles, errReadDir := os.ReadDir(aptListPath)
if errReadDir != nil {
return nil, errReadDir
}
var matchingPackagesFiles []string
filterPackagesFile, _ := regexp.Compile(`.*\_Packages$`)
for _, packagesFile := range allPackagesFiles {
if filterPackagesFile.MatchString(packagesFile.Name()) {
matchingPackagesFiles = append(matchingPackagesFiles, packagesFile.Name())
}
}
return matchingPackagesFiles, nil
}
// buildPackagesList: return packages available from a list of repositories
func buildPackagesList(repoList []string) ([]APTPackages, error) {
var packagesList []APTPackages
for _, packagesFile := range repoList {
readPackageFile, errOpen := os.ReadFile(filepath.Join(aptListPath, packagesFile))
if errOpen != nil {
return nil, errOpen
}
lines := strings.Split(string(readPackageFile), "\n")
var packageNameFromList string
var versionFromList string
var architectureFromList string
var dependsFromList []string
var descriptionFromList string
var sectionFromList string
var md5sumFromList string
var sha256FromList string
for _, line := range lines {
if strings.HasPrefix(line, "Package:") {
packageNameFromList, _ = strings.CutPrefix(line, "Package:")
} else if strings.HasPrefix(line, "Version:") {
versionFromList, _ = strings.CutPrefix(line, "Version:")
} else if strings.HasPrefix(line, "Architecture:") {
architectureFromList, _ = strings.CutPrefix(line, "Architecture:")
} else if strings.HasPrefix(line, "Depends:") {
dependsList, _ := strings.CutPrefix(line, "Depends:")
dependsFromList = strings.Split(dependsList, ",")
} else if strings.HasPrefix(line, "Description:") {
descriptionFromList, _ = strings.CutPrefix(line, "Description:")
} else if strings.HasPrefix(line, "Section:") {
sectionFromList, _ = strings.CutPrefix(line, "Section:")
} else if strings.HasPrefix(line, "MD5sum:") {
md5sumFromList, _ = strings.CutPrefix(line, "MD5sum:")
} else if strings.HasPrefix(line, "SHA256:") {
sha256FromList, _ = strings.CutPrefix(line, "SHA256:")
} else if line == "" {
// information dump because each new line starts a new package
packagesList = append(packagesList, APTPackages{
PackageName: strings.TrimSpace(packageNameFromList),
Version: strings.TrimSpace(versionFromList),
Architecture: strings.TrimSpace(architectureFromList),
Depends: dependsFromList,
Description: strings.TrimSpace(descriptionFromList),
Section: strings.TrimSpace(sectionFromList),
Md5sum: strings.TrimSpace(md5sumFromList),
Sha256: strings.TrimSpace(sha256FromList),
})
}
}
}
return packagesList, nil
}