[Add] VersionCheck #215
|
@ -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
|
||||
lafriks
commented
Outdated
Review
```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,
|
||||
lafriks
commented
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
|
||||
|
|
|
@ -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
|
|
@ -7,11 +7,9 @@ package gitea
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
version "github.com/hashicorp/go-version"
|
||||
"github.com/hashicorp/go-version"
|
||||
)
|
||||
|
||||
lafriks
commented
This should be moved into 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 {
|
||||
lafriks
commented
```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
}
```
lunny
commented
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 {
|
||||
lunny
commented
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()
|
||||
lafriks
commented
What if there is error? What if there is error?
6543
commented
raw = "" -> CheckServerVersionConstraint will always end with raw = "" -> CheckServerVersionConstraint will always end with `return fmt.Errorf("gitea se...`
lafriks
commented
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user