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 {
|
2018-06-26 14:18:05 +00:00
|
|
|
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 {
|
2018-06-26 14:18:05 +00:00
|
|
|
// 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
|
2018-06-26 14:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-06-26 14:18:05 +00:00
|
|
|
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 {
|
2018-06-26 14:18:05 +00:00
|
|
|
// (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
|
|
|
|
}
|
|
|
|
|
2018-06-26 14:18:05 +00:00
|
|
|
// 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 {
|
2018-06-26 14:18:05 +00:00
|
|
|
return err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-26 14:18:05 +00:00
|
|
|
return nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 14:18:05 +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
|
|
|
}
|