Better support on same prefix at the same level in the KV
This commit is contained in:
parent
ec6e46e2cb
commit
5eda08e9b8
5 changed files with 191 additions and 153 deletions
164
vendor/github.com/containous/staert/staert.go
generated
vendored
164
vendor/github.com/containous/staert/staert.go
generated
vendored
|
@ -2,12 +2,8 @@ package staert
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/containous/flaeg"
|
||||
)
|
||||
|
||||
|
@ -24,10 +20,7 @@ type Staert struct {
|
|||
|
||||
// NewStaert creates and return a pointer on Staert. Need defaultConfig and defaultPointersConfig given by references
|
||||
func NewStaert(rootCommand *flaeg.Command) *Staert {
|
||||
s := Staert{
|
||||
command: rootCommand,
|
||||
}
|
||||
return &s
|
||||
return &Staert{command: rootCommand}
|
||||
}
|
||||
|
||||
// AddSource adds new Source to Staert, give it by reference
|
||||
|
@ -35,40 +28,31 @@ func (s *Staert) AddSource(src Source) {
|
|||
s.sources = append(s.sources, src)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
var err error
|
||||
_, err = src.Parse(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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
|
||||
f, ok := src.(*flaeg.Flaeg)
|
||||
if ok {
|
||||
if fCmd, err := f.GetCommand(); err != nil {
|
||||
// Type assertion
|
||||
if flg, ok := src.(*flaeg.Flaeg); ok {
|
||||
fCmd, err := flg.GetCommand()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if s.command != fCmd {
|
||||
//IF fleag sub-command
|
||||
}
|
||||
|
||||
// if fleag sub-command
|
||||
if s.command != fCmd {
|
||||
// if parseAllSources
|
||||
if fCmd.Metadata["parseAllSources"] == "true" {
|
||||
//IF parseAllSources
|
||||
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())
|
||||
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())
|
||||
}
|
||||
s.command = fCmd
|
||||
} else {
|
||||
// ELSE (not parseAllSources)
|
||||
s.command, err = f.Parse(fCmd)
|
||||
// (not parseAllSources)
|
||||
s.command, err = flg.Parse(fCmd)
|
||||
return s.command.Config, err
|
||||
}
|
||||
}
|
||||
|
@ -78,117 +62,19 @@ func (s *Staert) LoadConfig() (interface{}, error) {
|
|||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run calls the Run func of the command
|
||||
// Warning, Run doesn't parse the config
|
||||
func (s *Staert) Run() error {
|
||||
return s.command.Run()
|
||||
}
|
||||
|
||||
//TomlSource impement Source
|
||||
type TomlSource struct {
|
||||
filename string
|
||||
dirNfullpath []string
|
||||
fullpath string
|
||||
}
|
||||
|
||||
// NewTomlSource creates and return a pointer on TomlSource.
|
||||
// Parameter filename is the file name (without extension type, ".toml" will be added)
|
||||
// dirNfullpath may contain directories or fullpath to the file.
|
||||
func NewTomlSource(filename string, dirNfullpath []string) *TomlSource {
|
||||
return &TomlSource{filename, dirNfullpath, ""}
|
||||
}
|
||||
|
||||
// ConfigFileUsed return config file used
|
||||
func (ts *TomlSource) ConfigFileUsed() string {
|
||||
return ts.fullpath
|
||||
}
|
||||
|
||||
func preprocessDir(dirIn string) (string, error) {
|
||||
dirOut := dirIn
|
||||
expanded := os.ExpandEnv(dirIn)
|
||||
dirOut, err := filepath.Abs(expanded)
|
||||
return dirOut, err
|
||||
}
|
||||
|
||||
func findFile(filename string, dirNfile []string) string {
|
||||
for _, df := range dirNfile {
|
||||
if df != "" {
|
||||
fullPath, _ := preprocessDir(df)
|
||||
if fileInfo, err := os.Stat(fullPath); err == nil && !fileInfo.IsDir() {
|
||||
return fullPath
|
||||
}
|
||||
|
||||
fullPath = filepath.Join(fullPath, filename+".toml")
|
||||
if fileInfo, err := os.Stat(fullPath); err == nil && !fileInfo.IsDir() {
|
||||
return fullPath
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Parse calls toml.DecodeFile() func
|
||||
func (ts *TomlSource) Parse(cmd *flaeg.Command) (*flaeg.Command, error) {
|
||||
ts.fullpath = findFile(ts.filename, ts.dirNfullpath)
|
||||
if len(ts.fullpath) < 2 {
|
||||
return cmd, nil
|
||||
}
|
||||
metadata, err := toml.DecodeFile(ts.fullpath, cmd.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
boolFlags, err := flaeg.GetBoolFlags(cmd.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flaegArgs, hasUnderField, err := generateArgs(metadata, boolFlags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// fmt.Println(flaegArgs)
|
||||
err = flaeg.Load(cmd.Config, cmd.DefaultPointersConfig, flaegArgs)
|
||||
//if err!= missing parser err
|
||||
if err != nil && err != flaeg.ErrParserNotFound {
|
||||
return nil, err
|
||||
}
|
||||
if hasUnderField {
|
||||
_, err := toml.DecodeFile(ts.fullpath, cmd.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func generateArgs(metadata toml.MetaData, flags []string) ([]string, bool, error) {
|
||||
var flaegArgs []string
|
||||
keys := metadata.Keys()
|
||||
hasUnderField := false
|
||||
for i, key := range keys {
|
||||
// fmt.Println(key)
|
||||
if metadata.Type(key.String()) == "Hash" {
|
||||
// TOML hashes correspond to Go structs or maps.
|
||||
// fmt.Printf("%s could be a ptr on a struct, or a map\n", key)
|
||||
for j := i; j < len(keys); j++ {
|
||||
// fmt.Printf("%s =? %s\n", keys[j].String(), "."+key.String())
|
||||
if strings.Contains(keys[j].String(), key.String()+".") {
|
||||
hasUnderField = true
|
||||
break
|
||||
}
|
||||
}
|
||||
match := false
|
||||
for _, flag := range flags {
|
||||
if flag == strings.ToLower(key.String()) {
|
||||
match = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if match {
|
||||
flaegArgs = append(flaegArgs, "--"+strings.ToLower(key.String()))
|
||||
}
|
||||
}
|
||||
}
|
||||
return flaegArgs, hasUnderField, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue