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 package cmdVersion
import ( import (
"encoding/json"
"errors"
u "nexus-client/utils" u "nexus-client/utils"
"nexus-client/version" "nexus-client/version"
"nexus-common/params" libclient "nexus-libclient/version"
g "nexus-libclient/globals"
) )
type Version struct { type Version struct {
...@@ -37,7 +34,7 @@ func (cmd *Version) Run(args []string) int { ...@@ -37,7 +34,7 @@ func (cmd *Version) Run(args []string) int {
return 1 return 1
} }
serverVersion, err := getServerVersion() serverVersion, err := libclient.GetServerVersion()
if err != nil { if err != nil {
u.PrintlnErr("Error: " + err.Error()) u.PrintlnErr("Error: " + err.Error())
return 1 return 1
...@@ -49,30 +46,10 @@ func (cmd *Version) Run(args []string) int { ...@@ -49,30 +46,10 @@ func (cmd *Version) Run(args []string) int {
return 0 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. // Checks the client version is compatible with the server's API.
func CheckServerCompatibility(appname string) bool { func CheckServerCompatibility(appname string) bool {
// Checks the client version is compatible with the server's API. // Checks the client version is compatible with the server's API.
serverVersion, err := getServerVersion() serverVersion, err := libclient.GetServerVersion()
if err != nil { if err != nil {
u.PrintlnErr("Error: " + err.Error()) u.PrintlnErr("Error: " + err.Error())
return false return false
......
...@@ -3,7 +3,6 @@ package main ...@@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
_ "embed" _ "embed"
"encoding/json"
"errors" "errors"
"nexus-client/cmdLogin" "nexus-client/cmdLogin"
"nexus-client/cmdToken" "nexus-client/cmdToken"
...@@ -15,6 +14,7 @@ import ( ...@@ -15,6 +14,7 @@ import (
"nexus-common/params" "nexus-common/params"
"nexus-common/vm" "nexus-common/vm"
g "nexus-libclient/globals" g "nexus-libclient/globals"
libclient "nexus-libclient/vm"
"os" "os"
"os/exec" "os/exec"
"path" "path"
...@@ -26,7 +26,6 @@ import ( ...@@ -26,7 +26,6 @@ import (
"fyne.io/fyne/v2/container" "fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/widget"
"github.com/go-playground/validator/v10"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
) )
...@@ -60,46 +59,20 @@ func exit(code int) { ...@@ -60,46 +59,20 @@ func exit(code int) {
} }
func attachVM(parent fyne.Window, hostname, cert, pwd string) { func attachVM(parent fyne.Window, hostname, cert, pwd string) {
client := g.GetInstance().Client
host := g.GetInstance().Host
setHeaderToken() setHeaderToken()
p := &params.VMAttachCreds{Pwd: pwd} p := params.VMAttachCreds{Pwd: pwd}
resp, err := client.R().SetBody(p).Post(host + "/vms/spicecreds") creds, err := libclient.VMGetAnySpiceCreds(p)
if err != nil { if err != nil {
errorPopup(parent, "Failed attaching to VM (code 4)") errorPopup(parent, "Failed attaching to VM: "+err.Error())
return return
} } else {
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
}
go func(creds vm.VMSpiceCredentialsSerialized) { go func(creds vm.VMSpiceCredentialsSerialized) {
_, err := e.RunRemoteViewer(hostname, cert, creds.Name, creds.SpicePort, creds.SpicePwd, true) _, err := e.RunRemoteViewer(hostname, cert, creds.Name, creds.SpicePort, creds.SpicePwd, true)
if err != nil { if err != nil {
errorPopup(parent, "Failed attaching to VM (code 7)") errorPopup(parent, "Failed executing remote-viewer: "+err.Error())
return
} }
}(creds) }(*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
} }
} }
......
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