[Add] VersionCheck #215

Merged
lafriks merged 12 commits from 6543/go-sdk:version-check into master 2020-01-27 06:20:55 +00:00
Showing only changes of commit 119ddc8a96 - Show all commits

View File

@ -21,23 +21,27 @@ 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.
checkFunc := func () error {
check, err := version.NewConstraint(constraint)
if err != nil {
c.versionLock.RLock()
if c.serverVersion == nil {

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
c.versionLock.RUnlock()
if err := initClientServerVersion(c); err != nil {
return err
}
if !check.Check(c.serverVersion) {
return fmt.Errorf("gitea server at %s does not satisfy version constraint %s", c.url, constraint)
}
return nil
}
c.versionLock.RLock()
if c.serverVersion != nil {
} else {
c.versionLock.RUnlock()
return checkFunc()
}
c.versionLock.RUnlock()
check, err := version.NewConstraint(constraint)
if err != nil {
return err
}
if !check.Check(c.serverVersion) {
return fmt.Errorf("gitea server at %s does not satisfy version constraint %s", c.url, constraint)
}
return nil
}
func initClientServerVersion(c *Client) error {
c.versionLock.Lock()
defer c.versionLock.Unlock()
@ -48,5 +52,4 @@ func (c *Client) CheckServerVersionConstraint(constraint string) error {
if c.serverVersion, err = version.NewVersion(raw); err != nil {
return err
}
return checkFunc()
}