datafs

package
v4.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnvFS = fsimpl.FSProviderFunc(NewEnvFS, "env")
View Source
var MergeFS = fsimpl.FSProviderFunc(NewMergeFS, "merge")
View Source
var StdinFS = fsimpl.FSProviderFunc(NewStdinFS, "stdin")
View Source
var WdFS = fsimpl.FSProviderFunc(
	func(u *url.URL) (fs.FS, error) {
		if !isSupportedPath(u.Path) {
			return nil, fmt.Errorf("unsupported path %q: %w", u.Path, fs.ErrInvalid)
		}

		vol, _, err := ResolveLocalPath(nil, u.Path)
		if err != nil {
			return nil, fmt.Errorf("resolve %q: %w", u.Path, err)
		}

		var fsys fs.FS
		if vol == "" || vol == "/" {
			fsys = osfs.NewFS()
		} else {
			var err error
			fsys, err = osfs.NewFS().SubVolume(vol)
			if err != nil {
				return nil, err
			}
		}

		return &wdFS{vol: vol, fsys: fsys}, nil
	},

	"file", "",
)

WdFS is a filesystem provider that creates local filesystems which support absolute paths beginning with '/', and interpret relative paths as relative to the current working directory (as reported by os.Getwd).

On Windows, certain types of paths are not supported, and will return an error. These are: - Drive Relative - e.g. C:foo\bar - Root Local - e.g. \\. or \\? - non-drive Local Devices - e.g. \\.\COM1, \\.\pipe\foo - NT Paths - e.g. \??\C:\foo\bar or \??\UNC\foo\bar

Functions

func ContextWithFSProvider

func ContextWithFSProvider(ctx context.Context, fsp fsimpl.FSProvider) context.Context

ContextWithFSProvider returns a context with the given FSProvider. Should only be used in tests.

func ContextWithStdin

func ContextWithStdin(ctx context.Context, r io.Reader) context.Context

ContextWithStdin injects an io.Reader into the context, which can be used to override the default stdin.

func DirInfo

func DirInfo(name string, modTime time.Time) fs.FileInfo

DirInfo creates a fs.FileInfo for a directory with the given name. Use FileInfo to set other values.

func ExpandEnvFsys

func ExpandEnvFsys(fsys fs.FS, s string) string

ExpandEnvFsys - a convenience function intended for internal use only!

func FSProviderFromContext

func FSProviderFromContext(ctx context.Context) fsimpl.FSProvider

FSProviderFromContext returns the FSProvider from the context, if any

func FSysForPath

func FSysForPath(ctx context.Context, path string) (fs.FS, error)

FSysForPath returns an io/fs.FS for the given path (which may be an URL), rooted at /. A fsimpl.FSProvider is required to be present in ctx, otherwise an error is returned.

func FileInfo

func FileInfo(name string, size int64, mode fs.FileMode, modTime time.Time, contentType string) fs.FileInfo

FileInfo creates a static fs.FileInfo with the given properties. The result is also a fs.DirEntry and can be safely cast.

func FileInfoDirEntry

func FileInfoDirEntry(fi fs.FileInfo) fs.DirEntry

FileInfoDirEntry adapts a fs.FileInfo into a fs.DirEntry. If it doesn't already implement fs.DirEntry, it will be wrapped to always return the same fs.FileInfo.

func GetenvFsys

func GetenvFsys(fsys fs.FS, key string, def ...string) string

GetenvFsys - a convenience function intended for internal use only!

func LookupEnvFsys added in v4.2.0

func LookupEnvFsys(fsys fs.FS, key string) (string, bool)

LookupEnvFsys - a convenience function intended for internal use only!

func NewEnvFS

func NewEnvFS(_ *url.URL) (fs.FS, error)

NewEnvFS returns a filesystem (an fs.FS) that can be used to read data from environment variables.

func NewMergeFS

func NewMergeFS(u *url.URL) (fs.FS, error)

NewMergeFS returns a new filesystem that merges the contents of multiple paths. Only a URL like "merge:" or "merge:///" makes sense here - the piped-separated lists of sub-sources to merge must be given to Open.

You can use WithDataSourceRegistryFS to provide the datasource registry, otherwise, an empty registry will be used.

An FSProvider will also be needed, which can be provided with a context using ContextWithFSProvider. Provide that context with fsimpl.WithContextFS.

func NewStdinFS

func NewStdinFS(_ *url.URL) (fs.FS, error)

NewStdinFS returns a filesystem (an fs.FS) that can be used to read data from standard input (os.Stdin).

func ResolveLocalPath

func ResolveLocalPath(fsys fs.FS, name string) (root, resolved string, err error)

ResolveLocalPath resolves a path on the given filesystem, relative to the current working directory, and returns both the root (/ or a volume name on Windows) and the resolved path. If the path is absolute (e.g. starts with a `/` or volume name on Windows), it is split and returned as-is. If fsys is nil, the current working directory is used. The output is suitable for use with io/fs functions.

func SplitFSMuxURL

func SplitFSMuxURL(in *url.URL) (*url.URL, string)

SplitFSMuxURL splits a URL into a filesystem URL and a relative file path

func StdinFromContext

func StdinFromContext(ctx context.Context) io.Reader

StdinFromContext returns the io.Reader that should be used for stdin as injected by ContextWithStdin. If no reader has been injected, os.Stdin is returned.

func WithDataSourceRegistryFS

func WithDataSourceRegistryFS(registry Registry, fsys fs.FS) fs.FS

WithDataSourceRegistryFS injects a datasource registry into the filesystem fs, if the filesystem supports it (i.e. has a WithDataSourceRegistry method). This is used for the mergefs filesystem.

func WrapWdFS

func WrapWdFS(fsys fs.FS) fs.FS

WrapWdFS is a filesystem wrapper that assumes non-absolute paths are relative to the current working directory (as reported by os.Getwd). It only works in a meaningful way when used with a local filesystem (e.g. os.DirFS or hackpadfs/os.FS).

func WrappedFSProvider

func WrappedFSProvider(fsys fs.FS, schemes ...string) fsimpl.FSProvider

WrappedFSProvider is an FSProvider that returns the given fs.FS

Types

type DataSourceReader

type DataSourceReader interface {
	// ReadSource reads the content of a datasource, given an alias and optional
	// arguments. If the datasource is not found, the alias is interpreted as a
	// URL. If the alias is not a valid URL, an error is returned.
	//
	// Returned content is cached, so subsequent calls with the same alias and
	// arguments will return the same content.
	ReadSource(ctx context.Context, alias string, args ...string) (string, []byte, error)

	// contains registry
	Registry
}

DataSourceReader reads content from a datasource

func NewSourceReader

func NewSourceReader(reg Registry) DataSourceReader

type Registry

type Registry interface {
	// Register a datasource
	Register(alias string, ds config.DataSource)
	// Lookup a registered datasource
	Lookup(alias string) (config.DataSource, bool)
	// List registered datasource aliases
	List() []string

	// Add extra headers not attached to a pre-defined datasource. These can be
	// used by datasources registered at runtime.
	AddExtraHeader(alias string, hdr http.Header)
}

Registry - a registry of datasources

func NewRegistry

func NewRegistry() Registry

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL