github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/configuration/core_config/config_repository.go (about)

     1  package core_config
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  
     7  	"github.com/cloudfoundry/cli/cf/configuration"
     8  	"github.com/cloudfoundry/cli/cf/models"
     9  	"github.com/cloudfoundry/cli/utils"
    10  )
    11  
    12  type ConfigRepository struct {
    13  	data      *Data
    14  	mutex     *sync.RWMutex
    15  	initOnce  *sync.Once
    16  	persistor configuration.Persistor
    17  	onError   func(error)
    18  }
    19  
    20  func NewRepositoryFromFilepath(filepath string, errorHandler func(error)) Repository {
    21  	if errorHandler == nil {
    22  		return nil
    23  	}
    24  	return NewRepositoryFromPersistor(configuration.NewDiskPersistor(filepath), errorHandler)
    25  }
    26  
    27  func NewRepositoryFromPersistor(persistor configuration.Persistor, errorHandler func(error)) Repository {
    28  	data := NewData()
    29  	if !persistor.Exists() {
    30  		//set default plugin repo
    31  		data.PluginRepos = append(data.PluginRepos, models.PluginRepo{
    32  			Name: "CF-Community",
    33  			Url:  "http://plugins.cloudfoundry.org",
    34  		})
    35  	}
    36  
    37  	return &ConfigRepository{
    38  		data:      data,
    39  		mutex:     new(sync.RWMutex),
    40  		initOnce:  new(sync.Once),
    41  		persistor: persistor,
    42  		onError:   errorHandler,
    43  	}
    44  }
    45  
    46  type Reader interface {
    47  	ApiEndpoint() string
    48  	ApiVersion() string
    49  	HasAPIEndpoint() bool
    50  
    51  	AuthenticationEndpoint() string
    52  	LoggregatorEndpoint() string
    53  	DopplerEndpoint() string
    54  	UaaEndpoint() string
    55  	AccessToken() string
    56  	RefreshToken() string
    57  
    58  	OrganizationFields() models.OrganizationFields
    59  	HasOrganization() bool
    60  
    61  	SpaceFields() models.SpaceFields
    62  	HasSpace() bool
    63  
    64  	Username() string
    65  	UserGuid() string
    66  	UserEmail() string
    67  	IsLoggedIn() bool
    68  	IsSSLDisabled() bool
    69  	IsMinApiVersion(string) bool
    70  	IsMinCliVersion(string) bool
    71  	MinCliVersion() string
    72  	MinRecommendedCliVersion() string
    73  
    74  	AsyncTimeout() uint
    75  	Trace() string
    76  
    77  	ColorEnabled() string
    78  
    79  	Locale() string
    80  
    81  	PluginRepos() []models.PluginRepo
    82  }
    83  
    84  type ReadWriter interface {
    85  	Reader
    86  	ClearSession()
    87  	SetApiEndpoint(string)
    88  	SetApiVersion(string)
    89  	SetMinCliVersion(string)
    90  	SetMinRecommendedCliVersion(string)
    91  	SetAuthenticationEndpoint(string)
    92  	SetLoggregatorEndpoint(string)
    93  	SetDopplerEndpoint(string)
    94  	SetUaaEndpoint(string)
    95  	SetAccessToken(string)
    96  	SetRefreshToken(string)
    97  	SetOrganizationFields(models.OrganizationFields)
    98  	SetSpaceFields(models.SpaceFields)
    99  	SetSSLDisabled(bool)
   100  	SetAsyncTimeout(uint)
   101  	SetTrace(string)
   102  	SetColorEnabled(string)
   103  	SetLocale(string)
   104  	SetPluginRepo(models.PluginRepo)
   105  	UnSetPluginRepo(int)
   106  }
   107  
   108  type Repository interface {
   109  	ReadWriter
   110  	Close()
   111  }
   112  
   113  // ACCESS CONTROL
   114  
   115  func (c *ConfigRepository) init() {
   116  	c.initOnce.Do(func() {
   117  		err := c.persistor.Load(c.data)
   118  		if err != nil {
   119  			c.onError(err)
   120  		}
   121  	})
   122  }
   123  
   124  func (c *ConfigRepository) read(cb func()) {
   125  	c.mutex.RLock()
   126  	defer c.mutex.RUnlock()
   127  	c.init()
   128  
   129  	cb()
   130  }
   131  
   132  func (c *ConfigRepository) write(cb func()) {
   133  	c.mutex.Lock()
   134  	defer c.mutex.Unlock()
   135  	c.init()
   136  
   137  	cb()
   138  
   139  	err := c.persistor.Save(c.data)
   140  	if err != nil {
   141  		c.onError(err)
   142  	}
   143  }
   144  
   145  // CLOSERS
   146  
   147  func (c *ConfigRepository) Close() {
   148  	c.read(func() {
   149  		// perform a read to ensure write lock has been cleared
   150  	})
   151  }
   152  
   153  // GETTERS
   154  
   155  func (c *ConfigRepository) ApiVersion() (apiVersion string) {
   156  	c.read(func() {
   157  		apiVersion = c.data.ApiVersion
   158  	})
   159  	return
   160  }
   161  
   162  func (c *ConfigRepository) AuthenticationEndpoint() (authEndpoint string) {
   163  	c.read(func() {
   164  		authEndpoint = c.data.AuthorizationEndpoint
   165  	})
   166  	return
   167  }
   168  
   169  func (c *ConfigRepository) LoggregatorEndpoint() (logEndpoint string) {
   170  	c.read(func() {
   171  		logEndpoint = c.data.LoggregatorEndPoint
   172  	})
   173  	return
   174  }
   175  
   176  func (c *ConfigRepository) DopplerEndpoint() (logEndpoint string) {
   177  	//revert this in v7.0, once CC advertise doppler endpoint, and
   178  	//everyone has migrated from loggregator to doppler
   179  
   180  	// c.read(func() {
   181  	// 	logEndpoint = c.data.DopplerEndPoint
   182  	// })
   183  	c.read(func() {
   184  		logEndpoint = c.data.LoggregatorEndPoint
   185  	})
   186  
   187  	return strings.Replace(logEndpoint, "loggregator", "doppler", 1)
   188  }
   189  
   190  func (c *ConfigRepository) UaaEndpoint() (uaaEndpoint string) {
   191  	c.read(func() {
   192  		uaaEndpoint = c.data.UaaEndpoint
   193  	})
   194  	return
   195  }
   196  
   197  func (c *ConfigRepository) ApiEndpoint() (apiEndpoint string) {
   198  	c.read(func() {
   199  		apiEndpoint = c.data.Target
   200  	})
   201  	return
   202  }
   203  
   204  func (c *ConfigRepository) HasAPIEndpoint() (hasEndpoint bool) {
   205  	c.read(func() {
   206  		hasEndpoint = c.data.ApiVersion != "" && c.data.Target != ""
   207  	})
   208  	return
   209  }
   210  
   211  func (c *ConfigRepository) AccessToken() (accessToken string) {
   212  	c.read(func() {
   213  		accessToken = c.data.AccessToken
   214  	})
   215  	return
   216  }
   217  
   218  func (c *ConfigRepository) RefreshToken() (refreshToken string) {
   219  	c.read(func() {
   220  		refreshToken = c.data.RefreshToken
   221  	})
   222  	return
   223  }
   224  
   225  func (c *ConfigRepository) OrganizationFields() (org models.OrganizationFields) {
   226  	c.read(func() {
   227  		org = c.data.OrganizationFields
   228  	})
   229  	return
   230  }
   231  
   232  func (c *ConfigRepository) SpaceFields() (space models.SpaceFields) {
   233  	c.read(func() {
   234  		space = c.data.SpaceFields
   235  	})
   236  	return
   237  }
   238  
   239  func (c *ConfigRepository) UserEmail() (email string) {
   240  	c.read(func() {
   241  		email = NewTokenInfo(c.data.AccessToken).Email
   242  	})
   243  	return
   244  }
   245  
   246  func (c *ConfigRepository) UserGuid() (guid string) {
   247  	c.read(func() {
   248  		guid = NewTokenInfo(c.data.AccessToken).UserGuid
   249  	})
   250  	return
   251  }
   252  
   253  func (c *ConfigRepository) Username() (name string) {
   254  	c.read(func() {
   255  		name = NewTokenInfo(c.data.AccessToken).Username
   256  	})
   257  	return
   258  }
   259  
   260  func (c *ConfigRepository) IsLoggedIn() (loggedIn bool) {
   261  	c.read(func() {
   262  		loggedIn = c.data.AccessToken != ""
   263  	})
   264  	return
   265  }
   266  
   267  func (c *ConfigRepository) HasOrganization() (hasOrg bool) {
   268  	c.read(func() {
   269  		hasOrg = c.data.OrganizationFields.Guid != "" && c.data.OrganizationFields.Name != ""
   270  	})
   271  	return
   272  }
   273  
   274  func (c *ConfigRepository) HasSpace() (hasSpace bool) {
   275  	c.read(func() {
   276  		hasSpace = c.data.SpaceFields.Guid != "" && c.data.SpaceFields.Name != ""
   277  	})
   278  	return
   279  }
   280  
   281  func (c *ConfigRepository) IsSSLDisabled() (isSSLDisabled bool) {
   282  	c.read(func() {
   283  		isSSLDisabled = c.data.SSLDisabled
   284  	})
   285  	return
   286  }
   287  
   288  func (c *ConfigRepository) IsMinApiVersion(v string) bool {
   289  	var apiVersion string
   290  	c.read(func() {
   291  		apiVersion = c.data.ApiVersion
   292  	})
   293  
   294  	requiredVersion := utils.NewVersion(v)
   295  	cliVersion := utils.NewVersion(apiVersion)
   296  	return cliVersion.GreaterThanOrEqual(requiredVersion)
   297  }
   298  
   299  func (c *ConfigRepository) IsMinCliVersion(version string) bool {
   300  	if version == "BUILT_FROM_SOURCE" {
   301  		return true
   302  	}
   303  	var minCliVersion string
   304  	c.read(func() {
   305  		minCliVersion = c.data.MinCliVersion
   306  	})
   307  	if minCliVersion == "" {
   308  		return true
   309  	}
   310  
   311  	minCliVersion = strings.Split(minCliVersion, "-")[0]
   312  	requiredVersion := utils.NewVersion(version)
   313  	cliVersion := utils.NewVersion(minCliVersion)
   314  
   315  	return requiredVersion.GreaterThanOrEqual(cliVersion)
   316  }
   317  
   318  func (c *ConfigRepository) MinCliVersion() (minCliVersion string) {
   319  	c.read(func() {
   320  		minCliVersion = c.data.MinCliVersion
   321  	})
   322  	return
   323  }
   324  
   325  func (c *ConfigRepository) MinRecommendedCliVersion() (minRecommendedCliVersion string) {
   326  	c.read(func() {
   327  		minRecommendedCliVersion = c.data.MinRecommendedCliVersion
   328  	})
   329  	return
   330  }
   331  
   332  func (c *ConfigRepository) AsyncTimeout() (timeout uint) {
   333  	c.read(func() {
   334  		timeout = c.data.AsyncTimeout
   335  	})
   336  	return
   337  }
   338  
   339  func (c *ConfigRepository) Trace() (trace string) {
   340  	c.read(func() {
   341  		trace = c.data.Trace
   342  	})
   343  	return
   344  }
   345  
   346  func (c *ConfigRepository) ColorEnabled() (enabled string) {
   347  	c.read(func() {
   348  		enabled = c.data.ColorEnabled
   349  	})
   350  	return
   351  }
   352  
   353  func (c *ConfigRepository) Locale() (locale string) {
   354  	c.read(func() {
   355  		locale = c.data.Locale
   356  	})
   357  	return
   358  }
   359  
   360  func (c *ConfigRepository) PluginRepos() (repos []models.PluginRepo) {
   361  	c.read(func() {
   362  		repos = c.data.PluginRepos
   363  	})
   364  	return
   365  }
   366  
   367  // SETTERS
   368  
   369  func (c *ConfigRepository) ClearSession() {
   370  	c.write(func() {
   371  		c.data.AccessToken = ""
   372  		c.data.RefreshToken = ""
   373  		c.data.OrganizationFields = models.OrganizationFields{}
   374  		c.data.SpaceFields = models.SpaceFields{}
   375  	})
   376  }
   377  
   378  func (c *ConfigRepository) SetApiEndpoint(endpoint string) {
   379  	c.write(func() {
   380  		c.data.Target = endpoint
   381  	})
   382  }
   383  
   384  func (c *ConfigRepository) SetApiVersion(version string) {
   385  	c.write(func() {
   386  		c.data.ApiVersion = version
   387  	})
   388  }
   389  
   390  func (c *ConfigRepository) SetMinCliVersion(version string) {
   391  	c.write(func() {
   392  		c.data.MinCliVersion = version
   393  	})
   394  }
   395  
   396  func (c *ConfigRepository) SetMinRecommendedCliVersion(version string) {
   397  	c.write(func() {
   398  		c.data.MinRecommendedCliVersion = version
   399  	})
   400  }
   401  
   402  func (c *ConfigRepository) SetAuthenticationEndpoint(endpoint string) {
   403  	c.write(func() {
   404  		c.data.AuthorizationEndpoint = endpoint
   405  	})
   406  }
   407  
   408  func (c *ConfigRepository) SetLoggregatorEndpoint(endpoint string) {
   409  	c.write(func() {
   410  		c.data.LoggregatorEndPoint = endpoint
   411  	})
   412  }
   413  
   414  func (c *ConfigRepository) SetDopplerEndpoint(endpoint string) {
   415  	c.write(func() {
   416  		c.data.DopplerEndPoint = endpoint
   417  	})
   418  }
   419  
   420  func (c *ConfigRepository) SetUaaEndpoint(uaaEndpoint string) {
   421  	c.write(func() {
   422  		c.data.UaaEndpoint = uaaEndpoint
   423  	})
   424  }
   425  
   426  func (c *ConfigRepository) SetAccessToken(token string) {
   427  	c.write(func() {
   428  		c.data.AccessToken = token
   429  	})
   430  }
   431  
   432  func (c *ConfigRepository) SetRefreshToken(token string) {
   433  	c.write(func() {
   434  		c.data.RefreshToken = token
   435  	})
   436  }
   437  
   438  func (c *ConfigRepository) SetOrganizationFields(org models.OrganizationFields) {
   439  	c.write(func() {
   440  		c.data.OrganizationFields = org
   441  	})
   442  }
   443  
   444  func (c *ConfigRepository) SetSpaceFields(space models.SpaceFields) {
   445  	c.write(func() {
   446  		c.data.SpaceFields = space
   447  	})
   448  }
   449  
   450  func (c *ConfigRepository) SetSSLDisabled(disabled bool) {
   451  	c.write(func() {
   452  		c.data.SSLDisabled = disabled
   453  	})
   454  }
   455  
   456  func (c *ConfigRepository) SetAsyncTimeout(timeout uint) {
   457  	c.write(func() {
   458  		c.data.AsyncTimeout = timeout
   459  	})
   460  }
   461  
   462  func (c *ConfigRepository) SetTrace(value string) {
   463  	c.write(func() {
   464  		c.data.Trace = value
   465  	})
   466  }
   467  
   468  func (c *ConfigRepository) SetColorEnabled(enabled string) {
   469  	c.write(func() {
   470  		c.data.ColorEnabled = enabled
   471  	})
   472  }
   473  
   474  func (c *ConfigRepository) SetLocale(locale string) {
   475  	c.write(func() {
   476  		c.data.Locale = locale
   477  	})
   478  }
   479  
   480  func (c *ConfigRepository) SetPluginRepo(repo models.PluginRepo) {
   481  	c.write(func() {
   482  		c.data.PluginRepos = append(c.data.PluginRepos, repo)
   483  	})
   484  }
   485  
   486  func (c *ConfigRepository) UnSetPluginRepo(index int) {
   487  	c.write(func() {
   488  		c.data.PluginRepos = append(c.data.PluginRepos[:index], c.data.PluginRepos[index+1:]...)
   489  	})
   490  }