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

read_opts()

@type read_opts() :: [{:keys, :strings | :atoms | :atoms!}]

Options passed to read_file/2.

Functions

read_file(file, opts \\ [])

@spec read_file(Path.t(), read_opts()) :: {:ok, map()} | {:error, any()}

Read a configuration file located at a given path.

Options:

  • :keys - control how configuration keys are parsed:

read_file!(file, opts \\ [])

@spec read_file!(Path.t(), read_opts()) :: map() | no_return()

Read a configuration file, but raise an exception if the operation fails.

write_file(file, data)

@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_file!(file, data)

@spec write_file!(Path.t(), map()) :: :ok | no_return()

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.