This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
repo.go
157 lines (119 loc) · 4.07 KB
/
repo.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
package main
import (
"strings"
)
// Parse URL to be consistent.
func parseURL(url string, silent bool) string {
if !silent {
log(1, "Parsing URL %s...", bolden(url))
}
repoURL := url
// Trim
repoURL = strings.TrimSpace(repoURL)
repoURL = strings.TrimSuffix(repoURL, "/")
// Change protocol to http://
if split := strings.Split(repoURL, "//"); strings.Contains(repoURL, "://") {
repoURL = "http://" + strings.Join(split[1:], "://")
} else {
repoURL = "http://" + repoURL
}
debugLog("Parsed URL: %s", repoURL)
return repoURL
}
// Read sources file & return it's contents.
func readSources() ([]string, string) {
log(1, "Reading sources file...")
raw := readFile(configPath+"sources.txt", "An error occurred while reading sources file")
log(1, "Parsing sources file...")
trimmed := strings.TrimSpace(raw)
return strings.Split(trimmed, "\n"), trimmed
}
// Remove empty lines or lines that begin with #.
func stripSources(sourcesFile string, noParse bool) ([]string, string) {
log(1, "Stripping sources file of comments...")
final := []string{}
finalStr := ""
// Iterate through each line
for _, line := range strings.Split(strings.TrimSpace(sourcesFile), "\n") {
if strings.HasPrefix(line, "#") || strings.TrimSpace(line) == "" { // Skip comments and empty lines
continue
}
parsedLine := line
if !noParse { // Parse URL if noParse is set to false
parsedLine = parseURL(line, true)
}
finalStr += parsedLine + "\n"
final = append(final, parsedLine)
}
return final, finalStr
}
// Write to sources file.
func saveChanges(sourcesFile string) {
log(1, "Saving changes...")
newFile(configPath+"sources.txt", sourcesFile, "An error occurred while saving changes to sources file")
}
// Add repo to sources file.
func addRepo(repoLink string) {
if !isValidURL(repoLink) {
if force {
log(3, "Invalid url, but continuing because force is set to true.")
} else {
errorLogRaw("Invalid url: %s", bolden(repoLink))
}
}
_, sourcesFile := readSources()
_, stripped := stripSources(sourcesFile, true)
if strings.Contains(stripped, repoLink) { // Check if repo already exists in sources file
if force {
log(3, "Repo %s already exists in sources file, but continuing because force is set to true.", bolden(repoLink))
} else {
errorLogRaw("Repo %s already exists in sources file", bolden(repoLink))
}
}
log(1, "Appending %s to sources file...", bolden(repoLink))
sourcesFile = strings.TrimSpace(sourcesFile + "\n" + repoLink)
saveChanges(sourcesFile)
}
// Remove repo from sources file.
func rmRepo(repoLink string) {
repos, _ := readSources()
log(1, "Removing %s from sources file...", bolden(repoLink))
final := ""
for i, repo := range repos {
if repo == repoLink {
debugLog("Match found at index %d.", i)
continue
}
final = final + repo + "\n"
}
sourcesFile := strings.TrimSpace(final)
saveChanges(sourcesFile)
}
// List repos in the sources file.
func listRepos() {
rawRepos, _ := readSources()
repos, _ := stripSources(strings.Join(rawRepos, "\n"), true)
log(1, "Repos:")
for _, repo := range repos {
rawLogf(" %s - %s\n", bolden(repo), repoLabel(repo, false))
}
}
// Return a label to the provided repo.
func repoLabel(repo string, includeLink bool) string {
prefixes := [][]string{ // Using a 2D array to retain order of elements
{"http://raw.githubusercontent.com/talwat/indiepkg/main/packages/linux-only", textCol.Blue + "(Linux only)" + RESETCOL},
{"http://raw.githubusercontent.com/talwat/indiepkg/main/packages/bin", textCol.Violet + "(Binary package)" + RESETCOL},
{"http://raw.githubusercontent.com/talwat/indiepkg/main", textCol.Cyan + "(Official repo)" + RESETCOL},
{"http://raw.githubusercontent.com/talwat/indiepkg", textCol.Blue + "(Other branch)" + RESETCOL},
}
// Iterate through 2D array
for prefix := range prefixes {
if strings.HasPrefix(parseURL(repo, true), prefixes[prefix][0]) { // Check prefix matches
return prefixes[prefix][1]
}
}
if includeLink {
return textCol.Yellow + "(Third party repo: " + repo + ")" + RESETCOL
}
return textCol.Yellow + "(Third party repo)" + RESETCOL
}