grim/resticide

32bc94f303e2
Give the --help output some tlc, also added a --version argument
package loader
import (
"io/ioutil"
"os"
"path/filepath"
"bitbucket.org/rw_grim/resticide/reporter"
"bitbucket.org/rw_grim/resticide/test"
)
// Loader is a function that can load a file
type Loader func([]byte) (string, []test.Request, error)
var loaders = map[string]Loader{
".json": jsonLoader,
}
// LoadTests will walk the file system attempting to load tests from the files
// it can read.
func LoadTests(path string, reporter *reporter.Reporter) ([]test.Test, error) {
tests := []test.Test{}
err := filepath.Walk(path, func(path string, _ os.FileInfo, _ error) error {
stat, err := os.Stat(path)
if os.IsNotExist(err) {
return nil
}
if !stat.Mode().IsRegular() {
return nil
}
ext := filepath.Ext(path)
if loader, found := loaders[ext]; found {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
name, requests, err := loader(data)
if err != nil {
reporter.LoadTestFailed(path, err)
return nil
}
test := test.Test{
Name: name,
Requests: requests,
}
tests = append(tests, test)
}
return nil
})
return tests, err
}