Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fs.FS #28

Closed
thomasf opened this issue Feb 16, 2021 · 2 comments
Closed

Add support for fs.FS #28

thomasf opened this issue Feb 16, 2021 · 2 comments

Comments

@thomasf
Copy link

thomasf commented Feb 16, 2021

Since go1.16 is released now I just added this to a project to integrate with embed.FS.

It would probably be handy to have built in and io.fs probably won't be going anywhere soon.

Something like this maybe with an exported constructor function. Maybe with build in support to specify a subdirectory NewIOFSMigratorFS(fs.FS) would make it clear which migratorfs it is but that name is kind of a mouthful.

type iofsMigratorFS struct{ fsys fs.FS }

func (m iofsMigratorFS) ReadDir(dirname string) ([]fs.FileInfo, error) {
	de, err := fs.ReadDir(m.fsys, dirname)
	if err != nil {
		return nil, err
	}
	var res []os.FileInfo
	for _, v := range de {
		fi, err := v.Info()
		if err != nil {
			return nil, err
		}
		res = append(res, fi)
	}
	return res, nil
}

func (m iofsMigratorFS) ReadFile(filename string) ([]byte, error) {
	return fs.ReadFile(m.fsys, filename)
}

func (m iofsMigratorFS) Glob(pattern string) (matches []string, err error) {
	return fs.Glob(m.fsys, pattern)
}
@thomasf thomasf closed this as completed Feb 17, 2021
@gnuletik
Copy link

Hi @thomasf, did you manage to use embed.FS ?

@thomasf
Copy link
Author

thomasf commented Apr 13, 2022

Yes, the code pasted above works. I created a pull request as well but it broke backwards compatibility with tools ( #29 (comment) ) so I didn't want to have it merged. It would probably be ok to merge it now though IIRC, it should work as far back as some of the later Go 1.15 point releases now and 1.15 is already an unsupported version since a while now.

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

No branches or pull requests

2 participants