• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang fs.Walk函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/kr/fs.Walk函数的典型用法代码示例。如果您正苦于以下问题:Golang Walk函数的具体用法?Golang Walk怎么用?Golang Walk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Walk函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:ildarisaev,项目名称:srclib,代码行数:27,代码来源:bundle.go


示例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()
}
开发者ID:aikawad,项目名称:docker-workshop,代码行数:30,代码来源:walk.go


示例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
}
开发者ID:solarnz,项目名称:jpgfs,代码行数:27,代码来源:walker.go


示例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
}
开发者ID:whyrusleeping,项目名称:gx-go,代码行数:29,代码来源:rewrite.go


示例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
}
开发者ID:pedantic-git,项目名称:godep,代码行数:31,代码来源:save.go


示例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)
	}
}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:25,代码来源:walk_test.go


示例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
}
开发者ID:maxamillion,项目名称:godep,代码行数:28,代码来源:save.go


示例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
}
开发者ID:jhautefeuille,项目名称:kdouble,代码行数:35,代码来源:kdouble.go


示例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
}
开发者ID:kjk,项目名称:web-blog,代码行数:28,代码来源:store.go


示例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
}
开发者ID:Kubuxu,项目名称:gx-lua,代码行数:35,代码来源:rewrite.go


示例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
}
开发者ID:moul,项目名称:radioman,代码行数:57,代码来源:radio.go


示例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())
	}
}
开发者ID:himanshugpt,项目名称:evergreen,代码行数:10,代码来源:example_test.go


示例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
}
开发者ID:pombredanne,项目名称:srclib-python,代码行数:16,代码来源:scan.go


示例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
}
开发者ID:hamfist,项目名称:deppy,代码行数:18,代码来源:rewrite.go


示例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
}
开发者ID:moul,项目名称:radioman,代码行数:45,代码来源:playlist.go


示例16: List

func (f *Finder) List(ignorePatterns []string) []string {
	fileList := []string{}

	for _, location := range f.locations {
		walker := fs.Walk(location)

		for walker.Step() {
			err := walker.Err()

			if err != nil || filenameMatchPatterns(walker.Path(), ignorePatterns) {
				continue
			}

			fileList = append(fileList, walker.Path())
		}
	}

	return fileList
}
开发者ID:pombredanne,项目名称:finder,代码行数:19,代码来源:finder.go


示例17: main

func main() {
	var g regexp.Grep
	g.AddFlags()
	g.Stdout = os.Stdout
	g.Stderr = os.Stderr
	flag.Usage = usage
	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		flag.Usage()
	}

	pat := "(?m)" + strings.Join(args, ".*")
	if *iflag {
		pat = "(?i)" + pat
	}
	re, err := regexp.Compile(pat)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	g.Regexp = re
	walker := fs.Walk(".")
	for walker.Step() {
		if walker.Stat().IsDir() {
			if strings.Contains(walker.Path(), ".git") {
				walker.SkipDir()
			}
			continue
		}
		g.File(walker.Path())
	}
	if err := walker.Err(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	if !g.Match {
		os.Exit(1)
	}
}
开发者ID:kaey,项目名称:codesearch,代码行数:41,代码来源:rgrep.go


示例18: visit

func visit(filename string) error {
	walker := fs.Walk(filename)
	for walker.Step() {
		if err := walker.Err(); err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		path := walker.Path()
		info := walker.Stat()
		if info.IsDir() {
			if isHidden(path) {
				walker.SkipDir()
			}
		}
		if info.Mode()&os.ModeSymlink == os.ModeSymlink {
			walker.SkipDir()
		}
		if info.Mode().IsRegular() {
			wg.Add(1)
			go parseFile(path)
		}
	}
	return nil
}
开发者ID:rubenk,项目名称:gospell,代码行数:24,代码来源:gospell.go


示例19: copySrc

func copySrc(dir string, deps []Dependency) error {
	// mapping to see if we visited a parent directory already
	visited := make(map[string]bool)
	ok := true
	for _, dep := range deps {
		debugln("copySrc for", dep.ImportPath)
		srcdir := filepath.Join(dep.ws, "src")
		rel, err := filepath.Rel(srcdir, dep.dir)
		debugln("srcdir", srcdir)
		debugln("rel", rel)
		debugln("err", err)
		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
		}

		// copy actual dependency
		vf := dep.vcs.listFiles(dep.dir)
		debugln("vf", vf)
		w := fs.Walk(dep.dir)
		for w.Step() {
			err = copyPkgFile(vf, dir, srcdir, w)
			if err != nil {
				log.Println(err)
				ok = false
			}
		}

		// Look for legal files in root
		//  some packages are imports as a sub-package but license info
		//  is at root:  exampleorg/common has license file in exampleorg
		//
		if dep.ImportPath == dep.root {
			// we are already at root
			continue
		}

		// prevent copying twice This could happen if we have
		//   two subpackages listed someorg/common and
		//   someorg/anotherpack which has their license in
		//   the parent dir of someorg
		rootdir := filepath.Join(srcdir, filepath.FromSlash(dep.root))
		if visited[rootdir] {
			continue
		}
		visited[rootdir] = true
		vf = dep.vcs.listFiles(rootdir)
		w = fs.Walk(rootdir)
		for w.Step() {
			fname := filepath.Base(w.Path())
			if IsLegalFile(fname) && !strings.Contains(w.Path(), sep) {
				err = copyPkgFile(vf, dir, srcdir, w)
				if err != nil {
					log.Println(err)
					ok = false
				}
			}
		}
	}

	if !ok {
		return errorCopyingSourceCode
	}

	return nil
}
开发者ID:Cepave,项目名称:lvs-metrics,代码行数:71,代码来源:save.go


示例20: CrawlROMs

// CrawlROMs crawls the rom directory and processes the files.
func CrawlROMs(gl *rom.GameListXML, sources []ds.DS, xmlOpts *rom.XMLOpts, gameOpts *rom.GameOpts) error {
	var ct http.RoundTripper = NewCancelTransport(http.DefaultTransport.(*http.Transport))
	http.DefaultClient.Transport = ct

	existing := make(map[string]struct{})

	if !dirExists(xmlOpts.RomDir) {
		log.Printf("ERR %s: does not exists", xmlOpts.RomDir)
		return nil
	}

	extraMap := make(map[string]struct{})
	if *extraExt != "" {
		extraSlice := strings.Split(*extraExt, ",")
		for _, e := range extraSlice {
			if e[0] != '.' {
				extraMap["."+e] = struct{}{}
			} else {
				extraMap[e] = struct{}{}
			}
		}
	}

	for _, x := range gl.GameList {
		switch {
		case *appendOut:
			p, err := filepath.Rel(xmlOpts.RomXMLDir, x.Path)
			if err != nil {
				log.Printf("Can't find original path: %s", x.Path)
			}
			f := filepath.Join(xmlOpts.RomDir, p)
			existing[f] = struct{}{}
		case *refreshOut:
			existing[x.Path] = struct{}{}
		}
	}

	var wg sync.WaitGroup
	results := make(chan *rom.GameXML, *workers)
	roms := make(chan *rom.ROM, 2**workers)
	for i := 0; i < *workers; i++ {
		wg.Add(1)
		go worker(sources, xmlOpts, gameOpts, results, roms, &wg)
	}
	go func() {
		defer wg.Done()
		for r := range results {
			if _, ok := existing[r.Path]; ok && *refreshOut {
				for i, g := range gl.GameList {
					if g.Path != r.Path {
						continue
					}
					copy(gl.GameList[i:], gl.GameList[i+1:])
					gl.GameList = gl.GameList[:len(gl.GameList)-1]
				}
			}
			gl.Append(r)
		}
	}()
	var stop bool
	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt)
	defer signal.Stop(sig)
	go func() {
		for {
			<-sig
			if !stop {
				stop = true
				log.Println("Stopping, ctrl-c again to stop now.")
				ct.(*CancelTransport).Stop()
				for _ = range roms {
				}
				continue
			}
			panic("AHHHH!")
		}
	}()
	bins := make(map[string]struct{})
	if !*mame {
		walker := fs.Walk(xmlOpts.RomDir)
		for walker.Step() {
			if stop {
				break
			}
			if err := walker.Err(); err != nil {
				return err
			}
			f := walker.Path()
			if b := filepath.Base(f); b != "." && strings.HasPrefix(b, ".") {
				walker.SkipDir()
				continue
			}
			r, err := rom.NewROM(f)
			if err != nil {
				log.Printf("ERR: Processing: %s, %s", f, err)
				continue
			}
			if !r.Cue {
				continue
//.........这里部分代码省略.........
开发者ID:toninodigiacomo,项目名称:scraper,代码行数:101,代码来源:scraper.go



注:本文中的github.com/kr/fs.Walk函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang fs.WalkFS函数代码示例发布时间:2022-05-23
下一篇:
Golang binarydist.Patch函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap