func gotree(root string, printroot bool, spy memfs.FS, w io.Writer) (err error) { var ( r io.Reader pr, pw = io.Pipe() ch = make(chan error, 1) ndir int nfile int fn filepath.WalkFunc ) if dir { fn = countdirdelfile(&ndir, spy) } else { fn = countdirfile(&ndir, &nfile) } if err = spy.Walk(string(os.PathSeparator), fn); err != nil { return } go func() { ch <- nonnil(memfs.Unix.Encode(spy, pw), pw.Close()) }() switch { case dir && printroot: r = io.MultiReader( strings.NewReader(fmt.Sprintf("%s%c", root, os.PathSeparator)), pr, strings.NewReader(fmt.Sprintf("\n%d directories\n", ndir-1)), ) case dir: r = io.MultiReader( pr, strings.NewReader(fmt.Sprintf("\n%d directories\n", ndir-1)), ) case printroot: r = io.MultiReader( strings.NewReader(fmt.Sprintf("%s%c", root, os.PathSeparator)), pr, strings.NewReader(fmt.Sprintf("\n%d directories, %d files\n", ndir-1, nfile)), ) default: r = io.MultiReader( pr, strings.NewReader(fmt.Sprintf("\n%d directories, %d files\n", ndir-1, nfile)), ) } _, err = io.Copy(w, r) if e := <-ch; e != nil && err == nil { err = e } return }
func countdirdelfile(ndir *int, fs memfs.FS) filepath.WalkFunc { return func(s string, fi os.FileInfo, _ error) (err error) { if fi.IsDir() { *ndir++ } else { err = fs.Remove(s) } return } }