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

Commit

Permalink
TomlSource continue if no file found & allow fullpath in the slice (#7)
Browse files Browse the repository at this point in the history
* TomlSource continue if no file found

* findFile allows fullpath in parametre dirNfullpath
+ refactor
  • Loading branch information
Martin Piegay committed May 19, 2016
1 parent a65e5b7 commit 0fe4bbd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
48 changes: 22 additions & 26 deletions staert.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ func (s *Staert) Run() error {

//TomlSource impement Source
type TomlSource struct {
filename string
directories []string
fullpath string
filename string
dirNfullpath []string
fullpath string
}

// NewTomlSource creats and return a pointer on TomlSource. Parameter filename is the name of the file without neither fullpath not extension type and directories is a slice of paths
// (staert look for the toml file from the first directory to last one)
func NewTomlSource(filename string, directories []string) *TomlSource {
return &TomlSource{filename, directories, ""}
// NewTomlSource creats 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, ""}
}

func preprocessDir(dirIn string) (string, error) {
Expand All @@ -92,39 +93,34 @@ func preprocessDir(dirIn string) (string, error) {
end = len(dirIn)
}
dirOut = os.Getenv(dirIn[1:end]) + dirIn[end:]

}
dirOut, err := filepath.Abs(dirOut)
return dirOut, err
}

func (ts *TomlSource) findFile() error {
for _, d := range ts.directories {
if d != "" {
dir, err := preprocessDir(d)
if err != nil {
return 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 := dir + "/" + ts.filename + ".toml"
// fmt.Printf("Lookup fullpath %s\n", fullpath)
// Test if the file exits
if _, err := os.Stat(fullpath); err == nil {
//Turn fullpath in absolute representation of path

// fmt.Printf("File in fullpath %s exists\n", fullpath)
ts.fullpath = fullpath
return nil
fullpath = fullpath + "/" + filename + ".toml"
if fileinfo, err := os.Stat(fullpath); err == nil && !fileinfo.IsDir() {
return fullpath
}
}
}
return fmt.Errorf("No file %s.toml found in directories %+v", ts.filename, ts.directories)
return ""
}

// Parse calls Flaeg Load Function
func (ts *TomlSource) Parse(cmd *flaeg.Command) (*flaeg.Command, error) {
if err := ts.findFile(); err != nil {
return nil, err
ts.fullpath = findFile(ts.filename, ts.dirNfullpath)
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
51 changes: 38 additions & 13 deletions staert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,13 +1406,20 @@ func TestTomlSourceErrorFileNotFound(t *testing.T) {
return nil
},
}
checkCmd := *rootCmd
s := NewStaert(rootCmd)
toml := NewTomlSource("nothing", []string{"../path", "/any/other/path"})
s.AddSource(toml)

//Check
if err := s.getConfig(rootCmd); err == nil || !strings.Contains(err.Error(), "No file nothing.toml found in directories [../path /any/other/path]") {
t.Errorf("Expected Error : help requested \nGot Error : %s", err)
if err := s.getConfig(rootCmd); err != nil {
t.Errorf("No Error expected\nGot Error : %s", err)
}
if !reflect.DeepEqual(checkCmd.Config, rootCmd.Config) {
t.Errorf("Expected %+v \nGot %+v", checkCmd.Config, rootCmd.Config)
}
if !reflect.DeepEqual(checkCmd.DefaultPointersConfig, rootCmd.DefaultPointersConfig) {
t.Errorf("Expected %+v \nGot %+v", checkCmd.DefaultPointersConfig, rootCmd.DefaultPointersConfig)
}

}
Expand All @@ -1423,11 +1430,12 @@ func TestPreprocessDir(t *testing.T) {
t.Fatalf("Error: %s", err.Error())
}
checkMap := map[string]string{
".": thisPath,
"$HOME": os.Getenv("HOME"),
"dir1/dir2": thisPath + "/dir1/dir2",
"$HOME/dir1/dir2": os.Getenv("HOME") + "/dir1/dir2",
"/etc/test": "/etc/test",
".": thisPath,
"$HOME": os.Getenv("HOME"),
"dir1/dir2": thisPath + "/dir1/dir2",
"$HOME/dir1/dir2": os.Getenv("HOME") + "/dir1/dir2",
"/etc/test": "/etc/test",
"/etc/dir1/file1.ext": "/etc/dir1/file1.ext",
}
for in, check := range checkMap {
out, err := preprocessDir(in)
Expand All @@ -1442,18 +1450,16 @@ func TestPreprocessDir(t *testing.T) {
}

func TestFindFile(t *testing.T) {
toml := &TomlSource{"nothing", []string{"", "$HOME/test", "toml"}, ""}
if err := toml.findFile(); err != nil {
t.Fatalf("Error: %s", err.Error())
}
result := findFile("nothing", []string{"", "$HOME/test", "toml"})

//check
thisPath, err := filepath.Abs(".")
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
expected := thisPath + "/toml/nothing.toml"
if toml.fullpath != expected {
t.Fatalf("Expected %s\ngot %s", expected, toml.fullpath)
if result != expected {
t.Fatalf("Expected %s\ngot %s", expected, result)
}
}

Expand Down Expand Up @@ -1496,3 +1502,22 @@ func TestTomlMissingCustomParser(t *testing.T) {
t.Fatalf("Expected %+v\ngot %+v", check.PtrCustom, config.PtrCustom)
}
}
func TestFindFileSliceFileAndDirLastIf(t *testing.T) {

//check
thisPath, _ := filepath.Abs(".")
check := thisPath + "/toml/trivial.toml"
if result := findFile("trivial", []string{"./toml/", "/any/other/path"}); result != check {
t.Fatalf("Expected %s\nGot %s", check, result)
}
}
func TestFindFileSliceFileAndDirFirstIf(t *testing.T) {
inFilename := ""
inDirNfile := []string{"$PWD/toml/nothing.toml"}
//check
thisPath, _ := filepath.Abs(".")
check := thisPath + "/toml/nothing.toml"
if result := findFile(inFilename, inDirNfile); result != check {
t.Fatalf("Expected %s\nGot %s", check, result)
}
}

0 comments on commit 0fe4bbd

Please sign in to comment.