-
Notifications
You must be signed in to change notification settings - Fork 336
/
version.go
51 lines (42 loc) · 1.34 KB
/
version.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
package loft
import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/loft-sh/devpod/pkg/http"
"github.com/loft-sh/devpod/pkg/provider"
)
type VersionObject struct {
// Version is the remote devpod version
DevPodVersion string `json:"devPodVersion,omitempty"`
}
func GetProInstanceDevPodVersion(proInstance *provider.ProInstance) (string, error) {
url := "https://" + proInstance.Host
return GetDevPodVersion(url)
}
func GetDevPodVersion(url string) (string, error) {
resp, err := http.GetHTTPClient().Get(url + "/version")
if err != nil {
return "", fmt.Errorf("get %s: %w", url, err)
} else if resp.StatusCode != 200 {
out, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("get %s: %s (Status: %d)", url, string(out), resp.StatusCode)
}
versionRaw, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("read %s: %w", url, err)
}
version := &VersionObject{}
err = json.Unmarshal(versionRaw, version)
if err != nil {
return "", fmt.Errorf("parse %s: %w", url, err)
} else if version.DevPodVersion == "" {
return "", fmt.Errorf("unexpected version '%s', please use --version to define a provider version", version.DevPodVersion)
}
// make sure it starts with a v
if !strings.HasPrefix(version.DevPodVersion, "v") {
version.DevPodVersion = "v" + version.DevPodVersion
}
return version.DevPodVersion, nil
}