You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

set.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package caps
  4. import (
  5. "sort"
  6. "strings"
  7. "sync"
  8. )
  9. // Set holds a set of enabled capabilities.
  10. type Set struct {
  11. sync.RWMutex
  12. // capabilities holds the capabilities this manager has.
  13. capabilities map[Capability]bool
  14. }
  15. // NewSet returns a new Set, with the given capabilities enabled.
  16. func NewSet(capabs ...Capability) *Set {
  17. newSet := Set{
  18. capabilities: make(map[Capability]bool),
  19. }
  20. newSet.Enable(capabs...)
  21. return &newSet
  22. }
  23. // Enable enables the given capabilities.
  24. func (s *Set) Enable(capabs ...Capability) {
  25. s.Lock()
  26. defer s.Unlock()
  27. for _, capab := range capabs {
  28. s.capabilities[capab] = true
  29. }
  30. }
  31. // Disable disables the given capabilities.
  32. func (s *Set) Disable(capabs ...Capability) {
  33. s.Lock()
  34. defer s.Unlock()
  35. for _, capab := range capabs {
  36. delete(s.capabilities, capab)
  37. }
  38. }
  39. // Add adds the given capabilities to this set.
  40. // this is just a wrapper to allow more clear use.
  41. func (s *Set) Add(capabs ...Capability) {
  42. s.Enable(capabs...)
  43. }
  44. // Remove removes the given capabilities from this set.
  45. // this is just a wrapper to allow more clear use.
  46. func (s *Set) Remove(capabs ...Capability) {
  47. s.Disable(capabs...)
  48. }
  49. // Has returns true if this set has the given capabilities.
  50. func (s *Set) Has(caps ...Capability) bool {
  51. s.RLock()
  52. defer s.RUnlock()
  53. for _, cap := range caps {
  54. if !s.capabilities[cap] {
  55. return false
  56. }
  57. }
  58. return true
  59. }
  60. // List return a list of our enabled capabilities.
  61. func (s *Set) List() []Capability {
  62. s.RLock()
  63. defer s.RUnlock()
  64. var allCaps []Capability
  65. for capab := range s.capabilities {
  66. allCaps = append(allCaps, capab)
  67. }
  68. return allCaps
  69. }
  70. // Count returns how many enabled caps this set has.
  71. func (s *Set) Count() int {
  72. s.RLock()
  73. defer s.RUnlock()
  74. return len(s.capabilities)
  75. }
  76. // String returns all of our enabled capabilities as a string.
  77. func (s *Set) String(version Version, values *Values) string {
  78. s.RLock()
  79. defer s.RUnlock()
  80. var strs sort.StringSlice
  81. for capability := range s.capabilities {
  82. capString := capability.Name()
  83. if version == Cap302 {
  84. val, exists := values.Get(capability)
  85. if exists {
  86. capString += "=" + val
  87. }
  88. }
  89. strs = append(strs, capString)
  90. }
  91. // sort the cap string before we send it out
  92. sort.Sort(strs)
  93. return strings.Join(strs, " ")
  94. }