This repository was archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget.go
185 lines (131 loc) · 5.16 KB
/
get.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
183
184
185
package main
import (
"os"
"strconv"
"strings"
)
// Writes a packages info to the proper location.
func writePkg(pkgName string, pkgFile string) {
log(1, "Writing package info for %s...", bolden(pkgName))
newFile(infoPath+pkgName+".json", pkgFile, "An error occurred while writing package information for %s", pkgName)
}
// Finds a packages info in the repos.
func findPkg(pkgName string) string {
log(1, "Finding package %s...", bolden(pkgName))
urls := parseSources()
validInfos, validUrls := make([]string, 0), make([]string, 0)
log(1, "Checking urls length...")
if urlsLen := len(urls); urlsLen <= 0 {
errorLogRaw("You don't have any sources defined in %s", bolden(configPath+"sources.txt"))
os.Exit(1)
} else if urlsLen == 1 {
debugLog("Only one source defined in %s. Using that source.", bolden(configPath+"sources.txt"))
log(1, "Getting package info for %s...", bolden(pkgName))
pkgURL := parseURL(urls[0], false) + "/" + pkgName + ".json"
log(1, "Getting info from %s...", bolden(pkgURL))
pkgFile, statusCode, err := viewFile(pkgURL)
errorLog(err, "An error occurred while getting package information for %s", bolden(pkgName))
debugLog("Status code: %d", statusCode)
if checkFor404(statusCode, pkgName) {
errorLogRaw("Package %s not found", bolden(pkgName))
os.Exit(1)
}
errorLog(err, "An error occurred while getting package information for %s", bolden(pkgName))
return pkgFile
}
rawLog("\n")
for _, url := range urls {
pkgURL := parseURL(url, false) + "/" + pkgName + ".json"
log(1, "Checking %s for package info...", bolden(pkgURL))
infoFile, statusCode, err := viewFile(pkgURL)
errorLog(err, "An error occurred while getting package information for %s", bolden(pkgName))
debugLog("Status code: %d", statusCode)
if checkFor404(statusCode, pkgName) {
log(3, "Not found in %s", bolden(pkgURL))
rawLog("\n")
continue
}
errorLog(err, "An error occurred while getting package information for %s", bolden(pkgName))
log(0, "Found %s in %s!", bolden(pkgName), bolden(pkgURL))
log(1, "Saving valid info & url...")
rawLog("\n")
validInfos = append(validInfos, infoFile)
validUrls = append(validUrls, pkgURL)
}
log(1, "Checking valid info...")
lenValidInfos := len(validInfos)
switch {
case lenValidInfos < 1:
errorLogRaw("Package %s not found in any repo", bolden(pkgName))
os.Exit(1)
case lenValidInfos == 1:
log(1, "Only 1 valid repo found. Using that repo...")
return validInfos[0]
default:
log(1, "Multiple packages found. Please choose one:")
for i, url := range validUrls {
log(1, "%d) %s - %s", i, bolden(url), repoLabel(url, false))
}
choice := input("0", "Number between 0 and %d or q to quit", lenValidInfos-1)
if strings.Contains(choice, "q") {
os.Exit(1)
} else {
convChoice, err := strconv.Atoi(choice)
errorLog(err, "An error occurred while converting choice to int")
return validInfos[convChoice]
}
}
return ""
}
// Gets a packages info from the internet.
func getPkgFromNet(pkgName string) (Package, string) {
packageFile := findPkg(pkgName)
pkg := loadPkg(packageFile, pkgName)
return pkg, packageFile
}
// Gets a packages info from a URL.
func getPkgFromURL(pkgName string, url string) string {
packageFile, statusCode, err := viewFile(url)
errorLog(err, "An error occurred while getting package information for %s", bolden(pkgName))
if checkFor404(statusCode, pkgName) {
errorLogRaw("The info URL provided from %s does not exist", bolden(pkgName))
}
return packageFile
}
// Downloads data for a package directly.
func doDirectDownload(pkg Package, srcPath string) {
pkgDispName := bolden(pkg.Name)
log(1, "Making sure %s is not already downloaded...", pkgDispName)
delPath(false, srcPath+pkg.Name, "An error occurred while deleting temporary downloaded files for %s", pkgDispName)
log(1, "Getting download URL for %s", pkgDispName)
url := getDownloadURL(pkg)
log(1, "Making directory for %s...", pkgDispName)
newDir(srcPath+pkg.Name, "An error occurred while creating temporary directory for %s", pkgDispName)
log(1, "Downloading file for %s from %s...", pkgDispName, bolden(url))
nameOfFile := srcPath + pkg.Name + "/" + pkg.Name + ".indiepkg.downloaded"
debugLog("Downloading and saving to %s", bolden(nameOfFile))
downloadFileWithProg(nameOfFile, url, "An error occurred while downloading file for %s", pkgDispName)
}
// Gets info for a package.
func getPkgInfo(pkgName string, isURL bool) string {
var pkgFile string
switch {
case isURL: // Run this if a URL is selected
log(1, "Reading info from direct URL...")
parsedURL := parseURL(pkgName, false)
raw, statusCode, err := viewFile(parsedURL)
pkgFile = raw
errorLog(err, "An error occurred while getting info from %s", bolden(pkgName))
if checkFor404(statusCode, pkgName) {
errorLogRaw("Package %s not found", bolden(pkgName))
os.Exit(1)
}
case strings.HasSuffix(pkgName, ".json"): // Run this if a file is selected
log(1, "Reading info from file...")
pkgFile = readFile(pkgName, "An error occurred while reading %s", bolden(pkgName))
default: // Run this to read from repos
log(1, "Reading info from official repositories...")
pkgFile = findPkg(pkgName)
}
return pkgFile
}