func testReaddir(fs afero.Fs, dir string, contents []string, t *testing.T) { file, err := fs.Open(dir) if err != nil { t.Fatalf("open %q failed: %v", dir, err) } defer file.Close() s, err2 := file.Readdir(-1) if err2 != nil { t.Fatalf("readdir %q failed: %v", dir, err2) } for _, m := range contents { found := false for _, n := range s { if equal(m, n.Name()) { if found { t.Error("present twice:", m) } found = true } } if !found { t.Error("could not find", m) } } }
// NewLazyFileReader creates and initializes a new LazyFileReader of filename. // It checks whether the file can be opened. If it fails, it returns nil and an // error. func NewLazyFileReader(fs afero.Fs, filename string) (*LazyFileReader, error) { f, err := fs.Open(filename) if err != nil { return nil, err } defer f.Close() return &LazyFileReader{fs: fs, filename: filename, contents: nil, pos: 0}, nil }
// resGetLocal loads the content of a local file func resGetLocal(url string, fs afero.Fs) ([]byte, error) { filename := filepath.Join(viper.GetString("WorkingDir"), url) if e, err := helpers.Exists(filename, fs); !e { return nil, err } f, err := fs.Open(filename) if err != nil { return nil, err } return ioutil.ReadAll(f) }
// resGetLocal loads the content of a local file func resGetLocal(url string, fs afero.Fs) ([]byte, error) { p := "" if viper.GetString("WorkingDir") != "" { p = viper.GetString("WorkingDir") if helpers.FilePathSeparator != p[len(p)-1:] { p = p + helpers.FilePathSeparator } } jFile := p + url if e, err := helpers.Exists(jFile, fs); !e { return nil, err } f, err := fs.Open(jFile) if err != nil { return nil, err } return ioutil.ReadAll(f) }
// resGetCache returns the content for an ID from the file cache or an error // if the file is not found returns nil,nil func resGetCache(id string, fs afero.Fs, ignoreCache bool) ([]byte, error) { if ignoreCache { return nil, nil } fID := getCacheFileID(id) isExists, err := helpers.Exists(fID, fs) if err != nil { return nil, err } if !isExists { return nil, nil } f, err := fs.Open(fID) if err != nil { return nil, err } return ioutil.ReadAll(f) }