DotEnvy

0.3.0

DotEnv handling for Swift
juri/dotenvy

What's New

0.3.0

2024-05-18T13:09:35Z
  • Add command line tool with syntax check and JSON export
  • Add export method for exporting values to the process environment

DotEnvy

Swift build status

DotEnvy is a dotenv file parser for Swift. It allows you to load values from an .env file, similar to the libraries for node.js, Python, Ruby, Rust1 etc.

DotEnvy supports multiline strings and variable substitution.

Documentation

For more detailed syntax examples and API documentation visit DotEnvy's documentation on Swift Package Index.

Supported format

The dotenv format does not have a specification, but this library supports the common features. The syntax resembles Bash. Examples:

KEY=value
KEY2 = "quoted value"
 
 KEY3= unquoted value "with" quotes inside
# comment
KEY4 ='quoted value referring to ${KEY3}' # trailing comment
KEY5=unquoted value referring to ${KEY4}
KEY6="multiline
string"

Error reporting

DotEnvy has helpful error reporting on syntax errors.

let source = #"""
KEY="VALUE
"""#
do {
    _ = try DotEnvironment.parse(string: source)
} catch let error as ParseErrorWithLocation {
    let formatted = error.formatError(source: source)
    print(formatted)
}

outputs

   1: KEY="VALUE
                ^

Error on line 1: Unterminated quote

Command Line

There's also a command line tool, dotenv-tool. It supports checking dotenv files for syntax errors and converting them to JSON. To install, run:

swift build -c release
cp .build/release/dotenv-tool /usr/local/bin

Footnotes

  1. I accidentally used the same name they use; apologies!

Description

  • Swift Tools 5.10.0
View More Packages from this Author

Dependencies

Last updated: Thu Oct 17 2024 20:38:06 GMT-0900 (Hawaii-Aleutian Daylight Time)