Skip to content

Instantly share code, notes, and snippets.

@t0rr3sp3dr0
Last active April 5, 2023 08:03
Show Gist options
  • Save t0rr3sp3dr0/6a781b5d2c1657f843c3d2b958a29b86 to your computer and use it in GitHub Desktop.
Save t0rr3sp3dr0/6a781b5d2c1657f843c3d2b958a29b86 to your computer and use it in GitHub Desktop.
package main
import (
"io/fs"
"os"
"path"
)
var (
osFiles = []*os.File{os.Stdin, os.Stdout, os.Stderr}
virtFiles = make(map[string]fs.File, len(osFiles))
)
func init() {
for _, f := range osFiles {
virtFiles[f.Name()] = virtFile{f}
}
}
type virtFile struct{ fs.File }
func (_ virtFile) Close() error {
return nil
}
type virtFS struct{}
var _ fs.FS = (*virtFS)(nil)
func (_ virtFS) Open(name string) (fs.File, error) {
f, ok := virtFiles[name]
if !ok {
return nil, fs.ErrNotExist
}
return f, nil
}
var _ fs.GlobFS = (*virtFS)(nil)
func (_ virtFS) Glob(pattern string) ([]string, error) {
var matches []string
for name := range virtFiles {
matched, err := path.Match(pattern, name)
if err != nil {
return nil, err
}
if !matched {
continue
}
matches = append(matches, name)
}
return matches, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment