This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
211 lines (172 loc) · 4.04 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"fmt"
"os"
"net/http"
"github.com/fatih/color"
"io"
"log"
"archive/zip"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
"io/ioutil"
"github.com/urfave/cli"
)
var mcprAPIBase = "https://mcpr.io/api/v1"
func download(filepath, url string) (err error) {
fmt.Println("\nDownloading...")
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
color.Set(color.FgRed)
fmt.Println(err)
color.Unset()
return err
}
if resp.StatusCode == http.StatusNotFound {
color.Set(color.FgRed)
fmt.Println(resp.StatusCode)
fmt.Println("The file you requested could not be found...")
color.Unset()
os.Exit(1)
}
defer resp.Body.Close()
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func createDir(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, os.ModePerm)
}
}
func moveFile(in, out string) {
err := os.Rename(in, out)
if err != nil {
os.Exit(1)
return
}
}
func downloadPlugin(pluginID, version string) {
var filepath = "plugins/" + pluginID + "-" + version + ".jar"
var url = mcprAPIBase + "/versions/" + pluginID + "/" + version + "/download"
createDir("plugins")
err := download(filepath, url)
if err != nil {
log.Fatalf("Download error: %s", err)
}
}
func Unzip(src, dest string) ([]string, error) {
fmt.Println("Extracting jar file...")
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return filenames, err
}
defer rc.Close()
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)
} else {
// Make File
var fdir string
if lastIndex := strings.LastIndex(fpath, string(os.PathSeparator)); lastIndex > -1 {
fdir = fpath[:lastIndex]
}
err = os.MkdirAll(fdir, os.ModePerm)
if err != nil {
log.Fatal(err)
return filenames, err
}
f, err := os.OpenFile(
fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
defer f.Close()
_, err = io.Copy(f, rc)
if err != nil {
return filenames, err
}
}
}
return filenames, nil
}
type plugin struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
}
func (c *plugin) getInfo(filepath string) *plugin {
yamlFile, err := ioutil.ReadFile(filepath)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return c
}
func runTest (pluginName, pluginVersion string) {
downloadPlugin(pluginName, pluginVersion)
var pluginFile = pluginName + "-" + pluginVersion
files, err := Unzip("plugins/" + pluginFile + ".jar", "tmp/" + pluginFile)
if err != nil {
log.Fatal(err)
}
fmt.Println("Unzipped: " + strings.Join(files, "\n"))
var p plugin
p.getInfo("tmp/" + pluginFile + "/plugin.yml")
fmt.Println("\nPlugin Name: " + p.Name)
fmt.Println("Plugin Version: " + p.Version)
}
func main() {
app := cli.NewApp()
app.Version = "0.0.1"
app.Name = "test-bot-worker"
app.Usage = "The MCPR-Test-Bot Worker"
app.Description = "The MCPR-Test-Bot Worker"
app.Commands = []cli.Command{
{
Name: "test",
Aliases: []string{"t"},
Usage: "Run tests on plugin - test-bot-worker test [pluginID] [version]",
Action: func(c *cli.Context) error {
var pluginID string
var ver string
if c.Args().Get(0) != "" {
pluginID = c.Args().Get(0)
} else {
fmt.Println("Plugin ID must be specified.")
os.Exit(1)
}
if c.Args().Get(1) != "" {
ver = c.Args().Get(1)
} else {
fmt.Println("Plugin version must be specified.")
os.Exit(1)
}
runTest(pluginID, ver)
return nil
},
},
}
app.Run(os.Args)
}