[Add] VersionCheck #215

Merged
lafriks merged 12 commits from 6543/go-sdk:version-check into master 2020-01-27 06:20:55 +00:00
3 changed files with 13 additions and 23 deletions
Showing only changes of commit 2b229116ea - Show all commits

View File

@ -14,7 +14,7 @@ import (
"net/http"
"strings"
version "github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
)
var jsonHeader = http.Header{"content-type": []string{"application/json"}}
@ -26,25 +26,22 @@ func Version() string {
// Client represents a Gitea API client.
type Client struct {
url string
accessToken string
username string
password string
sudo string
client *http.Client
url string
accessToken string
	serverVersion string
```go serverVersion string ```
username string
password string
sudo string
client *http.Client
serverVersion *version.Version
}
// NewClient initializes and returns a API client.
func NewClient(url, token string) *Client {
c := &Client{
return &Client{
url: strings.TrimSuffix(url, "/"),
accessToken: token,

Not needed here, load server version only when needed

Not needed here, load server version only when needed
client: &http.Client{},
}
if raw, err := c.ServerVersion(); err == nil {
serverVersion, _ = version.NewVersion(raw)
}
return c
}
// NewClientWithHTTP creates an API client with a custom http client

View File

@ -1,5 +0,0 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea

View File

@ -7,11 +7,9 @@ package gitea
import (
"fmt"
version "github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
)

This should be moved into Client struct as field serverVersion string

This should be moved into `Client` struct as field `serverVersion string`
var serverVersion *version.Version
// ServerVersion returns the version of the server
func (c *Client) ServerVersion() (string, error) {
var v = struct {
func (c *Client) ServerVersion() (string, error) {
	if len(c.serverVersion) != 0 {
		return c.serverVersion, nil
	}
	if err := c.ReloadServerVersion(); err != nil {
		return "", err
	}
	return c.serverVersion, nil
}

func (c *Client) ReloadServerVersion() error {
	// Reset cached server version string
	c.serverVersion = ""
	var v = struct {
		Version string `json:"version"`
	}{}
	if err := c.getParsedResponse("GET", "/version", nil, nil, &v) != nil {
		return err
	}
	c.serverVersion = v.Version
	return nil
}
```go func (c *Client) ServerVersion() (string, error) { if len(c.serverVersion) != 0 { return c.serverVersion, nil } if err := c.ReloadServerVersion(); err != nil { return "", err } return c.serverVersion, nil } func (c *Client) ReloadServerVersion() error { // Reset cached server version string c.serverVersion = "" var v = struct { Version string `json:"version"` }{} if err := c.getParsedResponse("GET", "/version", nil, nil, &v) != nil { return err } c.serverVersion = v.Version return nil } ```
Outdated
Review

Why not cache the version here?

Why not cache the version here?
@ -23,12 +21,12 @@ func (c *Client) ServerVersion() (string, error) {
// CheckServerVersionConstraint validates that the login's server satisfies a
// given version constraint such as ">= 1.11.0+dev"
func (c *Client) CheckServerVersionConstraint(constraint string) error {
Outdated
Review

I think you should have a lock since there maybe multiple goroutines check the verions.

I think you should have a lock since there maybe multiple goroutines check the verions.
if serverVersion == nil {
if c.serverVersion == nil {
raw, err := c.ServerVersion()

What if there is error?

What if there is error?
Outdated
Review

raw = "" -> CheckServerVersionConstraint will always end with return fmt.Errorf("gitea se...

raw = "" -> CheckServerVersionConstraint will always end with `return fmt.Errorf("gitea se...`

You can use sync.RWMutex instead to be able to control behavior with errors better

You can use sync.RWMutex instead to be able to control behavior with errors better
if err != nil {
return err
}
if serverVersion, err = version.NewVersion(raw); err != nil {
if c.serverVersion, err = version.NewVersion(raw); err != nil {
return err
}
}
@ -36,7 +34,7 @@ func (c *Client) CheckServerVersionConstraint(constraint string) error {
if err != nil {
return err
}
if !check.Check(serverVersion) {
if !check.Check(c.serverVersion) {
return fmt.Errorf("gitea server at %s does not satisfy version constraint %s", c.url, constraint)
}
return nil