github.com/whiteboxio/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/cfg/default_provider.go (about)

     1  package cfg
     2  
     3  import (
     4  	"github.com/awesome-flow/flow/pkg/types"
     5  )
     6  
     7  // DefaultProvider represents a set of default values.
     8  // Prefer keeping defaults over providing default values local to other
     9  // providers as it guarantees presence of the default values indiffirent to
    10  // the provider set that have been activated.
    11  type DefaultProvider struct {
    12  	weight   int
    13  	registry map[string]types.Value
    14  	ready    chan struct{}
    15  }
    16  
    17  var (
    18  	defaults map[string]types.Value
    19  )
    20  
    21  func init() {
    22  	defaults = map[string]types.Value{
    23  		CfgPathKey:     "/etc/flowd/flow-config.yaml",
    24  		PluginPathKey:  "/etc/flowd/plugins",
    25  		SystemMaxprocs: 1,
    26  	}
    27  }
    28  
    29  var _ Provider = (*DefaultProvider)(nil)
    30  
    31  // NewDefaultProvider is a constructor for DefaultProvider.
    32  func NewDefaultProvider(repo *Repository, weight int) (*DefaultProvider, error) {
    33  	return NewDefaultProviderWithRegistry(repo, weight, defaults)
    34  }
    35  
    36  // NewDefaultProviderWithRegistry is an alternative constructor for
    37  // DefaultProvider. Accepts an extra registry argument as a complete replacement
    38  // for the default one.
    39  func NewDefaultProviderWithRegistry(repo *Repository, weight int, registry map[string]types.Value) (*DefaultProvider, error) {
    40  	prov := &DefaultProvider{
    41  		weight:   weight,
    42  		registry: registry,
    43  		ready:    make(chan struct{}),
    44  	}
    45  	repo.RegisterProvider(prov)
    46  	return prov, nil
    47  }
    48  
    49  // Name returns provider name: default
    50  func (dp *DefaultProvider) Name() string { return "default" }
    51  
    52  // Depends returns the list of provider dependencies: none
    53  func (dp *DefaultProvider) Depends() []string { return []string{} }
    54  
    55  // Weight returns the provider weight
    56  func (dp *DefaultProvider) Weight() int { return dp.weight }
    57  
    58  // SetUp registers all keys from the registry in the repo
    59  func (dp *DefaultProvider) SetUp(repo *Repository) error {
    60  	defer close(dp.ready)
    61  	for k := range dp.registry {
    62  		if err := repo.RegisterKey(types.NewKey(k), dp); err != nil {
    63  			return err
    64  		}
    65  	}
    66  	return nil
    67  }
    68  
    69  // TearDown is a no-op operation for DefaultProvider
    70  func (dp *DefaultProvider) TearDown(*Repository) error { return nil }
    71  
    72  // Get is the primary method for fetching values from the default registry
    73  func (dp *DefaultProvider) Get(key types.Key) (*types.KeyValue, bool) {
    74  	<-dp.ready
    75  	if val, ok := dp.registry[key.String()]; ok {
    76  		return &types.KeyValue{Key: key, Value: val}, ok
    77  	}
    78  	return nil, false
    79  }