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

Updated version route serialization: use proper structure instead of string

parent cb8f3f8d
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ package cmdVersion
import (
"errors"
"encoding/json"
"nexus-common/params"
"nexus-client/version"
u "nexus-client/utils"
g "nexus-client/globals"
......@@ -42,31 +43,28 @@ func (cmd *Version)Run(args []string) int {
return 1
}
u.Println("client version: "+version.Get().String())
u.Println("server version: "+serverVersion)
clientVersion := version.Get()
u.Println("client version: "+clientVersion.String())
u.Println("server version: "+serverVersion.String())
return 0
}
func getServerVersion() (string, error) {
func getServerVersion() (*params.Version, error) {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().Get(host+"/version")
if err != nil {
return "", err
return nil, err
} else {
if resp.IsSuccess() {
type Response struct {
Version string
}
var response Response
err = json.Unmarshal(resp.Body(), &response)
if err != nil {
return "", err
version := params.Version{}
if err := json.Unmarshal(resp.Body(), &version); err != nil {
return nil, err
}
return response.Version, nil
return &version, nil
} else {
return "", errors.New(resp.Status()+": "+resp.String())
return nil, errors.New(resp.Status()+": "+resp.String())
}
}
}
......@@ -74,13 +72,12 @@ func getServerVersion() (string, error) {
// 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.
v, err := getServerVersion()
serverVersion, err := getServerVersion()
if err != nil {
u.PrintlnErr("Error: "+err.Error())
return false
}
clientVersion := version.Get()
serverVersion, _ := version.FromString(v)
if !clientVersion.IsCompatible(serverVersion) {
u.PrintlnErr(appname+" version "+clientVersion.String()+" is not compatible with server version "+serverVersion.String()+".")
if clientVersion.IsOlder(serverVersion) {
......
......@@ -69,7 +69,8 @@ var cmdList = []cmd.Command {
func run() int {
var appname = path.Base(os.Args[0])
u.PrintlnErr(appname+" version "+version.Get().String())
clientVersion := version.Get()
u.PrintlnErr(appname+" version "+clientVersion.String())
if len(os.Args) < 2 {
u.PrintlnErr("USAGE: "+appname+" CMD")
......
......@@ -80,7 +80,8 @@ var savedTermState *term.State = nil
func run() int {
var appname = path.Base(os.Args[0])
u.PrintlnErr(appname+" version "+version.Get().String())
clientVersion := version.Get()
u.PrintlnErr(appname+" version "+clientVersion.String())
if len(os.Args) < 2 {
u.PrintlnErr("USAGE: "+appname+" EMAIL")
......
package version
import (
"strings"
"strconv"
"nexus-common/params"
)
const (
......@@ -11,70 +10,8 @@ const (
bugfix = 0
)
type Version struct {
major int
minor int
bugfix int
}
var version Version = FromInt(major, minor, bugfix)
func FromInt(major, minor, bugfix int) Version {
return Version{major, minor, bugfix}
}
// Format: "x.y.z"
func FromString(s string) (Version, error) {
f := strings.Split(s, ".")
major, _ := strconv.Atoi(f[0])
minor, _ := strconv.Atoi(f[1])
bugfix, _ := strconv.Atoi(f[2])
return Version{major, minor, bugfix}, nil
}
var version params.Version = params.NewVersion(major, minor, bugfix)
func Get() Version {
func Get() params.Version {
return version
}
func (v Version) String() string {
return strconv.Itoa(v.major)+"."+strconv.Itoa(v.minor)+"."+strconv.Itoa(v.bugfix)
}
func (v Version) StringWithoutBugfix() string {
return strconv.Itoa(v.major)+"."+strconv.Itoa(v.minor)+".x"
}
// Returns true if v is older than candidate
func (v Version) IsOlder(candidate Version) bool {
v1 := v.getMajorMinor()
v2 := candidate.getMajorMinor()
if v1 > v2 {
return false
} else if v1 < v2 {
return true
} else {
bugfix1 := v.getBugfix()
bugfix2 := candidate.getBugfix()
return bugfix1 < bugfix2
}
}
// Returns true if v is compatible with candidate
func (v Version) IsCompatible(candidate Version) bool {
v1 := v.getMajorMinor()
v2 := candidate.getMajorMinor()
return v1 == v2
}
func (v Version) getMajorMinorAsString() string {
return strconv.Itoa(v.major)+strconv.Itoa(v.minor)
}
func (v Version) getMajorMinor() int {
n, _ := strconv.Atoi(v.getMajorMinorAsString())
return n
}
func (v Version) getBugfix() int {
return v.bugfix
}
......@@ -9,7 +9,3 @@ type Login struct {
type Token struct {
Token string `json:"token" validate:"required"`
}
type Version struct {
Version string `json:"version" validate:"required"`
}
\ No newline at end of file
package params
import (
"strconv"
)
type Version struct {
Major int `json:"major" validate:"required,gte=0"`
Minor int `json:"minor" validate:"required,gte=0"`
Bugfix int `json:"bugfix" validate:"required,gte=0"`
}
func NewVersion(major, minor, bugfix int) Version {
return Version{major, minor, bugfix}
}
// // Creates a version from a string fromatted as: "x.y.z"
// func NewVersionFromString(s string) (Version, error) {
// f := strings.Split(s, ".")
// major, _ := strconv.Atoi(f[0])
// minor, _ := strconv.Atoi(f[1])
// bugfix, _ := strconv.Atoi(f[2])
// return Version{major, minor, bugfix}, nil
// }
func (v *Version)String() string {
return strconv.Itoa(v.Major)+"."+strconv.Itoa(v.Minor)+"."+strconv.Itoa(v.Bugfix)
}
func (v *Version)StringWithoutBugfix() string {
return strconv.Itoa(v.Major)+"."+strconv.Itoa(v.Minor)+".x"
}
// Returns true if v is older than candidate
func (v *Version)IsOlder(candidate *Version) bool {
v1 := v.getMajorMinor()
v2 := candidate.getMajorMinor()
if v1 > v2 {
return false
} else if v1 < v2 {
return true
} else {
bugfix1 := v.getBugfix()
bugfix2 := candidate.getBugfix()
return bugfix1 < bugfix2
}
}
// Returns true if v is compatible with candidate
func (v *Version)IsCompatible(candidate *Version) bool {
v1 := v.getMajorMinor()
v2 := candidate.getMajorMinor()
return v1 == v2
}
func (v *Version)getMajorMinorAsString() string {
return strconv.Itoa(v.Major)+strconv.Itoa(v.Minor)
}
func (v *Version)getMajorMinor() int {
n, _ := strconv.Atoi(v.getMajorMinorAsString())
return n
}
func (v *Version)getBugfix() int {
return v.Bugfix
}
......@@ -21,7 +21,8 @@ func main() {
log.SetLevel(logrus.InfoLevel)
var appname = path.Base(os.Args[0])
log.Info(appname+" version "+version.Get().String())
serverVersion := version.Get()
log.Info(appname+" version "+serverVersion.String())
// Initialize the RNG' seed to the current time.
utils.RandInit()
......
......@@ -2,7 +2,6 @@ package router
import (
"net/http"
"nexus-common/params"
"nexus-server/version"
"github.com/labstack/echo/v4"
)
......@@ -14,8 +13,8 @@ func NewRouterVersion() *RouterVersion {
return &RouterVersion{}
}
// Retrives the version number.
// Gets the version number.
// curl --cacert ca.pem -X GET http://localhost:1077/version
func (r *RouterVersion)GetVersion(c echo.Context) error {
return c.JSON(http.StatusOK, params.Version{Version:version.Get().String()})
return c.JSONPretty(http.StatusOK, version.Get(), " ")
}
package version
import (
"strconv"
"nexus-common/params"
)
const (
......@@ -10,22 +10,8 @@ const (
bugfix = 0
)
type Version struct {
major int
minor int
bugfix int
}
var version Version = FromInt(major, minor, bugfix)
func FromInt(major, minor, bugfix int) Version {
return Version{major, minor, bugfix}
}
var version params.Version = params.NewVersion(major, minor, bugfix)
func Get() Version {
func Get() params.Version {
return version
}
func (v Version) String() string {
return strconv.Itoa(v.major)+"."+strconv.Itoa(v.minor)+"."+strconv.Itoa(v.bugfix)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment