/
main.go
225 lines (186 loc) · 4.64 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// Package - CCIP file structure
type Package struct {
XMLName xml.Name `xml:"package"`
Project struct {
ID string `xml:"id,attr"`
File string `xml:"file,attr"`
} `xml:"project"`
}
// AddonInfo - AddonInfo info json structure
type AddonInfo struct {
Name string `json:"name"`
Attachments []struct {
IsDefault bool `json:"isDefault"`
URL string `json:"url"`
} `json:"attachments"`
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Nothing provided")
os.Exit(128)
}
input := strings.Join(os.Args[1:], " ")
var addon string
var file string
url, err := url.ParseRequestURI(input)
if err == nil && url.Scheme == "curseforge" {
addon = url.Query().Get("addonId")
file = url.Query().Get("fileId")
} else {
pkg := LoadXML(input)
addon = pkg.Project.ID
file = pkg.Project.File
}
pack, err := GetPackURL(addon, file)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
addonInfo, err := GetAddonInfo(addon)
if err != nil {
fmt.Println(err)
fmt.Println("Error occured getting addon info, proceeding")
}
var iconPath string
var execArgs []string
switch runtime.GOOS {
case "darwin":
execArgs = []string{"open", "-a", "PolyMC", "--args", "--import", pack}
iconPath = "/Applications/PolyMC.app/Contents/MacOS/icons/" + addonInfo.Name
case "freebsd", "linux", "netbsd", "openbsd":
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
}
execArgs = []string{"polymc", "--import", pack}
iconPath = home + "/.local/share/polymc/icons/" + addonInfo.Name
err = exec.Command("flatpak", "info", "org.polymc.PolyMC").Run()
if err == nil {
execArgs = []string{"flatpak", "run", "org.polymc.PolyMC", "--import", pack}
iconPath = home + "/.var/app/org.polymc.PolyMC/data/polymc/icons/" + addonInfo.Name
}
case "windows":
executable, err := os.Executable()
if err != nil {
fmt.Println(err)
}
execPath := filepath.Dir(executable)
execArgs = []string{"PolyMC.exe", "--import", pack}
iconPath = execPath + "\\icons\\" + addonInfo.Name
}
var attachmentURL string
for _, attachment := range addonInfo.Attachments {
if attachment.IsDefault {
attachmentURL = attachment.URL
break
}
}
err = DownloadFile(iconPath, attachmentURL)
if err != nil {
fmt.Println(err)
fmt.Println("Error occured downloaading icon, proceeding")
}
RunCMD(execArgs)
}
// LoadXML - Load XML from disk into variable
func LoadXML(fileName string) Package {
xmlFile, err := os.Open(fileName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
var pkg Package
xml.Unmarshal(byteValue, &pkg)
return pkg
}
// GetPackURL - Request the pack url from Curseforge's API
func GetPackURL(addon string, file string) (string, error) {
resp, err := http.Get("https://addons-ecs.forgesvc.net/api/v2/addon/" + addon + "/file/" + file + "/download-url")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
url, err := url.Parse(string(body))
if err != nil {
return "", err
}
return url.String(), nil
}
// GetAddonInfo - Get addon info
func GetAddonInfo(addon string) (AddonInfo, error) {
resp, err := http.Get("https://addons-ecs.forgesvc.net/api/v2/addon/" + addon)
if err != nil {
return AddonInfo{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return AddonInfo{}, err
}
var addonInfo AddonInfo
err = json.Unmarshal(body, &addonInfo)
if err != nil {
return AddonInfo{}, err
}
return addonInfo, nil
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
// RunCMD - Execute CMD with args
func RunCMD(args []string) {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
cmd.Wait()
}
// FileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}