本文整理汇总了Golang中github.com/kr/fs.Walk函数的典型用法代码示例。如果您正苦于以下问题:Golang Walk函数的具体用法?Golang Walk怎么用?Golang Walk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Walk函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: expandBundlePath
// expandBundlePath calls filepath.Glob first, then recursively adds
// descendents of any directories specified.
func expandBundlePath(pathGlob string) ([]string, error) {
paths, err := filepath.Glob(pathGlob)
if err != nil {
return nil, err
}
for _, path := range paths {
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if fi.Mode().IsDir() {
w := fs.Walk(path)
for w.Step() {
if err := w.Err(); err != nil {
return nil, err
}
paths = append(paths, w.Path())
}
}
}
return paths, nil
}
示例2: main
func main() {
if len(os.Args) > 1 {
starting_path = os.Args[1]
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 2, 0, 1, ' ', tabwriter.AlignRight)
walker := fs.Walk(starting_path)
for walker.Step() {
if err := walker.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
path := walker.Path()
if REGEX_EXCLUDE_PATH.FindStringSubmatch(path) != nil {
continue
}
info := walker.Stat()
file_type := "f"
if info.IsDir() {
file_type = "d"
}
fmt.Fprintln(w, file_type+" \t"+(info.ModTime().Format(DATE_LAYOUT)+" \t"+strconv.FormatInt(info.Size(), 10)+"\t\t"+path))
}
w.Flush()
}
示例3: Walk
func (w *Walker) Walk() fusefs.Tree {
wg := sync.WaitGroup{}
paths := make(chan string, runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ {
go worker(w, paths, &wg)
}
walker := fs.Walk(w.Path)
for walker.Step() {
if err := walker.Err(); err != nil {
continue
}
if walker.Stat().IsDir() {
continue
}
wg.Add(1)
paths <- walker.Path()
}
close(paths)
wg.Wait()
return w.tree
}
示例4: RewriteImports
func RewriteImports(path string, rw func(string) string, filter func(string) bool) error {
w := fs.Walk(path)
for w.Step() {
rel := w.Path()[len(path):]
if len(rel) == 0 {
continue
}
rel = rel[1:]
if strings.HasPrefix(rel, ".git") || strings.HasPrefix(rel, "vendor") {
w.SkipDir()
continue
}
if !strings.HasSuffix(w.Path(), ".go") {
continue
}
if !filter(rel) {
continue
}
err := rewriteImportsInFile(w.Path(), rw)
if err != nil {
fmt.Println("rewrite error: ", err)
}
}
return nil
}
示例5: copySrc
func copySrc(dir string, g *Godeps) error {
ok := true
for _, dep := range g.Deps {
w := fs.Walk(dep.dir)
for w.Step() {
if w.Err() != nil {
log.Println(w.Err())
ok = false
continue
}
if c := w.Stat().Name()[0]; c == '.' || c == '_' {
// Skip directories using a rule similar to how
// the go tool enumerates packages.
// See $GOROOT/src/cmd/go/main.go:/matchPackagesInFs
w.SkipDir()
}
if w.Stat().IsDir() {
continue
}
dst := filepath.Join(dir, w.Path()[len(dep.ws)+1:])
if err := copyFile(dst, w.Path()); err != nil {
log.Println(err)
ok = false
}
}
}
if !ok {
return errors.New("error copying source code")
}
return nil
}
示例6: TestBug3486
func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
root, err := filepath.EvalSymlinks(runtime.GOROOT())
if err != nil {
t.Fatal(err)
}
lib := filepath.Join(root, "lib")
src := filepath.Join(root, "src")
seenSrc := false
walker := fs.Walk(root)
for walker.Step() {
if walker.Err() != nil {
t.Fatal(walker.Err())
}
switch walker.Path() {
case lib:
walker.SkipDir()
case src:
seenSrc = true
}
}
if !seenSrc {
t.Fatalf("%q not seen", src)
}
}
示例7: copySrc
func copySrc(dir string, deps []Dependency) error {
ok := true
for _, dep := range deps {
srcdir := filepath.Join(dep.ws, "src")
rel, err := filepath.Rel(srcdir, dep.dir)
if err != nil { // this should never happen
return err
}
dstpkgroot := filepath.Join(dir, rel)
err = os.RemoveAll(dstpkgroot)
if err != nil {
log.Println(err)
ok = false
}
w := fs.Walk(dep.dir)
for w.Step() {
err = copyPkgFile(dir, srcdir, w)
if err != nil {
log.Println(err)
ok = false
}
}
}
if !ok {
return errors.New("error copying source code")
}
return nil
}
示例8: marcher
// Walk through root and hash each file, return a map with
// filepath for key and hash for value, check only file not dir
func marcher(dir string, method string) map[string]string {
fmt.Println("")
fmt.Println("Scanner Report")
fmt.Println("==============")
fmt.Println("")
res := make(map[string]string)
walker := fs.Walk(dir)
for walker.Step() {
// Start walking
if err := walker.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
// Check if it is a file
finfo, err := os.Stat(walker.Path())
if err != nil {
fmt.Println(err)
continue
}
if finfo.IsDir() {
// it's a dir so pass and continue
continue
} else {
// it's a file so add couple to data map
path := walker.Path()
hash := hasher(walker.Path(), method)
res[path] = hash
fmt.Println(hash, "\t", path)
}
}
fmt.Println("")
return res
}
示例9: readArticles
func readArticles() ([]*Article, []string, error) {
timeStart := time.Now()
walker := fs.Walk("blog_posts")
var res []*Article
var dirs []string
for walker.Step() {
if walker.Err() != nil {
fmt.Printf("readArticles: walker.Step() failed with %s\n", walker.Err())
return nil, nil, walker.Err()
}
st := walker.Stat()
path := walker.Path()
if st.IsDir() {
dirs = append(dirs, path)
continue
}
a, err := readArticle(path)
if err != nil {
fmt.Printf("readArticle() of %s failed with %s\n", path, err)
return nil, nil, err
}
if a != nil {
res = append(res, a)
}
}
fmt.Printf("read %d articles in %s\n", len(res), time.Since(timeStart))
return res, dirs, nil
}
示例10: RewriteImports
func RewriteImports(path string, rw func(string) string, filter func(string) bool) error {
w := fs.Walk(path)
for w.Step() {
rel := w.Path()[len(path):]
if len(rel) == 0 {
continue
}
rel = rel[1:]
if strings.HasPrefix(rel, ".git") || strings.HasPrefix(rel, "vendor") {
w.SkipDir()
continue
}
if !filter(rel) {
continue
}
dir, fi := filepath.Split(w.Path())
good, err := build.Default.MatchFile(dir, fi)
if err != nil {
return err
}
if !good {
continue
}
err = rewriteImportsInFile(w.Path(), rw)
if err != nil {
fmt.Println("rewrite error: ", err)
return err
}
}
return nil
}
示例11: StdPopulate
func (r *Radio) StdPopulate() error {
// Add a dummy manual playlist
r.NewPlaylist("manual")
// Add local directory
playlistsDirs := []string{"/playlists"}
playlistsDirs = append(playlistsDirs, path.Join(os.Getenv("HOME"), "playlists"))
playlistsDirs = append(playlistsDirs, path.Join("/home", "playlists"))
dir, err := os.Getwd()
if err == nil && os.Getenv("NO_LOCAL_PLAYLISTS") != "1" {
r.NewDirectoryPlaylist("local directory", dir)
playlistsDirs = append(playlistsDirs, path.Join(dir, "playlists"))
}
// Add each folders in '/playlists' and './playlists'
for _, playlistsDir := range playlistsDirs {
walker := fs.Walk(playlistsDir)
for walker.Step() {
if walker.Path() == playlistsDir {
continue
}
if err := walker.Err(); err != nil {
logrus.Warnf("walker error: %v", err)
continue
}
var realpath string
if walker.Stat().IsDir() {
realpath = walker.Path()
walker.SkipDir()
} else {
realpath, err = filepath.EvalSymlinks(walker.Path())
if err != nil {
logrus.Warnf("filepath.EvalSymlinks error for %q: %v", walker.Path(), err)
continue
}
}
stat, err := os.Stat(realpath)
if err != nil {
logrus.Warnf("os.Stat error: %v", err)
continue
}
if stat.IsDir() {
r.NewDirectoryPlaylist(fmt.Sprintf("playlist: %s", walker.Stat().Name()), realpath)
}
}
}
// Add 'standard' music paths
r.NewDirectoryPlaylist("iTunes Music", "~/Music/iTunes/iTunes Media/Music/")
r.NewDirectoryPlaylist("iTunes Podcasts", "~/Music/iTunes/iTunes Media/Podcasts/")
r.NewDirectoryPlaylist("iTunes Music", "/home/Music/iTunes/iTunes Media/Music/")
r.NewDirectoryPlaylist("iTunes Podcasts", "/home/Music/iTunes/iTunes Media/Podcasts/")
return nil
}
示例12: ExampleWalker
func ExampleWalker() {
walker := fs.Walk("/usr/lib")
for walker.Step() {
if err := walker.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
fmt.Println(walker.Path())
}
}
示例13: pythonSourceFiles
// Get all python source files under dir
func pythonSourceFiles(dir string, discoveredScripts map[string]bool) (files []string) {
walker := fs.Walk(dir)
for walker.Step() {
if err := walker.Err(); err == nil && !walker.Stat().IsDir() && filepath.Ext(walker.Path()) == ".py" {
file := walker.Path()
_, found := discoveredScripts[file]
if !found {
files = append(files, filepath.ToSlash(file))
discoveredScripts[file] = true
}
}
}
return
}
示例14: rewriteTree
// rewriteTree recursively visits the go files in path, rewriting
// import statments according to the rules for func qualify.
func rewriteTree(path, qual string, paths []string) error {
w := fs.Walk(path)
for w.Step() {
if w.Err() != nil {
log.Println("rewrite:", w.Err())
continue
}
if !w.Stat().IsDir() && strings.HasSuffix(w.Path(), ".go") {
err := rewriteGoFile(w.Path(), qual, paths)
if err != nil {
return err
}
}
}
return nil
}
示例15: AutoUpdate
func (p *Playlist) AutoUpdate() error {
if p.Path == "" {
logrus.Debugf("Playlist %q is not dynamic, skipping update", p.Name)
return nil
}
// if we are here, the playlist is based on local file system
logrus.Infof("Updating playlist %q", p.Name)
p.Status = "updating"
walker := fs.Walk(p.Path)
for walker.Step() {
if err := walker.Err(); err != nil {
logrus.Warnf("walker error: %v", err)
continue
}
stat := walker.Stat()
if stat.IsDir() {
switch stat.Name() {
case ".git", "bower_components":
walker.SkipDir()
}
} else {
switch stat.Name() {
case ".DS_Store":
continue
}
p.NewLocalTrack(walker.Path())
}
}
logrus.Infof("Playlist %q updated, %d tracks", p.Name, len(p.Tracks))
if p.Stats.Tracks > 0 {
p.Status = "ready"
} else {
p.Status = "empty"
}
p.ModificationDate = time.Now()
return nil
}