-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (100 loc) · 3.5 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"io"
"github.com/Masterminds/semver"
)
const version = "1.0.0"
type CompatibleVersions struct {
MinVersion string `json:"minVersion"`
MaxVersion string `json:"maxVersion"`
}
func main() {
if len(os.Args) == 1 {
fmt.Fprintln(os.Stderr, "Command expected. Check https://code.onedev.io/onedev/tod for details")
os.Exit(1)
}
var command Command
switch os.Args[1] {
case "exec":
command = ExecCommand{}
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s. Check https://code.onedev.io/onedev/tod for details\n", os.Args[1])
os.Exit(1)
}
command.Execute(os.Args[2:])
}
func checkResponse(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err == nil {
return fmt.Errorf("non-successful status code received (status: %s, message: %s)", resp.Status, string(body))
} else {
return fmt.Errorf("non-successful status code received: %s", resp.Status)
}
} else {
return nil
}
}
func checkVersion(serverUrl string, accessToken string) error {
// Make a GET request to the API endpoint
client := &http.Client{}
// Create a new GET request
req, err := http.NewRequest("GET", serverUrl+"/~api/version/compatible-tod-versions", nil)
if err != nil {
return fmt.Errorf("error requesting compatible versions: %w", err)
}
req.Header.Set("Authorization", "Bearer "+accessToken)
// Send the request using the HTTP client
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error requesting compatible versions: %w", err)
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
return fmt.Errorf("error requesting compatible versions: %w", err)
}
// Decode the JSON response into a VersionInfo struct
var compatibleVersions CompatibleVersions
err = json.NewDecoder(resp.Body).Decode(&compatibleVersions)
if err != nil {
return fmt.Errorf("error decoding compatible versions response: %w", err)
}
semVer, err := semver.NewVersion(version)
if err != nil {
return fmt.Errorf("error parsing semver: %w", err)
}
var minVersionSatisfied = true
if compatibleVersions.MinVersion != "" {
minSemVer, err := semver.NewVersion(compatibleVersions.MinVersion)
if err != nil {
return fmt.Errorf("error parsing semver: %w", err)
}
if semVer.LessThan(minSemVer) {
minVersionSatisfied = false
}
}
var maxVersionSatisfied = true
if compatibleVersions.MaxVersion != "" {
maxSemVer, err := semver.NewVersion(compatibleVersions.MaxVersion)
if err != nil {
return fmt.Errorf("error parsing semver: %w", err)
}
if semVer.GreaterThan(maxSemVer) {
maxVersionSatisfied = false
}
}
if minVersionSatisfied && maxVersionSatisfied {
return nil
} else if compatibleVersions.MinVersion != "" && compatibleVersions.MaxVersion != "" {
return fmt.Errorf("this server requires version >= %s and version <= %s, please download from https://code.onedev.io/onedev/tod/~builds?query=%%22Job%%22+is+%%22Release%%22+and+successful", compatibleVersions.MinVersion, compatibleVersions.MaxVersion)
} else if compatibleVersions.MinVersion != "" {
return fmt.Errorf("this server requires version >= %s, please download from https://code.onedev.io/onedev/tod/~builds?query=%%22Job%%22+is+%%22Release%%22+and+successful", compatibleVersions.MinVersion)
} else {
return fmt.Errorf("this server requires version <= %s, please download from https://code.onedev.io/onedev/tod/~builds?query=%%22Job%%22+is+%%22Release%%22+and+successful", compatibleVersions.MaxVersion)
}
}