Skip to content
Snippets Groups Projects
Commit bce3a39d authored by Florent Gluck's avatar Florent Gluck
Browse files

libclient: updated nexus-exam to use it; added version support

parent 89f30045
No related branches found
No related tags found
No related merge requests found
package cmdVersion
import (
"encoding/json"
"errors"
u "nexus-client/utils"
"nexus-client/version"
"nexus-common/params"
g "nexus-libclient/globals"
libclient "nexus-libclient/version"
)
type Version struct {
......@@ -37,7 +34,7 @@ func (cmd *Version) Run(args []string) int {
return 1
}
serverVersion, err := getServerVersion()
serverVersion, err := libclient.GetServerVersion()
if err != nil {
u.PrintlnErr("Error: " + err.Error())
return 1
......@@ -49,30 +46,10 @@ func (cmd *Version) Run(args []string) int {
return 0
}
func getServerVersion() (*params.Version, error) {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().Get(host + "/version")
if err != nil {
return nil, err
} else {
if resp.IsSuccess() {
version := params.Version{}
if err := json.Unmarshal(resp.Body(), &version); err != nil {
return nil, err
}
return &version, nil
} else {
return nil, errors.New(resp.Status() + ": " + resp.String())
}
}
}
// Checks the client version is compatible with the server's API.
func CheckServerCompatibility(appname string) bool {
// Checks the client version is compatible with the server's API.
serverVersion, err := getServerVersion()
serverVersion, err := libclient.GetServerVersion()
if err != nil {
u.PrintlnErr("Error: " + err.Error())
return false
......
......@@ -3,7 +3,6 @@ package main
import (
"bytes"
_ "embed"
"encoding/json"
"errors"
"nexus-client/cmdLogin"
"nexus-client/cmdToken"
......@@ -15,6 +14,7 @@ import (
"nexus-common/params"
"nexus-common/vm"
g "nexus-libclient/globals"
libclient "nexus-libclient/vm"
"os"
"os/exec"
"path"
......@@ -26,7 +26,6 @@ import (
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/go-playground/validator/v10"
"github.com/go-resty/resty/v2"
)
......@@ -60,46 +59,20 @@ func exit(code int) {
}
func attachVM(parent fyne.Window, hostname, cert, pwd string) {
client := g.GetInstance().Client
host := g.GetInstance().Host
setHeaderToken()
p := &params.VMAttachCreds{Pwd: pwd}
resp, err := client.R().SetBody(p).Post(host + "/vms/spicecreds")
p := params.VMAttachCreds{Pwd: pwd}
creds, err := libclient.VMGetAnySpiceCreds(p)
if err != nil {
errorPopup(parent, "Failed attaching to VM (code 4)")
errorPopup(parent, "Failed attaching to VM: "+err.Error())
return
}
if resp.IsSuccess() {
var creds vm.VMSpiceCredentialsSerialized
if err := json.Unmarshal(resp.Body(), &creds); err != nil {
errorPopup(parent, "Failed attaching to VM (code 5)")
return
}
if err := validator.New(validator.WithRequiredStructEnabled()).Struct(creds); err != nil {
errorPopup(parent, "Failed attaching to VM (code 6)")
return
}
} else {
go func(creds vm.VMSpiceCredentialsSerialized) {
_, err := e.RunRemoteViewer(hostname, cert, creds.Name, creds.SpicePort, creds.SpicePwd, true)
if err != nil {
errorPopup(parent, "Failed attaching to VM (code 7)")
return
errorPopup(parent, "Failed executing remote-viewer: "+err.Error())
}
}(creds)
} else {
type msg struct {
Message string `json:"message"`
}
var m msg
if err := json.Unmarshal(resp.Body(), &m); err != nil {
errorPopup(parent, "Failed attaching to VM (code 8)")
return
}
errorPopup(parent, m.Message)
// errorPopup(parent, resp.Status()+": "+resp.String())
return
}(*creds)
}
}
......
package version
import (
"encoding/json"
"errors"
"nexus-common/params"
g "nexus-libclient/globals"
)
func GetServerVersion() (*params.Version, error) {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().Get(host + "/version")
if err != nil {
return nil, err
} else {
if resp.IsSuccess() {
version := params.Version{}
if err := json.Unmarshal(resp.Body(), &version); err != nil {
return nil, err
}
return &version, nil
} else {
return nil, errors.New(resp.Status() + ": " + resp.String())
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment