-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocol.go
More file actions
134 lines (112 loc) · 3.12 KB
/
Copy pathProtocol.go
File metadata and controls
134 lines (112 loc) · 3.12 KB
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
package protocol
import (
"Backend/Network"
"bufio"
"encoding/json"
"fmt"
"os"
)
/*
This will talk to C++ front end through stdin-stdout+JSON
*/
type Request struct {
Action string `json:"action"`
Repo string `json:"repo"`
Destination string `json:"destination"`
Platform string `json:"platform"`
}
type Response struct {
Success bool `json:"success"`
Version string `json:"version,omitempty"`
Error string `json:"error,omitempty"`
}
func ImportTest() {
fmt.Println("Protocol was Initialized")
}
func RequestTest() {
// the following line is just an example
release, err := network.GetLatest("https://github.com/hannes-swd/code-miner")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("RequestTest Version:", release.Version)
}
// FrontendLineReader starts the infinite loop listening to standard input for C++ commands
func FrontendLineReader() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
var request Request
rawBytes := scanner.Bytes()
// skip empty lines if c++ sends blank line
if len(rawBytes) == 0 {
continue
}
if err := json.Unmarshal(rawBytes, &request); err != nil {
writeResponse(Response{
Success: false,
Error: "Invalid JSON: " + err.Error(),
})
continue
}
switch request.Action {
case "check_release":
handleCheckRelease(request)
case "download_newest":
handleDownloadRelease(request)
default:
writeResponse(Response{
Success: false,
Error: "Unknown action: " + request.Action,
})
}
}
// if scanner stopped because of crash/error
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Backend stdin reading error: %v\n", err)
}
}
func handleCheckRelease(request Request) {
if request.Repo == "" {
writeResponse(Response{Success: false, Error: "repo field is required"})
return
}
release, err := network.GetLatest(request.Repo)
if err != nil {
writeResponse(Response{
Success: false,
Error: err.Error(),
})
return
}
writeResponse(Response{
Success: true,
Version: release.Version,
})
}
func handleDownloadRelease(request Request) {
if request.Repo == "" {
writeResponse(Response{Success: false, Error: "repo field is required"})
return
}
err := network.Download(request.Repo, request.Platform , request.Destination)
if err != nil {
writeResponse(Response{
Success: false,
Error: err.Error(),
})
return
}
writeResponse(Response{
Success: true,
})
}
func writeResponse(response Response) {
data, err := json.Marshal(response)
if err != nil {
// fallback error
fmt.Println(`{"success":false,"error":"Failed to encode response JSON"}`)
return
}
fmt.Println(string(data))
}