-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
107 lines (91 loc) · 2.54 KB
/
util.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os/exec"
"runtime"
)
const APP_VERSION = "1.4.0"
const APP_UNIQUEID = "fake.nexus.20986"
const APP_UPDATEKEY = "Nexus:20986"
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func runSimpleCommand(cmd string) *exec.Cmd {
debugLog("Running " + cmd)
if runtime.GOOS == "windows" {
return exec.Command(
"powershell",
"-NoProfile",
"-Command",
cmd,
)
} else {
// TODO
return nil
}
}
func sendPOST(url string, data interface{}) ([]map[string]interface{}, error) {
// Marshal the JSON data
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("error marshaling JSON: %v", err)
}
// Create a request object
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
// Set headers
req.Header.Set("Content-Type", "application/json")
// Create a HTTP client
client := &http.Client{}
// Send the request
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
// Read the response body
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
debugLog(fmt.Sprintf("Response from %s with %s", url, string(responseBody)))
// Parse the JSON response array
var responseArray []map[string]interface{}
if err := json.Unmarshal(responseBody, &responseArray); err != nil {
return nil, fmt.Errorf("error parsing JSON response: %v", err)
}
return responseArray, nil
}
func checkForUpdate(a *App) (int, string) {
reqMods := []map[string]interface{}{{
"id": "fake.nexus.20986",
"updateKeys": []string{"Nexus:20986"},
"installedVersion": APP_VERSION,
}}
reqData := map[string]interface{}{
"mods": reqMods,
"apiVersion": "3.0.0",
}
var updates, err = sendPOST("https://smapi.io/api/v3.0/mods", reqData)
if err != nil {
return 2, "Failed to check for an update."
} else if len(updates) == 0 {
return 0, "No update available."
} else if updates[0]["suggestedUpdate"] != nil {
return 1, fmt.Sprintf("There is a new update - %s - available. ", updates[0]["suggestedUpdate"].(map[string]interface{})["version"].(string))
} else {
return 0, "No update available."
}
}