This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shovel.go
155 lines (140 loc) · 3.83 KB
/
shovel.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
package main
import (
"bytes"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/Jeffail/gabs/v2"
)
// configure
var workDir = ".work"
func main() {
prepareWorkDir()
cloneBuckets()
// get a list of all json files
files, err := filepath.Glob(workDir + "/*/bucket/*.json")
catch(err, "", "")
// read files
filesArray := parseManifests(files)
filesString := strings.Join(filesArray, ",")
filesString = "[" + filesString + "]"
// write to file
log.Println("Writing manifests to file")
write("manifests.json", filesString)
log.Println("Done")
}
// A Bucket consists of its name and a git url
type Bucket struct {
name string
repo string
}
var gitHubURL = "https://github.com/"
var rawGitHubURL = "https://raw.githubusercontent.com/"
func getBuckets() []Bucket {
buckets := []Bucket{
{"main", "ScoopInstaller/Main"},
{"extras", "lukesampson/scoop-extras"},
{"versions", "ScoopInstaller/Versions"},
{"nightlies", "ScoopInstaller/Nightlies"},
{"nirsoft", "kodybrown/scoop-nirsoft"},
{"php", "ScoopInstaller/PHP"},
{"nonportable", "TheRandomLabs/scoop-nonportable"},
{"java", "ScoopInstaller/Java"},
{"games", "Calinou/scoop-games"},
}
return buckets
}
func cloneBuckets() {
buckets := getBuckets()
for i := 0; i < len(buckets); i++ {
clone(buckets[i])
}
}
func write(fileName string, content string) {
file, err := os.Create("docs/" + fileName)
catch(err, "", "")
defer file.Close()
_, err = io.WriteString(file, content)
catch(err, "", "")
}
func clone(bucket Bucket) {
log.Println("Cloning bucket repository " + bucket.name)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("git", "clone", gitHubURL+bucket.repo, workDir+"/"+bucket.name)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
catch(err, stdout.String(), stderr.String())
}
func parseManifests(files []string) []string {
log.Println("Parsing manifests")
var result []string
errorCount := 0
successCount := 0
for i := 0; i < len(files); i++ {
manifest, err := gabs.ParseJSONFile(files[i])
name, bucket, manifestURL, rawManifestURL := extractManifestDetails(files[i])
manifest.Set(name, "name")
manifest.Set(bucket, "bucket")
manifest.Set(manifestURL, "manifestURL")
manifest.Set(rawManifestURL, "rawManifestURL")
if err == nil {
result = append(result, manifest.String())
successCount = successCount + 1
} else {
log.Println("Skipping", name, "from", bucket, "--", err)
errorCount = errorCount + 1
}
}
log.Println("Successfully parsed", successCount, "manifest(s).")
log.Println("Skipped", errorCount, "erroneous manifest(s).")
return result
}
func extractManifestDetails(path string) (string, string, string, string) {
// extract from filename
separator := string(os.PathSeparator)
parts := strings.Split(path, separator)
bucket := parts[1] // workDir/bucket
nameWithJSON := parts[len(parts)-1]
jsonParts := strings.Split(nameWithJSON, ".json")
name := jsonParts[0]
// extract from repository url
buckets := getBuckets()
var repo string
for i := 0; i < len(buckets); i++ {
if buckets[i].name == bucket {
repo = buckets[i].repo
}
}
manifestURL := gitHubURL + repo + "/tree/master/bucket/" + name + ".json"
rawManifestURL := rawGitHubURL + repo + "/master/bucket/" + name + ".json"
return name, bucket, manifestURL, rawManifestURL
}
func prepareWorkDir() {
log.Println("Preparing work directory")
removeErr := os.RemoveAll(workDir)
if !os.IsNotExist(removeErr) {
catch(removeErr, "", "")
}
createErr := os.Mkdir(workDir, 0755)
if !os.IsExist(createErr) {
catch(createErr, "", "")
}
}
func catch(err error, stdout string, stderr string) {
if err != nil {
log.Println("ERROR")
log.Println(err)
if stdout != "" {
log.Println(stdout)
}
if stderr != "" {
log.Println(stderr)
}
log.Fatalln("Exiting shovel because of an error. Check the logging output above.")
}
}