File system resource readiness assertion library for Go. This library extends the waitfor framework to provide file system checking capabilities, allowing you to wait for files and directories to exist before proceeding with your application logic.
- File existence checking: Wait for files to be created or become available
- Directory existence checking: Wait for directories to be created
- Context support: Full support for Go contexts with cancellation and timeouts
- Configurable retries: Use the waitfor framework's built-in retry mechanisms
- Thread-safe: Safe for concurrent use
go get github.com/go-waitfor/waitfor-fspackage main
import (
"context"
"fmt"
"os"
"github.com/go-waitfor/waitfor"
fs "github.com/go-waitfor/waitfor-fs"
)
func main() {
// Create a waitfor runner with file system support
runner := waitfor.New(fs.Use())
// Wait for files and directories to exist
err := runner.Test(
context.Background(),
[]string{"file://./my-file.txt", "file://./my-dir/"},
waitfor.WithAttempts(5),
)
if err != nil {
fmt.Printf("Files not ready: %v\n", err)
os.Exit(1)
}
fmt.Println("All files are ready!")
}The library uses the file:// URL scheme to specify file system resources:
- Files:
file:///absolute/path/to/file.txtorfile://./relative/path/file.txt - Directories:
file:///absolute/path/to/directory/orfile://./relative/directory/
package main
import (
"context"
"time"
"github.com/go-waitfor/waitfor"
fs "github.com/go-waitfor/waitfor-fs"
)
func main() {
runner := waitfor.New(fs.Use())
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Wait for a configuration file to be created
err := runner.Test(ctx, []string{"file://./config.json"})
if err != nil {
panic(err)
}
}package main
import (
"context"
"time"
"github.com/go-waitfor/waitfor"
fs "github.com/go-waitfor/waitfor-fs"
)
func main() {
runner := waitfor.New(fs.Use())
ctx := context.Background()
// Wait for multiple files with custom retry configuration
err := runner.Test(
ctx,
[]string{
"file://./data/input.csv",
"file://./data/schema.json",
"file://./logs/",
},
waitfor.WithAttempts(10),
waitfor.WithDelay(2*time.Second),
)
if err != nil {
panic(err)
}
}package main
import (
"context"
"net/url"
fs "github.com/go-waitfor/waitfor-fs"
)
func main() {
// Parse the file URL
u, err := url.Parse("file://./important-file.txt")
if err != nil {
panic(err)
}
// Create the file resource
resource, err := fs.New(u)
if err != nil {
panic(err)
}
// Test if the file exists
ctx := context.Background()
err = resource.Test(ctx)
if err != nil {
panic(err)
}
}Returns a resource configuration for the waitfor framework that enables file system checking with the file scheme.
Creates a new file system resource from a URL. The URL must use the file scheme.
Parameters:
u: A URL with thefilescheme pointing to a file or directory
Returns:
- A
waitfor.Resourcethat can be used to test file existence - An error if the URL is invalid or nil
Tests whether the file or directory exists. This method is called by the waitfor framework during resource checking.
Parameters:
ctx: Context for cancellation and timeout control
Returns:
nilif the file or directory exists- An error if the file doesn't exist, the context is cancelled, or there's a file system error
This library is part of the waitfor ecosystem, which provides a unified framework for waiting on various types of resources to become ready. Other available waitfor plugins include:
- HTTP endpoints
- Database connections
- Network services
- And more...
- Go 1.23 or later
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.