Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
refactor func name and update readme
Browse files Browse the repository at this point in the history
Signed-off-by: Martin <martin.piegay@zenika.com>
  • Loading branch information
Martin committed May 27, 2016
1 parent f0da5ea commit 46fbd78
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 34 deletions.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -106,14 +106,26 @@ Add TOML and flæg sources
s.AddSource(f)
```
NB : You can change order, so that, flaeg configuration will overwrite toml one
### Call Run
### Load your configuration
Just call Run function :
```go
loadedConfig, err := s.LoadConfig();
if err != nil {
//OOPS
}
//DO WATH YOU WANT WITH loadedConfig
//OR CALL RUN FUNC
```
### You can call Run
Run function will call the func `run()` from the command :
```go
if err := s.Run(); err != nil {
//OOPS
}
}
```
NB : If you didn't call `LoadConfig()` before, your func `run()` will use your original configuration
### Let's run example

TOML file `./toml/example.toml` :
Expand Down
17 changes: 9 additions & 8 deletions staert.go
Expand Up @@ -33,7 +33,7 @@ func (s *Staert) AddSource(src Source) {
}

// getConfig for a flaeg.Command run sources Parse func in the raw
func (s *Staert) getConfig(cmd *flaeg.Command) error {
func (s *Staert) parseConfigAllSources(cmd *flaeg.Command) error {
for _, src := range s.sources {
var err error
_, err = src.Parse(cmd)
Expand All @@ -44,8 +44,9 @@ func (s *Staert) getConfig(cmd *flaeg.Command) error {
return nil
}

// GetConfig gets the parsed config
func (s *Staert) GetConfig() (interface{}, error) {
// 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)
Expand All @@ -54,17 +55,17 @@ func (s *Staert) GetConfig() (interface{}, error) {
return nil, err
} else if s.command != fCmd {
//IF fleag sub-command
s.command = fCmd
_, err = f.Parse(s.command)
return nil, err
s.command, err = f.Parse(fCmd)
return s.command.Config, err
}
}
}
err := s.getConfig(s.command)
err := s.parseConfigAllSources(s.command)
return s.command.Config, err
}

// Run calls the Run func of the command with the parsed config
// Run calls the Run func of the command
// Warning, Run doesn't parse the config
func (s *Staert) Run() error {
return s.command.Run()
}
Expand Down
115 changes: 90 additions & 25 deletions staert_test.go
Expand Up @@ -81,7 +81,7 @@ func TestFleagSourceNoArgs(t *testing.T) {
s := NewStaert(rootCmd)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func TestFleagSourcePtrUnderPtrArgs(t *testing.T) {
s := NewStaert(rootCmd)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestFleagSourceFieldUnderPtrUnderPtrArgs(t *testing.T) {
s := NewStaert(rootCmd)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestTomlSourceNothing(t *testing.T) {
toml := NewTomlSource("nothing", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)

if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -333,7 +333,7 @@ func TestTomlSourceTrivial(t *testing.T) {
toml := NewTomlSource("trivial", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)

if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestTomlSourcePointer(t *testing.T) {
toml := NewTomlSource("pointer", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)

if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -456,7 +456,7 @@ func TestTomlSourcePointerUnderPointer(t *testing.T) {
toml := NewTomlSource("pointerUnderPointer", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)

if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -518,7 +518,7 @@ func TestTomlSourceFieldUnderPointerUnderPointer(t *testing.T) {
toml := NewTomlSource("fieldUnderPtrUnderPtr", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)

if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -583,7 +583,7 @@ func TestMergeTomlNothingFlaegNoArgs(t *testing.T) {
s.AddSource(toml)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}

Expand Down Expand Up @@ -647,7 +647,7 @@ func TestMergeTomlFieldUnderPointerUnderPointerFlaegNoArgs(t *testing.T) {
s.AddSource(toml)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}

Expand Down Expand Up @@ -714,7 +714,7 @@ func TestMergeTomlTrivialFlaegOverwriteField(t *testing.T) {
s.AddSource(toml)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}
//Check
Expand Down Expand Up @@ -781,7 +781,7 @@ func TestMergeTomlPointerUnderPointerFlaegManyArgs(t *testing.T) {
s.AddSource(toml)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}

Expand Down Expand Up @@ -852,7 +852,7 @@ func TestMergeFlaegNoArgsTomlNothing(t *testing.T) {
s.AddSource(fs)
toml := NewTomlSource("nothing", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}

Expand Down Expand Up @@ -917,7 +917,7 @@ func TestMergeFlaegFieldUnderPointerUnderPointerTomlNothing(t *testing.T) {
s.AddSource(fs)
toml := NewTomlSource("nothing", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}

Expand Down Expand Up @@ -987,7 +987,7 @@ func TestMergeFlaegManyArgsTomlOverwriteField(t *testing.T) {
s.AddSource(fs)
toml := NewTomlSource("trivial", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("Error %s", err.Error())
}

Expand Down Expand Up @@ -1069,7 +1069,7 @@ func TestRunFleagFieldUnderPtrUnderPtr1Command(t *testing.T) {
s := NewStaert(rootCmd)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
_, err := s.GetConfig()
_, err := s.LoadConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1169,7 +1169,7 @@ func TestRunFleagFieldUnderPtrUnderPtr2Command(t *testing.T) {
fs.AddCommand(versionCmd)
s.AddSource(fs)
//check in command run func
_, err := s.GetConfig()
_, err := s.LoadConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1215,7 +1215,7 @@ func TestRunFleagVersion2CommandCallVersion(t *testing.T) {
args := []string{
"--toto", //no effect
"version", //call Command
// "-v2.2beta",
"-v2.2beta",
}

rootCmd := &flaeg.Command{
Expand Down Expand Up @@ -1254,8 +1254,8 @@ func TestRunFleagVersion2CommandCallVersion(t *testing.T) {
Run: func() error {
fmt.Fprintf(&b, "Version %s \n", versionConfig.Version)
//CHECK
if versionConfig.Version != "0.1" {
return fmt.Errorf("expected 0.1 got %s", versionConfig.Version)
if versionConfig.Version != "2.2beta" {
return fmt.Errorf("expected 2.2beta got %s", versionConfig.Version)
}
return nil

Expand All @@ -1267,15 +1267,15 @@ func TestRunFleagVersion2CommandCallVersion(t *testing.T) {
fs.AddCommand(versionCmd)
s.AddSource(fs)
//check in command run func
_, err := s.GetConfig()
_, err := s.LoadConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
if err := s.Run(); err != nil {
t.Fatalf("Error %s", err.Error())
}
//check buffer
checkB := `Version 0.1`
checkB := `Version 2.2beta`
if !strings.Contains(b.String(), checkB) {
t.Fatalf("Error output doesn't contain %s,\ngot: %s", checkB, &b)
}
Expand Down Expand Up @@ -1369,7 +1369,7 @@ func TestRunMergeFlaegToml2CommmandCallRootCmd(t *testing.T) {
toml := NewTomlSource("trivial", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)
//check in command run func
_, err := s.GetConfig()
_, err := s.LoadConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1428,7 +1428,7 @@ func TestTomlSourceErrorFileNotFound(t *testing.T) {
s.AddSource(toml)

//Check
if err := s.getConfig(rootCmd); err != nil {
if err := s.parseConfigAllSources(rootCmd); err != nil {
t.Errorf("No Error expected\nGot Error : %s", err)
}
if !reflect.DeepEqual(checkCmd.Config, rootCmd.Config) {
Expand Down Expand Up @@ -1508,7 +1508,7 @@ func TestTomlMissingCustomParser(t *testing.T) {
s := NewStaert(command)
toml := NewTomlSource("missingCustomParser", []string{"toml"})
s.AddSource(toml)
_, err := s.GetConfig()
_, err := s.LoadConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1541,3 +1541,68 @@ func TestFindFileSliceFileAndDirFirstIf(t *testing.T) {
t.Fatalf("Expected %s\nGot %s", check, result)
}
}
func TestRunWithoutLoadConfig(t *testing.T) {
//use buffer instead of stdout
var b bytes.Buffer
//Init
config := &StructPtr{
PtrStruct1: &Struct1{
S1Int: 1,
S1String: "S1StringInitConfig",
},
DurationField: time.Second,
}
defaultPointersConfig := &StructPtr{
PtrStruct1: &Struct1{
S1Int: 11,
S1String: "S1StringDefaultPointersConfig",
S1Bool: true,
S1PtrStruct3: &Struct3{
S3Float64: 11.11,
},
},
PtrStruct2: &Struct2{
S2Int64: 22,
S2String: "S2StringDefaultPointersConfig",
S2Bool: false,
},
}

args := []string{
"--ptrstruct2",
}

//Test
rootCmd := &flaeg.Command{
Name: "test",
Description: "description test",
Config: config,
DefaultPointersConfig: defaultPointersConfig,
Run: func() error {
fmt.Fprintf(&b, "Run with config :\n%+v\n", config)
return nil
},
}
s := NewStaert(rootCmd)
toml := NewTomlSource("trivial", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
// s.LoadConfig() IS MISSING
s.Run()
check := &StructPtr{
PtrStruct1: &Struct1{
S1Int: 1,
S1String: "S1StringInitConfig",
},
DurationField: time.Second,
}
if !reflect.DeepEqual(rootCmd.Config, check) {
t.Fatalf("\nexpected\t: %+v\ngot\t\t\t: %+v\n", check, rootCmd.Config)
}
//check buffer
checkB := `Run with config`
if !strings.Contains(b.String(), checkB) {
t.Fatalf("Error output doesn't contain %s,\ngot: %s", checkB, &b)
}
}

0 comments on commit 46fbd78

Please sign in to comment.