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

Commit

Permalink
split process into GetConfig() and Run() func (#8)
Browse files Browse the repository at this point in the history
* add config file used

* add GetConfig() func

* rm print

* fix test
  • Loading branch information
Martin Piegay committed May 24, 2016
1 parent 0fe4bbd commit f0da5ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
38 changes: 20 additions & 18 deletions staert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package staert

import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/containous/flaeg"
"os"
Expand Down Expand Up @@ -45,30 +44,29 @@ func (s *Staert) getConfig(cmd *flaeg.Command) error {
return nil
}

// Run calls the Run func of the command with the parsed config
func (s *Staert) Run() error {
cmd := s.command
// GetConfig gets the parsed config
func (s *Staert) GetConfig() (interface{}, error) {
for _, src := range s.sources {
//Type assertion
f, ok := src.(*flaeg.Flaeg)
if ok {
if fCmd, err := f.GetCommand(); err != nil {
return err
} else if cmd != fCmd {
if err := f.Run(); err != nil {
return err
}
return nil
return nil, err
} else if s.command != fCmd {
//IF fleag sub-command
s.command = fCmd
_, err = f.Parse(s.command)
return nil, err
}
}
}
if err := s.getConfig(cmd); err != nil {
return err
}
if err := cmd.Run(); err != nil {
return err
}
return nil
err := s.getConfig(s.command)
return s.command.Config, err
}

// Run calls the Run func of the command with the parsed config
func (s *Staert) Run() error {
return s.command.Run()
}

//TomlSource impement Source
Expand All @@ -85,6 +83,11 @@ 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
if strings.HasPrefix(dirIn, "$") {
Expand Down Expand Up @@ -120,7 +123,6 @@ func (ts *TomlSource) Parse(cmd *flaeg.Command) (*flaeg.Command, error) {
if len(ts.fullpath) < 2 {
return cmd, nil
}
fmt.Printf("Read config in file : %s\n", ts.fullpath)
metadata, err := toml.DecodeFile(ts.fullpath, cmd.Config)
if err != nil {
return nil, err
Expand Down
20 changes: 20 additions & 0 deletions staert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,10 @@ func TestRunFleagFieldUnderPtrUnderPtr1Command(t *testing.T) {
s := NewStaert(rootCmd)
fs := flaeg.New(rootCmd, args)
s.AddSource(fs)
_, err := s.GetConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
if err := s.Run(); err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1165,6 +1169,10 @@ func TestRunFleagFieldUnderPtrUnderPtr2Command(t *testing.T) {
fs.AddCommand(versionCmd)
s.AddSource(fs)
//check in command run func
_, err := s.GetConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
if err := s.Run(); err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1259,6 +1267,10 @@ func TestRunFleagVersion2CommandCallVersion(t *testing.T) {
fs.AddCommand(versionCmd)
s.AddSource(fs)
//check in command run func
_, err := s.GetConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
if err := s.Run(); err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1357,6 +1369,10 @@ func TestRunMergeFlaegToml2CommmandCallRootCmd(t *testing.T) {
toml := NewTomlSource("trivial", []string{"./toml/", "/any/other/path"})
s.AddSource(toml)
//check in command run func
_, err := s.GetConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
if err := s.Run(); err != nil {
t.Fatalf("Error %s", err.Error())
}
Expand Down Expand Up @@ -1492,6 +1508,10 @@ func TestTomlMissingCustomParser(t *testing.T) {
s := NewStaert(command)
toml := NewTomlSource("missingCustomParser", []string{"toml"})
s.AddSource(toml)
_, err := s.GetConfig()
if err != nil {
t.Fatalf("Error %s", err.Error())
}
if err := s.Run(); err != nil {
t.Fatalf("Error :%s", err)
}
Expand Down

0 comments on commit f0da5ea

Please sign in to comment.