constraint.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package version
  2. import (
  3. "strings"
  4. )
  5. type Constraint struct {
  6. operator string
  7. version string
  8. }
  9. // Return a new Constrain and sets operator and version to compare
  10. func NewConstrain(operator, version string) *Constraint {
  11. constraint := new(Constraint)
  12. constraint.SetOperator(operator)
  13. constraint.SetVersion(version)
  14. return constraint
  15. }
  16. // Sets operator to compare
  17. func (self *Constraint) SetOperator(operator string) {
  18. self.operator = operator
  19. }
  20. // Get operator to compare
  21. func (self *Constraint) GetOperator() string {
  22. return self.operator
  23. }
  24. // Sets version to compare
  25. func (self *Constraint) SetVersion(version string) {
  26. self.version = version
  27. }
  28. // Get version to compare
  29. func (self *Constraint) GetVersion() string {
  30. return self.version
  31. }
  32. // Match a given version againts the constraint
  33. func (self *Constraint) Match(version string) bool {
  34. return Compare(version, self.version, self.operator)
  35. }
  36. // Return a string representation
  37. func (self *Constraint) String() string {
  38. return strings.Trim(self.operator+" "+self.version, " ")
  39. }