[Add] VersionCheck #215
|
@ -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 {
|
||||
|
||||
checkFunc := func () error {
|
||||
check, err := version.NewConstraint(constraint)
|
||||
if err != nil {
|
||||
c.versionLock.RLock()
|
||||
if c.serverVersion == nil {
|
||||
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
|
||||
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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user
I think you should have a lock since there maybe multiple goroutines check the verions.