Structure
DirectoryFilesProvider
A configuration provider that reads values from individual files in a directory.
- iOS 18.0+
- macOS 15.0+
- tvOS 18.0+
- visionOS 2.0+
- watchOS 11.0+
struct DirectoryFilesProvider
Mentioned In
Overview
This provider reads configuration values from a directory where each file represents a single configuration key-value pair. The file name becomes the configuration key, and the file contents become the value. This approach is commonly used by secret management systems that mount secrets as individual files.
Key mapping
This provider transforms configuration keys into file names using these rules:
For example:
Value handling
The provider reads file contents as UTF-8 strings and converts them to the requested type. For binary data (bytes type), it reads raw file contents directly without string conversion. The provider always trims leading and trailing whitespace from string values.
Supported data types
The provider supports all standard configuration types:
Strings (UTF-8 text files)
Integers, doubles, and booleans (parsed from string contents)
Arrays (using configurable separator, comma by default)
Byte arrays (raw file contents)
Secret handling
By default, all values are marked as secrets for security. This is appropriate since this provider is typically used for sensitive data mounted by secret management systems.
Usage
Reading from a secrets directory
// Assuming /run/secrets contains files:
// - database-password (contains: "secretpass123")
// - max-connections (contains: "100")
// - enable-cache (contains: "true")
let provider = try await DirectoryFilesProvider(
directoryPath: "/run/secrets"
)
let config = ConfigReader(provider: provider)
let dbPassword = config.string(forKey: "database.password") // "secretpass123"
let maxConn = config.int(forKey: "max.connections", default: 50) // 100
let cacheEnabled = config.bool(forKey: "enable.cache", default: false) // true
Reading binary data
// For binary files like certificates or keys
let provider = try await DirectoryFilesProvider(
directoryPath: "/run/secrets"
)
let config = ConfigReader(provider: provider)
let certData = try config.requiredBytes(forKey: "tls.cert") // Raw file bytes
Custom array handling
// If files contain comma-separated lists
let provider = try await DirectoryFilesProvider(
directoryPath: "/etc/config"
)
// File "allowed-hosts" contains: "host1.example.com,host2.example.com,host3.example.com"
let hosts = config.stringArray(forKey: "allowed.hosts", default: [])
// ["host1.example.com", "host2.example.com", "host3.example.com"]
Configuration context
This provider ignores the context passed in context. All keys are resolved using only their component path.
Topics
Creating a directory files provider
Relationships
Conforms To