-
Notifications
You must be signed in to change notification settings - Fork 0
/
rclone.go
91 lines (76 loc) · 1.89 KB
/
rclone.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
package main
import (
"encoding/json"
"io"
"net/http"
"strings"
"time"
"github.com/rs/zerolog/log"
)
const (
BASE_URL = "http://localhost:5572"
RC_USER = "cmd"
RC_PASS = "f6Lhi09wfbxkd8Ok2l4H"
RC_TIMEOUT time.Duration = time.Second * 10
)
func sendToRclone(command string, payload string) (string, error) {
client := http.Client{Timeout: RC_TIMEOUT}
url := BASE_URL + command
async := false
// start job for sync (add _async:true to json)
if strings.HasPrefix(command, "/sync") {
async = true
json_data := map[string]interface{}{}
err := json.Unmarshal([]byte(payload), &json_data)
if err != nil {
log.Fatal().Err(err)
}
json_data["_async"] = true
msg, _ := json.Marshal(json_data)
payload = string(msg)
}
log.Debug().Msgf("Rclone url: %s", url)
log.Debug().Msgf("Rclone json: %s", payload)
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(payload))
if err != nil {
log.Fatal().Err(err)
return "", err
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(RC_USER, RC_PASS)
res, err := client.Do(req)
if err != nil {
log.Fatal().Err(err)
return "", err
}
defer res.Body.Close()
response, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal().Err(err)
return "", err
}
responseMessage := string(response)
log.Trace().Msgf("Rclone response: %s", responseMessage)
if async {
log.Debug().Msgf("Job: %s", responseMessage)
}
return responseMessage, nil
}
func rcloneOnline() bool {
online := false
for i := 1; i < 10; i++ {
time.Sleep(time.Second * 1)
response, err := sendToRclone("/core/version", "{}")
if err == nil {
online = true
data := map[string]interface{}{}
err := json.Unmarshal([]byte(response), &data)
if err != nil {
log.Fatal().Err(err)
}
log.Info().Msgf("Rclone version: %s", data["version"])
break
}
}
return online
}