-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from ysim/list-values-for-given-key
List values for given key
- Loading branch information
Showing
6 changed files
with
146 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
) | ||
|
||
func listValuesForKey(key string, values *[]string) filepath.WalkFunc { | ||
return func(fullFilepath string, info os.FileInfo, err error) error { | ||
shouldSkip := ShouldSkipFile(info, err) | ||
if shouldSkip { | ||
return nil | ||
} | ||
|
||
recipeFile, err := ParseFile(fullFilepath) | ||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"file": fullFilepath, | ||
}).Warn(err.Error()) | ||
return nil | ||
} | ||
|
||
frontMatter, err := ParseFrontMatter(recipeFile.FrontMatter) | ||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"file": fullFilepath, | ||
}).Warn("Unknown type detected in front matter") | ||
} | ||
|
||
valueOfKey := frontMatter[key] | ||
for _, v := range valueOfKey { | ||
if StringInSlice(v, *values) == false { | ||
*values = append(*values, v) | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func List(key string) { | ||
if key == "" { | ||
fmt.Println("Functionality to list all keys is not implemented yet. (#21)") | ||
} else { | ||
values := make([]string, 0) | ||
listErr := filepath.Walk(prefix, listValuesForKey(key, &values)) | ||
if len(values) == 0 { | ||
fmt.Printf("No values were found for key '%s'.\n", key) | ||
return | ||
} | ||
sort.Strings(values) | ||
PrintArrayOnNewlines(values) | ||
if listErr != nil { | ||
errMsg := "An error occurred while attempting to list values for key '%s'." | ||
fmt.Printf(errMsg, key) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func StringInSlice(s string, slice []string) bool { | ||
for _, i := range slice { | ||
if s == i { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func PrintArrayOnNewlines(a []string) { | ||
for _, v := range a { | ||
fmt.Println(v) | ||
} | ||
} |