View Source Tux.Config (Tux v0.4.0)
Basic support for reading and writing key = value
configuration files,
with support for ignoring comment lines (lines that start with #
or ;
).
Example
Say you have an existing configuration file file.conf:
# This is a comment line (will be ignored)
path = /tmp
# Note that all values will be parsed as strings
rounds = 10
; Windows-style comments are also ignored,
; and so are these [sections]
[couting]
count = 123
You can read it with read_file/2
:
with {:ok, config} <- Tux.Config.read_file("file.conf") do
assert config["path"] == "/tmp"
assert config["rounds"] == "10"
assert config["count"] == "123"
end
Notes
- All read values will be strings.
- All read valeus which are empty strings will be converted to nil.
Summary
Types
Options passed to read_file/2
.
Functions
Read a configuration file located at a given path.
Read a configuration file, but raise an exception if the operation fails.
Write configuration data to a file.
Write configuration data to a file, but raise an exception if the operation fails.
Types
@type read_opts() :: [{:keys, :strings | :atoms | :atoms!}]
Options passed to read_file/2
.
Functions
Read a configuration file located at a given path.
Options:
:keys
- control how configuration keys are parsed::strings
(default) - keys are converted to strings:atoms
- keys are converted to atoms usingString.to_atom/1
:atoms!
- keys are converted to atoms usingString.to_existing_atom/1
Read a configuration file, but raise an exception if the operation fails.
@spec write_file(Path.t(), map()) :: :ok | {:error, File.posix()}
Write configuration data to a file.
The file is created if it does not exist, and if it does exist, the previous content is overwritten.
Write configuration data to a file, but raise an exception if the operation fails.
The file is created if it does not exist, and if it does exist, the previous content is overwritten.