github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/configuration/core_config/config_repository.go (about) 1 package core_config 2 3 import ( 4 "sync" 5 6 "github.com/cloudfoundry/cli/cf/configuration" 7 "github.com/cloudfoundry/cli/cf/models" 8 ) 9 10 type ConfigRepository struct { 11 data *Data 12 mutex *sync.RWMutex 13 initOnce *sync.Once 14 persistor configuration.Persistor 15 onError func(error) 16 } 17 18 func NewRepositoryFromFilepath(filepath string, errorHandler func(error)) Repository { 19 return NewRepositoryFromPersistor(configuration.NewDiskPersistor(filepath), errorHandler) 20 } 21 22 func NewRepositoryFromPersistor(persistor configuration.Persistor, errorHandler func(error)) Repository { 23 return &ConfigRepository{ 24 data: NewData(), 25 mutex: new(sync.RWMutex), 26 initOnce: new(sync.Once), 27 persistor: persistor, 28 onError: errorHandler, 29 } 30 } 31 32 type Reader interface { 33 ApiEndpoint() string 34 ApiVersion() string 35 HasAPIEndpoint() bool 36 37 AuthenticationEndpoint() string 38 LoggregatorEndpoint() string 39 UaaEndpoint() string 40 AccessToken() string 41 RefreshToken() string 42 43 OrganizationFields() models.OrganizationFields 44 HasOrganization() bool 45 46 SpaceFields() models.SpaceFields 47 HasSpace() bool 48 49 Username() string 50 UserGuid() string 51 UserEmail() string 52 IsLoggedIn() bool 53 IsSSLDisabled() bool 54 55 AsyncTimeout() uint 56 Trace() string 57 58 ColorEnabled() string 59 60 Locale() string 61 } 62 63 type ReadWriter interface { 64 Reader 65 ClearSession() 66 SetApiEndpoint(string) 67 SetApiVersion(string) 68 SetAuthenticationEndpoint(string) 69 SetLoggregatorEndpoint(string) 70 SetUaaEndpoint(string) 71 SetAccessToken(string) 72 SetRefreshToken(string) 73 SetOrganizationFields(models.OrganizationFields) 74 SetSpaceFields(models.SpaceFields) 75 SetSSLDisabled(bool) 76 SetAsyncTimeout(uint) 77 SetTrace(string) 78 SetColorEnabled(string) 79 SetLocale(string) 80 } 81 82 type Repository interface { 83 ReadWriter 84 Close() 85 } 86 87 // ACCESS CONTROL 88 89 func (c *ConfigRepository) init() { 90 c.initOnce.Do(func() { 91 err := c.persistor.Load(c.data) 92 if err != nil { 93 c.onError(err) 94 } 95 }) 96 } 97 98 func (c *ConfigRepository) read(cb func()) { 99 c.mutex.RLock() 100 defer c.mutex.RUnlock() 101 c.init() 102 103 cb() 104 } 105 106 func (c *ConfigRepository) write(cb func()) { 107 c.mutex.Lock() 108 defer c.mutex.Unlock() 109 c.init() 110 111 cb() 112 113 err := c.persistor.Save(c.data) 114 if err != nil { 115 c.onError(err) 116 } 117 } 118 119 // CLOSERS 120 121 func (c *ConfigRepository) Close() { 122 c.read(func() { 123 // perform a read to ensure write lock has been cleared 124 }) 125 } 126 127 // GETTERS 128 129 func (c *ConfigRepository) ApiVersion() (apiVersion string) { 130 c.read(func() { 131 apiVersion = c.data.ApiVersion 132 }) 133 return 134 } 135 136 func (c *ConfigRepository) AuthenticationEndpoint() (authEndpoint string) { 137 c.read(func() { 138 authEndpoint = c.data.AuthorizationEndpoint 139 }) 140 return 141 } 142 143 func (c *ConfigRepository) LoggregatorEndpoint() (logEndpoint string) { 144 c.read(func() { 145 logEndpoint = c.data.LoggregatorEndPoint 146 }) 147 return 148 } 149 150 func (c *ConfigRepository) UaaEndpoint() (uaaEndpoint string) { 151 c.read(func() { 152 uaaEndpoint = c.data.UaaEndpoint 153 }) 154 return 155 } 156 157 func (c *ConfigRepository) ApiEndpoint() (apiEndpoint string) { 158 c.read(func() { 159 apiEndpoint = c.data.Target 160 }) 161 return 162 } 163 164 func (c *ConfigRepository) HasAPIEndpoint() (hasEndpoint bool) { 165 c.read(func() { 166 hasEndpoint = c.data.ApiVersion != "" && c.data.Target != "" 167 }) 168 return 169 } 170 171 func (c *ConfigRepository) AccessToken() (accessToken string) { 172 c.read(func() { 173 accessToken = c.data.AccessToken 174 }) 175 return 176 } 177 178 func (c *ConfigRepository) RefreshToken() (refreshToken string) { 179 c.read(func() { 180 refreshToken = c.data.RefreshToken 181 }) 182 return 183 } 184 185 func (c *ConfigRepository) OrganizationFields() (org models.OrganizationFields) { 186 c.read(func() { 187 org = c.data.OrganizationFields 188 }) 189 return 190 } 191 192 func (c *ConfigRepository) SpaceFields() (space models.SpaceFields) { 193 c.read(func() { 194 space = c.data.SpaceFields 195 }) 196 return 197 } 198 199 func (c *ConfigRepository) UserEmail() (email string) { 200 c.read(func() { 201 email = NewTokenInfo(c.data.AccessToken).Email 202 }) 203 return 204 } 205 206 func (c *ConfigRepository) UserGuid() (guid string) { 207 c.read(func() { 208 guid = NewTokenInfo(c.data.AccessToken).UserGuid 209 }) 210 return 211 } 212 213 func (c *ConfigRepository) Username() (name string) { 214 c.read(func() { 215 name = NewTokenInfo(c.data.AccessToken).Username 216 }) 217 return 218 } 219 220 func (c *ConfigRepository) IsLoggedIn() (loggedIn bool) { 221 c.read(func() { 222 loggedIn = c.data.AccessToken != "" 223 }) 224 return 225 } 226 227 func (c *ConfigRepository) HasOrganization() (hasOrg bool) { 228 c.read(func() { 229 hasOrg = c.data.OrganizationFields.Guid != "" && c.data.OrganizationFields.Name != "" 230 }) 231 return 232 } 233 234 func (c *ConfigRepository) HasSpace() (hasSpace bool) { 235 c.read(func() { 236 hasSpace = c.data.SpaceFields.Guid != "" && c.data.SpaceFields.Name != "" 237 }) 238 return 239 } 240 241 func (c *ConfigRepository) IsSSLDisabled() (isSSLDisabled bool) { 242 c.read(func() { 243 isSSLDisabled = c.data.SSLDisabled 244 }) 245 return 246 } 247 248 func (c *ConfigRepository) AsyncTimeout() (timeout uint) { 249 c.read(func() { 250 timeout = c.data.AsyncTimeout 251 }) 252 return 253 } 254 255 func (c *ConfigRepository) Trace() (trace string) { 256 c.read(func() { 257 trace = c.data.Trace 258 }) 259 return 260 } 261 262 func (c *ConfigRepository) ColorEnabled() (enabled string) { 263 c.read(func() { 264 enabled = c.data.ColorEnabled 265 }) 266 return 267 } 268 269 func (c *ConfigRepository) Locale() (locale string) { 270 c.read(func() { 271 locale = c.data.Locale 272 }) 273 return 274 } 275 276 // SETTERS 277 278 func (c *ConfigRepository) ClearSession() { 279 c.write(func() { 280 c.data.AccessToken = "" 281 c.data.RefreshToken = "" 282 c.data.OrganizationFields = models.OrganizationFields{} 283 c.data.SpaceFields = models.SpaceFields{} 284 }) 285 } 286 287 func (c *ConfigRepository) SetApiEndpoint(endpoint string) { 288 c.write(func() { 289 c.data.Target = endpoint 290 }) 291 } 292 293 func (c *ConfigRepository) SetApiVersion(version string) { 294 c.write(func() { 295 c.data.ApiVersion = version 296 }) 297 } 298 299 func (c *ConfigRepository) SetAuthenticationEndpoint(endpoint string) { 300 c.write(func() { 301 c.data.AuthorizationEndpoint = endpoint 302 }) 303 } 304 305 func (c *ConfigRepository) SetLoggregatorEndpoint(endpoint string) { 306 c.write(func() { 307 c.data.LoggregatorEndPoint = endpoint 308 }) 309 } 310 311 func (c *ConfigRepository) SetUaaEndpoint(uaaEndpoint string) { 312 c.write(func() { 313 c.data.UaaEndpoint = uaaEndpoint 314 }) 315 } 316 317 func (c *ConfigRepository) SetAccessToken(token string) { 318 c.write(func() { 319 c.data.AccessToken = token 320 }) 321 } 322 323 func (c *ConfigRepository) SetRefreshToken(token string) { 324 c.write(func() { 325 c.data.RefreshToken = token 326 }) 327 } 328 329 func (c *ConfigRepository) SetOrganizationFields(org models.OrganizationFields) { 330 c.write(func() { 331 c.data.OrganizationFields = org 332 }) 333 } 334 335 func (c *ConfigRepository) SetSpaceFields(space models.SpaceFields) { 336 c.write(func() { 337 c.data.SpaceFields = space 338 }) 339 } 340 341 func (c *ConfigRepository) SetSSLDisabled(disabled bool) { 342 c.write(func() { 343 c.data.SSLDisabled = disabled 344 }) 345 } 346 347 func (c *ConfigRepository) SetAsyncTimeout(timeout uint) { 348 c.write(func() { 349 c.data.AsyncTimeout = timeout 350 }) 351 } 352 353 func (c *ConfigRepository) SetTrace(value string) { 354 c.write(func() { 355 c.data.Trace = value 356 }) 357 } 358 359 func (c *ConfigRepository) SetColorEnabled(enabled string) { 360 c.write(func() { 361 c.data.ColorEnabled = enabled 362 }) 363 } 364 365 func (c *ConfigRepository) SetLocale(locale string) { 366 c.write(func() { 367 c.data.Locale = locale 368 }) 369 }