Skip to content

Commit 890ad35

Browse files
committed
Added RepositoryList struct helper
1 parent 1e54b12 commit 890ad35

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

repos.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ import (
2828
"strings"
2929
)
3030

31+
// RepositoryList is an array of Repository definitions
32+
type RepositoryList []*Repository
33+
34+
// Contains check if a repository definition is already contained
35+
// in the RepositoryList
36+
func (r RepositoryList) Contains(repo *Repository) bool {
37+
for _, i := range r {
38+
if repo.Equals(i) {
39+
return true
40+
}
41+
}
42+
return false
43+
}
44+
3145
// Repository is a repository installed in the system
3246
type Repository struct {
3347
Enabled bool
@@ -39,6 +53,27 @@ type Repository struct {
3953
Comment string
4054
}
4155

56+
// Equals check if the Repository definition is equivalent to the
57+
// one provided as parameter
58+
func (r *Repository) Equals(repo *Repository) bool {
59+
if r.Components != repo.Components {
60+
return false
61+
}
62+
if r.Distribution != repo.Distribution {
63+
return false
64+
}
65+
if r.URI != repo.URI {
66+
return false
67+
}
68+
if r.SourceRepo != repo.SourceRepo {
69+
return false
70+
}
71+
if r.Options != repo.Options {
72+
return false
73+
}
74+
return true
75+
}
76+
4277
// APTConfigLine returns the source.list config line for the Repository
4378
func (r *Repository) APTConfigLine() string {
4479
res := ""
@@ -80,14 +115,14 @@ func parseAPTConfigLine(line string) *Repository {
80115
}
81116
}
82117

83-
func parseAPTConfigFile(configPath string) ([]*Repository, error) {
118+
func parseAPTConfigFile(configPath string) (RepositoryList, error) {
84119
data, err := ioutil.ReadFile(configPath)
85120
if err != nil {
86121
return nil, fmt.Errorf("Reading %s: %s", configPath, err)
87122
}
88123
scanner := bufio.NewScanner(bytes.NewReader(data))
89124

90-
res := []*Repository{}
125+
res := RepositoryList{}
91126
for scanner.Scan() {
92127
line := scanner.Text()
93128
repo := parseAPTConfigLine(line)
@@ -101,7 +136,7 @@ func parseAPTConfigFile(configPath string) ([]*Repository, error) {
101136

102137
// ParseAPTConfigFolder scans an APT config folder (usually /etc/apt) to
103138
// get information about configured repositories
104-
func ParseAPTConfigFolder(folderPath string) ([]*Repository, error) {
139+
func ParseAPTConfigFolder(folderPath string) (RepositoryList, error) {
105140
sources := []string{filepath.Join(folderPath, "sources.list")}
106141

107142
sourcesFolder := filepath.Join(folderPath, "sources.list.d")
@@ -115,7 +150,7 @@ func ParseAPTConfigFolder(folderPath string) ([]*Repository, error) {
115150
}
116151
}
117152

118-
res := []*Repository{}
153+
res := RepositoryList{}
119154
for _, source := range sources {
120155
repos, err := parseAPTConfigFile(source)
121156
if err != nil {

0 commit comments

Comments
 (0)