本文整理汇总了Golang中github.com/spf13/afero.Fs类的典型用法代码示例。如果您正苦于以下问题:Golang Fs类的具体用法?Golang Fs怎么用?Golang Fs使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Fs类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SafeWriteToDisk
func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
dir, _ := filepath.Split(inpath)
ospath := filepath.FromSlash(dir)
if ospath != "" {
err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
if err != nil {
return
}
}
exists, err := Exists(inpath, fs)
if err != nil {
return
}
if exists {
return fmt.Errorf("%v already exists", inpath)
}
file, err := fs.Create(inpath)
if err != nil {
return
}
defer file.Close()
_, err = io.Copy(file, r)
return
}
示例2: IsDir
func IsDir(path string, fs afero.Fs) (bool, error) {
fi, err := fs.Stat(path)
if err != nil {
return false, err
}
return fi.IsDir(), nil
}
示例3: FindArchetype
// FindArchetype takes a given kind/archetype of content and returns an output
// path for that archetype. If no archetype is found, an empty string is
// returned.
func FindArchetype(fs afero.Fs, kind string) (outpath string) {
search := []string{helpers.AbsPathify(viper.GetString("archetypeDir"))}
if viper.GetString("theme") != "" {
themeDir := filepath.Join(helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), "/archetypes/")
if _, err := fs.Stat(themeDir); os.IsNotExist(err) {
jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", viper.GetString("theme"), themeDir)
} else {
search = append(search, themeDir)
}
}
for _, x := range search {
// If the new content isn't in a subdirectory, kind == "".
// Therefore it should be excluded otherwise `is a directory`
// error will occur. github.com/spf13/hugo/issues/411
var pathsToCheck []string
if kind == "" {
pathsToCheck = []string{"default.md", "default"}
} else {
pathsToCheck = []string{kind + ".md", kind, "default.md", "default"}
}
for _, p := range pathsToCheck {
curpath := filepath.Join(x, p)
jww.DEBUG.Println("checking", curpath, "for archetypes")
if exists, _ := helpers.Exists(curpath, fs); exists {
jww.INFO.Println("curpath: " + curpath)
return curpath
}
}
}
return ""
}
示例4: testReaddir
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)
}
}
}
示例5: NewLazyFileReader
// 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
}
示例6: getSettingsFolder
// getSettingsFolder returns the path of the settings folder
// and ensures that the folder exists.
func getSettingsFolder(fs afero.Fs, baseFolder string) string {
settingsFolder := filepath.Join(baseFolder, ".dee")
createFolderError := fs.MkdirAll(settingsFolder, 0700)
if createFolderError != nil {
panic(createFolderError)
}
return settingsFolder
}
示例7: lstatIfOs
// Code copied from Afero's path.go
// if the filesystem is OsFs use Lstat, else use fs.Stat
func lstatIfOs(fs afero.Fs, path string) (info os.FileInfo, err error) {
_, ok := fs.(*afero.OsFs)
if ok {
info, err = os.Lstat(path)
} else {
info, err = fs.Stat(path)
}
return
}
示例8: Exists
// Check if File / Directory Exists
func Exists(path string, fs afero.Fs) (bool, error) {
_, err := fs.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
示例9: DirExists
// Check if Exists && is Directory
func DirExists(path string, fs afero.Fs) (bool, error) {
fi, err := fs.Stat(path)
if err == nil && fi.IsDir() {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
示例10: resGetLocal
// 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)
}
示例11: resWriteCache
// resWriteCache writes bytes to an ID into the file cache
func resWriteCache(id string, c []byte, fs afero.Fs) error {
fID := getCacheFileID(id)
f, err := fs.Create(fID)
if err != nil {
return err
}
n, err := f.Write(c)
if n == 0 {
return errors.New("No bytes written to file: " + fID)
}
return err
}
示例12: writeFile
func writeFile(t *testing.T, fs afero.Fs, fname string, flag int, text string) string {
f, err := fs.OpenFile(fname, flag, 0666)
if err != nil {
t.Fatalf("Open: %v", err)
}
n, err := io.WriteString(f, text)
if err != nil {
t.Fatalf("WriteString: %d, %v", n, err)
}
f.Close()
data, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
return string(data)
}
示例13: resWriteCache
// resWriteCache writes bytes to an ID into the file cache
func resWriteCache(id string, c []byte, fs afero.Fs) error {
fID := getCacheFileID(id)
f, err := fs.Create(fID)
if err != nil {
return errors.New("Error: " + err.Error() + ". Failed to create file: " + fID)
}
defer f.Close()
n, err := f.Write(c)
if n == 0 {
return errors.New("No bytes written to file: " + fID)
}
if err != nil {
return errors.New("Error: " + err.Error() + ". Failed to write to file: " + fID)
}
return nil
}
示例14: newFile
func newFile(testName string, fs afero.Fs, t *testing.T) (f afero.File) {
fs.MkdirAll(testDir, 0777)
f, err := fs.Create(path.Join(testDir, testName))
if err != nil {
t.Fatalf("%v: create %s: %s", fs.Name(), testName, err)
}
_, err = f.WriteString("")
if err != nil {
t.Fatalf("%v: writestring %s: %s", fs.Name(), testName, err)
}
return f
}
示例15: resGetLocal
// 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)
}