Skip to content

Add context support #8

@prochac

Description

@prochac

Hi,
I'm working on a similar project, but for OneDrive.
The thing that kinda bothers me, is a lack of context.Context support.
I see you resort to context.Background().

The solution I settled on, without violating fs.Fs interface is:

// ContextFS is the interface implemented by a file system that is aware of context.
type ContextFS interface {
	fs.FS
	// Context returns a new FS with the given context. If the fs.FS implements
	// additional interfaces, such as [ReadFileFS], they must be implemented by the
	// returned FS.
	Context(context.Context) fs.FS
}

For work with multiple fs.ContextFS implementation, one can use this helper:

func FSWithContext[T ContextFS](ctx context.Context, fs T) T {
	return fs.Context(ctx).(T)
}

WDYT?

Then you can set context.Context before any fs.Fs use:

f, _ := fs.Context(ctx).Open("extractor_test/foo.json")
defer f.Close()
fileStat, err := fs.Stat(fileSystem.Context(ctx), filePath)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions