-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
116 lines (87 loc) · 2.49 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
package main
import (
"fmt"
"github.com/buger/jsonparser"
"github.com/cavaliercoder/grab"
"github.com/fatih/color"
"github.com/joho/godotenv"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
const second = time.Second
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
color.Red("Error loading .env file")
}
serverID := os.Getenv("SERVERID")
apiKEY := os.Getenv("APIKEY")
backupNUM := os.Getenv("BACKUPNUM")
panelURL := os.Getenv("PANELURL")
req, err := http.NewRequest("GET", "https://" + panelURL + "/api/client/servers/" +serverID + "/backups", nil)
if err != nil {
// handle err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer " + apiKEY)
resp, err := http.DefaultClient.Do(req)
if err != nil {
// handle err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
uuid, _ := jsonparser.GetString(body, "data", "[" + backupNUM + "]", "attributes", "uuid")
//fmt.Println(uuid)
req1, err1 := http.NewRequest("GET", "https://" + panelURL + "/api/client/servers/" + serverID + "/backups/" + uuid + "/download", nil)
if err1 != nil {
// handle err
}
req1.Header.Set("Accept", "application/json")
req1.Header.Set("Content-Type", "application/json")
req1.Header.Set("Authorization", "Bearer " + apiKEY)
resp1, err1 := http.DefaultClient.Do(req1)
if err1 != nil {
// handle err
}
defer resp1.Body.Close()
body1, _ := ioutil.ReadAll(resp1.Body)
dlLink, _ := jsonparser.GetString(body1, "attributes", "url")
////////////////////////////////////////////
// create client
client := grab.NewClient()
req2, _ := grab.NewRequest(".", dlLink)
// color defining
blueText := color.New(color.FgCyan, color.Bold)
greenText := color.New(color.FgHiGreen, color.Bold)
// start download
blueText.Printf("Downloading %v...\n", req2.URL())
resp2 := client.Do(req2)
greenText.Printf(" %v\n", resp2.HTTPResponse.Status)
// start UI loop
t := time.NewTicker(5000 * time.Millisecond)
defer t.Stop()
Loop:
for {
select {
case <-t.C:
greenText.Printf(" transferred %v / %v megabytes (%.2f%%)\n",
resp2.BytesComplete()/1048576,
resp2.Size()/1048576,
100*resp2.Progress())
case <-resp2.Done:
// download is complete
break Loop
}
}
// check for errors
if err := resp2.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Download failed: %v\n", err)
os.Exit(1)
}
fmt.Printf("Download saved to ./%v \n", resp2.Filename)
}