Use of Viper and cobra

This commit is contained in:
emile 2016-01-13 22:46:44 +01:00
parent a0b15a0efd
commit 35070f7c1c
No known key found for this signature in database
GPG key ID: D808B4C167352E59
32 changed files with 414 additions and 143 deletions

View file

@ -8,13 +8,13 @@ import (
// BoltDb holds configurations of the BoltDb provider.
type BoltDb struct {
Kv
Kv `mapstructure:",squash"`
}
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *BoltDb) Provide(configurationChan chan<- types.ConfigMessage) error {
provider.StoreType = store.BOLTDB
provider.storeType = store.BOLTDB
boltdb.Register()
return provider.provide(configurationChan)
}

View file

@ -8,13 +8,13 @@ import (
// Consul holds configurations of the Consul provider.
type Consul struct {
Kv
Kv `mapstructure:",squash"`
}
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *Consul) Provide(configurationChan chan<- types.ConfigMessage) error {
provider.StoreType = store.CONSUL
provider.storeType = store.CONSUL
consul.Register()
return provider.provide(configurationChan)
}

View file

@ -17,10 +17,10 @@ import (
// Docker holds configurations of the Docker provider.
type Docker struct {
baseProvider
Endpoint string
Domain string
TLS *DockerTLS
BaseProvider `mapstructure:",squash"`
Endpoint string
Domain string
TLS *DockerTLS
}
// DockerTLS holds TLS specific configurations

View file

@ -8,13 +8,13 @@ import (
// Etcd holds configurations of the Etcd provider.
type Etcd struct {
Kv
Kv `mapstructure:",squash"`
}
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *Etcd) Provide(configurationChan chan<- types.ConfigMessage) error {
provider.StoreType = store.ETCD
provider.storeType = store.ETCD
etcd.Register()
return provider.provide(configurationChan)
}

View file

@ -13,7 +13,7 @@ import (
// File holds configurations of the File provider.
type File struct {
baseProvider
BaseProvider `mapstructure:",squash"`
}
// Provide allows the provider to provide configurations to traefik

View file

@ -15,16 +15,16 @@ import (
// Kv holds common configurations of key-value providers.
type Kv struct {
baseProvider
Endpoint string
Prefix string
StoreType store.Backend
kvclient store.Store
BaseProvider `mapstructure:",squash"`
Endpoint string
Prefix string
storeType store.Backend
kvclient store.Store
}
func (provider *Kv) provide(configurationChan chan<- types.ConfigMessage) error {
kv, err := libkv.NewStore(
provider.StoreType,
provider.storeType,
[]string{provider.Endpoint},
&store.Config{
ConnectionTimeout: 30 * time.Second,
@ -50,7 +50,7 @@ func (provider *Kv) provide(configurationChan chan<- types.ConfigMessage) error
configuration := provider.loadConfig()
if configuration != nil {
configurationChan <- types.ConfigMessage{
ProviderName: string(provider.StoreType),
ProviderName: string(provider.storeType),
Configuration: configuration,
}
}
@ -60,7 +60,7 @@ func (provider *Kv) provide(configurationChan chan<- types.ConfigMessage) error
}
configuration := provider.loadConfig()
configurationChan <- types.ConfigMessage{
ProviderName: string(provider.StoreType),
ProviderName: string(provider.storeType),
Configuration: configuration,
}
return nil

View file

@ -14,7 +14,7 @@ import (
// Marathon holds configuration of the Marathon provider.
type Marathon struct {
baseProvider
BaseProvider `mapstructure:",squash"`
Endpoint string
Domain string
NetworkInterface string

View file

@ -18,12 +18,13 @@ type Provider interface {
Provide(configurationChan chan<- types.ConfigMessage) error
}
type baseProvider struct {
// BaseProvider should be inherited by providers
type BaseProvider struct {
Watch bool
Filename string
}
func (p *baseProvider) getConfiguration(defaultTemplateFile string, funcMap template.FuncMap, templateObjects interface{}) (*types.Configuration, error) {
func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap template.FuncMap, templateObjects interface{}) (*types.Configuration, error) {
var (
buf []byte
err error

View file

@ -9,7 +9,7 @@ import (
)
type myProvider struct {
baseProvider
BaseProvider
}
func (p *myProvider) Foo() string {
@ -49,7 +49,7 @@ func TestConfigurationErrors(t *testing.T) {
}{
{
provider: &myProvider{
baseProvider{
BaseProvider{
Filename: "/non/existent/template.tmpl",
},
},
@ -62,7 +62,7 @@ func TestConfigurationErrors(t *testing.T) {
},
{
provider: &myProvider{
baseProvider{
BaseProvider{
Filename: templateErrorFile.Name(),
},
},
@ -70,7 +70,7 @@ func TestConfigurationErrors(t *testing.T) {
},
{
provider: &myProvider{
baseProvider{
BaseProvider{
Filename: templateInvalidTOMLFile.Name(),
},
},
@ -125,7 +125,7 @@ func TestGetConfiguration(t *testing.T) {
}
provider := &myProvider{
baseProvider{
BaseProvider{
Filename: templateFile.Name(),
},
}

View file

@ -14,7 +14,7 @@ type Zookepper struct {
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *Zookepper) Provide(configurationChan chan<- types.ConfigMessage) error {
provider.StoreType = store.ZK
provider.storeType = store.ZK
zookeeper.Register()
return provider.provide(configurationChan)
}