Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

pkger.Walk() walks additional similarly named directories #91

Open
andriy-bulynko opened this issue May 15, 2020 · 1 comment
Open

pkger.Walk() walks additional similarly named directories #91

andriy-bulynko opened this issue May 15, 2020 · 1 comment

Comments

@andriy-bulynko
Copy link

If you have a directory structure like this:

MacBook-Pro:pkger dlx$ tree root
root
├── public
│   ├── images
│   │   ├── img1.png
│   │   └── img2.png
│   └── index.html
└── public-foo
    ├── images
    │   ├── img1.png
    │   └── img2.png
    └── index.html

4 directories, 6 files
MacBook-Pro:pkger dlx$ 

And code like this:

package main

import (
	"fmt"
	"log"
	"os"
	"text/tabwriter"
	"time"

	"github.com/markbates/pkger"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	w := tabwriter.NewWriter(os.Stdout, 0, 0, 0, ' ', tabwriter.Debug)
	defer w.Flush()

	fmt.Println("walking /root/public...")
	if err := pkger.Walk("/root/public", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		fmt.Fprintf(w,
			"%s \t %s \t %d \t %s \t %s \t\n",
			path,
			info.Name(),
			info.Size(),
			info.Mode(),
			info.ModTime().Format(time.RFC3339),
		)
		return nil
	}); err != nil {
		return err
	}
	w.Flush()

	fmt.Println("walking /root/public-foo...")
	if err := pkger.Walk("/root/public-foo", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		fmt.Fprintf(w,
			"%s \t %s \t %d \t %s \t %s \t\n",
			path,
			info.Name(),
			info.Size(),
			info.Mode(),
			info.ModTime().Format(time.RFC3339),
		)
		return nil
	}); err != nil {
		return err
	}
	w.Flush()

	return nil
}

Then go run main.go prints:

walking /root/public...
app:/root/public                 | public     | 128   | drwxr-xr-x | 2020-03-09T17:47:03-04:00 |
app:/root/public/images          | images     | 128   | drwxr-xr-x | 2020-03-09T17:47:03-04:00 |
app:/root/public/images/img1.png | img1.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public/images/img2.png | img2.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public/index.html      | index.html | 257   | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
walking /root/public-foo...
app:/root/public-foo                 | public-foo | 128   | drwxr-xr-x | 2020-05-15T00:17:38-04:00 |
app:/root/public-foo/images          | images     | 128   | drwxr-xr-x | 2020-05-15T00:17:38-04:00 |
app:/root/public-foo/images/img1.png | img1.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public-foo/images/img2.png | img2.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public-foo/index.html      | index.html | 257   | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |

However, pkger; mv root root_hide; go build -v -o example; ./example prints:

walking /root/public...
app:/root/public                     | public     | 128   | drwxr-xr-x | 2020-03-09T17:47:03-04:00 |
app:/root/public-foo                 | public-foo | 128   | drwxr-xr-x | 2020-05-15T00:17:38-04:00 |
app:/root/public-foo/images          | images     | 128   | drwxr-xr-x | 2020-05-15T00:17:38-04:00 |
app:/root/public-foo/images/img1.png | img1.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public-foo/images/img2.png | img2.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public-foo/index.html      | index.html | 257   | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public/images              | images     | 128   | drwxr-xr-x | 2020-03-09T17:47:03-04:00 |
app:/root/public/images/img1.png     | img1.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public/images/img2.png     | img2.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public/index.html          | index.html | 257   | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
walking /root/public-foo...
app:/root/public-foo                 | public-foo | 128   | drwxr-xr-x | 2020-05-15T00:17:38-04:00 |
app:/root/public-foo/images          | images     | 128   | drwxr-xr-x | 2020-05-15T00:17:38-04:00 |
app:/root/public-foo/images/img1.png | img1.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public-foo/images/img2.png | img2.png   | 27718 | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
app:/root/public-foo/index.html      | index.html | 257   | -rw-r--r-- | 2020-03-09T17:47:03-04:00 |
MacBook-Pro:pkger dlx$ 
@andriy-bulynko
Copy link
Author

Just noticed that slightly different but similar problems occur in the same use-case with pkgingFile.Readdir(). Listing /root/public returns /root/public-foo as one of the sub-items.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant