traefik/vendor/github.com/containous/staert/staert.go

81 lines
2.2 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package staert
import (
"fmt"
"reflect"
2017-11-17 16:22:03 +00:00
"github.com/containous/flaeg"
2017-02-07 21:33:23 +00:00
)
// Source interface must be satisfy to Add any kink of Source to Staert as like as TomlFile or Flaeg
type Source interface {
Parse(cmd *flaeg.Command) (*flaeg.Command, error)
}
// Staert contains the struct to configure, thee default values inside structs and the sources
type Staert struct {
command *flaeg.Command
sources []Source
}
2017-11-17 16:22:03 +00:00
// NewStaert creates and return a pointer on Staert. Need defaultConfig and defaultPointersConfig given by references
2017-02-07 21:33:23 +00:00
func NewStaert(rootCommand *flaeg.Command) *Staert {
return &Staert{command: rootCommand}
2017-02-07 21:33:23 +00:00
}
// AddSource adds new Source to Staert, give it by reference
func (s *Staert) AddSource(src Source) {
s.sources = append(s.sources, src)
}
// LoadConfig check which command is called and parses config
// It returns the the parsed config or an error if it fails
func (s *Staert) LoadConfig() (interface{}, error) {
for _, src := range s.sources {
// Type assertion
if flg, ok := src.(*flaeg.Flaeg); ok {
fCmd, err := flg.GetCommand()
if err != nil {
2017-02-07 21:33:23 +00:00
return nil, err
}
// if fleag sub-command
if s.command != fCmd {
// if parseAllSources
2017-02-07 21:33:23 +00:00
if fCmd.Metadata["parseAllSources"] == "true" {
fCmdConfigType := reflect.TypeOf(fCmd.Config)
sCmdConfigType := reflect.TypeOf(s.command.Config)
if fCmdConfigType != sCmdConfigType {
return nil, fmt.Errorf("command %s : Config type doesn't match with root command config type. Expected %s got %s",
fCmd.Name, sCmdConfigType.Name(), fCmdConfigType.Name())
2017-02-07 21:33:23 +00:00
}
s.command = fCmd
} else {
// (not parseAllSources)
s.command, err = flg.Parse(fCmd)
2017-02-07 21:33:23 +00:00
return s.command.Config, err
}
}
}
}
err := s.parseConfigAllSources(s.command)
return s.command.Config, err
}
// parseConfigAllSources getConfig for a flaeg.Command run sources Parse func in the raw
func (s *Staert) parseConfigAllSources(cmd *flaeg.Command) error {
for _, src := range s.sources {
_, err := src.Parse(cmd)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
2017-02-07 21:33:23 +00:00
}
}
return nil
2017-02-07 21:33:23 +00:00
}
// Run calls the Run func of the command
// Warning, Run doesn't parse the config
func (s *Staert) Run() error {
return s.command.Run()
2017-02-07 21:33:23 +00:00
}