### Example JSON Configuration Source: https://github.com/gmtprime/skogsra/blob/master/README.md Example of a JSON file structure expected by the custom MyApp.Json binding. ```json { "MYAPP_PORT": 5000 } ``` -------------------------------- ### Configuration File Example for Multiple Environments Source: https://github.com/gmtprime/skogsra/blob/master/README.md Illustrates how to define configuration values for different environments (e.g., `Prod`, `Test`) within a `config.exs` file. ```elixir # file: config/config.exs use Mix.Config config :myapp, Prod port: 80 config :myapp, Test, port: 4001 config :myapp, port: 4000 ``` -------------------------------- ### YAML Configuration Example Source: https://github.com/gmtprime/skogsra/blob/master/README.md Example of a YAML configuration file structure for an Elixir application. This format is used to define application settings, including database credentials and connection details. ```yaml - app: "my_app" # Name of the application. module: "MyApp.Repo" # Optional module/namespace. config: # Actual configuration. database: "my_app_db" username: "postgres" password: "postgres" hostname: "localhost" port: 5432 ``` -------------------------------- ### JSON Configuration Example Source: https://github.com/gmtprime/skogsra/blob/master/README.md Example of a JSON configuration file structure for an Elixir application. This format is equivalent to the YAML configuration and is used for defining application settings. ```json [ { "app": "my_app", "module": "MyApp.Repo", "config": { "database": "my_app_db", "username": "postgres", "password": "postgres", "hostname": "localhost", "port": 5432 } } ] ``` -------------------------------- ### Install Skogsra with JSON Support Source: https://github.com/gmtprime/skogsra/blob/master/README.md To enable JSON configuration support, include both Skogsra and jason dependencies in your `mix.exs` file. ```elixir def deps do [ {:skogsra, "~> 2.5"}, {:jason, "~> 1.4"} ] end ``` -------------------------------- ### Example Usage of Custom Binding Source: https://github.com/gmtprime/skogsra/blob/master/README.md Demonstrates how to retrieve a value configured via the custom JSON binding. ```elixir iex> MyApp.Config.port() {:ok, 5000} ``` -------------------------------- ### Retrieve Environment-Specific Configuration Source: https://github.com/gmtprime/skogsra/blob/master/README.md Example of how to retrieve configuration values dynamically based on the application environment, using `MyApp.Config.env()` and `Myapp.Config.port(env)`. ```elixir ... with {:ok, env} <- MyApp.Config.env(), {:ok, port} <- Myapp.Config.port(env) do ... do something with the port ... end ... ``` -------------------------------- ### Preload and Validate Configuration Source: https://github.com/gmtprime/skogsra/blob/master/README.md This snippet shows how to preload and validate the application configuration. Calling `Myapp.Config.preload()` and `Myapp.Config.validate()` before application start ensures that all required configuration values are present, preventing application failure on environments like `prod` if `DATABASE_URL` is not set. ```elixir Myapp.Config.preload() Myapp.Config.validate() ``` -------------------------------- ### Install Skogsra with YAML Support Source: https://github.com/gmtprime/skogsra/blob/master/README.md To enable YAML configuration support, include both Skogsra and yamerl dependencies in your `mix.exs` file. ```elixir def deps do [ {:skogsra, "~> 2.5"}, {:yamerl, "~> 0.10"} ] end ``` -------------------------------- ### Load Production Environment Variables with Hab Source: https://github.com/gmtprime/skogsra/blob/master/README.md Use the `hab_load` command with the extension to load specific environment variable files. This example shows loading production variables. ```bash ~/my_project $ hab_load prod [SUCCESS] Loaded hab [/home/user/my_project/.envrc.prod] ``` -------------------------------- ### Skogsra Module for Handling Namespaced Environments Source: https://github.com/gmtprime/skogsra/blob/master/README.md Defines Skogsra variables, including one for the environment type (`:unsafe_module`) and another for the port with a default value. This setup allows for environment-specific configuration loading. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "Application environment" app_env :env, :myapp, :environment, type: :unsafe_module @envdoc "Application port" app_env :port, :myapp, :port, default: 4000 end ``` -------------------------------- ### Install Skogsra Dependency Source: https://github.com/gmtprime/skogsra/blob/master/README.md Add Skogsra as a dependency in your Elixir project's `mix.exs` file. Ensure Elixir version is 1.15+ and Erlang version is 22+. ```elixir def deps do [ {:skogsra, "~> 2.5"} ] end ``` -------------------------------- ### Preloading Skogsrå Configuration in Application Init Source: https://github.com/gmtprime/skogsra/blob/master/README.md This code demonstrates how to call the `preload/1` function from the Skogsrå configuration module within the `Application`'s `start/2` function to apply runtime configurations. ```elixir defmodule MyappWeb.Application do use Application def start(_type, _args) do MyappWeb.Config.preload(MyappWeb.Endpoint) children = [ MyappWeb.Endpoint ] ... end end ``` -------------------------------- ### Validate All Required Variables at Startup Source: https://github.com/gmtprime/skogsra/blob/master/README.md Use the `validate!` function to check if all required configuration variables are present during application startup. This will raise a `RuntimeError` if any are missing. ```elixir iex(1)> System.get_env("MYAPP_PORT") nil iex(2)> Application.get_env(:myapp, :port) nil iex(3)> MyApp.Config.validate!() ** (RuntimeError) Variable port in app myapp is undefined ``` -------------------------------- ### Simplified Configuration with Skogsrå Source: https://github.com/gmtprime/skogsra/blob/master/README.md This snippet shows how Skogsrå simplifies the configuration by removing direct OS environment variable access from the main config file. ```elixir use Mix.Config config :myapp_web, Myapp.Endpoint, http: [ transport_options: [socket_opts: [:inet6]] ] ``` -------------------------------- ### Verify Overridden Environment Variable Source: https://github.com/gmtprime/skogsra/blob/master/README.md Demonstrates how to check if the custom OS environment variable is being used and how the configuration function retrieves its value. ```elixir iex(1)> System.get_env("MYAPP_POSTGRES_HOSTNAME") "unreachable" iex(2)> System.get_env("PGHOST") "reachable" iex(3)> MyApp.Config.db_hostname() {:ok, "reachable"} ``` -------------------------------- ### Elixir Equivalent Configuration Source: https://github.com/gmtprime/skogsra/blob/master/README.md This Elixir configuration demonstrates how the YAML and JSON configurations are translated into Elixir's built-in configuration system. ```elixir config :my_app, MyApp.Repo, database: "my_App_db", username: "postgres" password: "postgres" hostname: "localhost" port: 5432 ``` -------------------------------- ### Custom JSON Binding Implementation Source: https://github.com/gmtprime/skogsra/blob/master/README.md Implement the Skogsra.Binding behavior to load configuration from JSON files. ```elixir defmodule MyApp.Json do use Skogsra.Binding alias Skogsra.Env @impl true def init(%Env{} = env) do options = Env.extra_options(env) case options[:config_path] do nil -> {:error, "JSON config path not specified"} path -> load(path) end end @impl true def get_env(%Env{} = env, config) when is_map(config) do name = Env.os_env(env) value = config[name] {:ok, value} end # Helpers # Loads JSON once and caches it in a :persistent_term using the path # as the key. @spec load(binary()) :: {:ok, map()} | {:error, term()} defp load(path) do with nil <- :persistent_term.get(path, nil), {:ok, contents} <- File.read(path), {:ok, config} <- Jason.decode(contents), :ok <- :persistent_term.put(path, config) do {:ok, config} else {:error, reason} -> {:error, "Cannot load #{path} due to #{inspect(reason)}"} config -> {:ok, config} end end end ``` -------------------------------- ### Define Environment-Specific Database Configurations Source: https://github.com/gmtprime/skogsra/blob/master/README.md This snippet demonstrates how to set environment-specific defaults and requirements for database connection attributes using `app_env` and `env_overrides`. It configures different settings for `dev`, `test`, and `prod` environments. ```elixir defmodule Myapp.Config do use Skogsra @envdoc "DB username" app_env :db_username, :myapp, [Myapp.Repo, :username], env_overrides: [ dev: [default: "postgres"], test: [default: "postgres"] ] @envdoc "DB password" app_env :db_password, :myapp, [Myapp.Repo, :password], env_overrides: [ dev: [default: "postgres"], test: [default: "postgres"] ] @envdoc "DB hostname" app_env :db_hostname, :myapp, [Myapp.Repo, :hostname], env_overrides: [ dev: [default: "localhost"], test: [default: "localhost"] ] @envdoc "DB port" app_env :db_port, :myapp, [Myapp.Repo, :port], env_overrides: [ dev: [default: 5432], test: [default: 5432] ] @envdoc "DB name" app_env :db_name, :myapp, [Myapp.Repo, :database], env_overrides: [ dev: [default: "myapp_dev"], test: [default: "myapp_test"] ] @envdoc "DB URL" app_env :db_url, :myapp, [Myapp.Repo, :url], os_env: "DATABASE_URL", env_overrides: [ prod: [required: true] ] end ``` -------------------------------- ### Define Configuration with Default Value Source: https://github.com/gmtprime/skogsra/blob/master/README.md Set a default value for a configuration parameter using the `default` option. This value is used if the variable is not found in the OS environment or configuration files. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "My port" app_env :my_port, :myapp, :port, default: 4000 end ``` -------------------------------- ### Skogsrå Configuration Module Source: https://github.com/gmtprime/skogsra/blob/master/README.md Defines environment variables for application port and secret key base using Skogsrå's `app_env` macro. It specifies the OS environment variable to read from and provides a default value. ```elixir defmodule MyappWeb.Config do use Skogsra @envdoc """ App port. """ app_env :port, :myapp, [:http, :port], os_env: "PORT", default: 4000 @envdoc """ Secret key base. """ app_env :secret_key_base, :myapp, :secret_key_base, os_env: "SECRET_KEY_BASE", default: "+/JwMGGtsTVOoX5gQrCMn8aHKfKDdUK8GeAKJ2fIUabUnmWTwg+zsCy4pAOmOdTs" end ``` -------------------------------- ### Generate Windows Environment File Source: https://github.com/gmtprime/skogsra/blob/master/README.md Generates a batch file (.bat) for Windows systems. This command creates a file named 'env.bat' with SET commands for environment variables. ```elixir iex(1)> Myapp.Config.template("env.bat", type: :windows) :ok ``` -------------------------------- ### Generate Unix Environment File Source: https://github.com/gmtprime/skogsra/blob/master/README.md Generates an environment file for Unix-like systems. This command creates a file named 'env' with export statements for environment variables. ```elixir iex(1)> Myapp.Config.template("env", type: :unix) :ok ``` -------------------------------- ### Standard Phoenix Configuration Source: https://github.com/gmtprime/skogsra/blob/master/README.md This is a typical configuration for a Phoenix application using Mix.Config, directly accessing OS environment variables for port and secret key base. ```elixir use Mix.Config secret_key_base = System.get_env("SECRET_KEY_BASE") || raise """ environment variable SECRET_KEY_BASE is missing. You can generate one by calling: mix phx.gen.secret """ config :myapp_web, MyappWeb.Endpoint, http: [ port: String.to_integer(System.get_env("PORT") || "4000"), transport_options: [socket_opts: [:inet6]] ], secret_key_base: secret_key_base ``` -------------------------------- ### Per AppEnv Skip System Binding Source: https://github.com/gmtprime/skogsra/blob/master/README.md Configure a specific application environment variable to skip the system binding. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "My port" app_env :my_port, :myapp, :port, binding_skip: [:system] end ``` -------------------------------- ### Handle Undefined Required Variable Source: https://github.com/gmtprime/skogsra/blob/master/README.md Shows the error returned by Skogsra when a required configuration variable is not found in the system. ```elixir iex(1)> System.get_env("MYAPP_PORT") nil iex(2)> Application.get_env(:myapp, :port) nil iex(3)> MyApp.Config.my_port() {:error, "Variable port in app myapp is undefined"} ``` -------------------------------- ### Generate Elixir Release Environment File Source: https://github.com/gmtprime/skogsra/blob/master/README.md Generates an environment file for Elixir releases. This command creates a file named 'env' with the necessary variable definitions. ```elixir iex(1)> Myapp.Config.template("env") :ok ``` -------------------------------- ### Globally Skip System Environment Variables Source: https://github.com/gmtprime/skogsra/blob/master/README.md Configure Skogsra to skip reading from OS environment variables globally. ```elixir config :skogsra, binding_skip: [:system] ``` -------------------------------- ### Casting System Environment Variable to Custom List Type Source: https://github.com/gmtprime/skogsra/blob/master/README.md Demonstrates casting a system environment variable to a custom list of integers using Skogsra and the `MyList` type. The value is retrieved from the system environment and then accessed via the config function. ```elixir iex(1)> System.get_env("MYAPP_INTEGERS") "1, 2, 3" iex(2)> MyApp.Config.my_integers() {:ok, [1, 2, 3]} ``` -------------------------------- ### Integrating JSON Config Provider in Elixir Release Source: https://github.com/gmtprime/skogsra/blob/master/README.md Add this to your Elixir release configuration to enable the JSON configuration provider. Specify the path to your JSON configuration file. ```elixir config_providers: [{Skogsra.Provider.Json, ["/path/to/config/file.json"]}] ``` -------------------------------- ### Define Application Environment Variable Source: https://github.com/gmtprime/skogsra/blob/master/README.md Define an application environment variable with default values and environment-specific overrides. This snippet shows how to set up a hostname configuration. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "My hostname" app_env :my_hostname, :myapp, :hostname, default: "localhost", env_overrides: [ prod: [default: "example.com"] ] end ``` -------------------------------- ### Integrating YAML Config Provider in Elixir Release Source: https://github.com/gmtprime/skogsra/blob/master/README.md Add this to your Elixir release configuration to enable the YAML configuration provider. Specify the path to your YAML configuration file. ```elixir config_providers: [{Skogsra.Provider.Yaml, ["/path/to/config/file.yml"]}] ``` -------------------------------- ### Generated Elixir Release Environment File Content Source: https://github.com/gmtprime/skogsra/blob/master/README.md The content of the 'env' file generated for Elixir releases, defining the MYAPP_PORT environment variable. ```bash # DOCS My port # TYPE integer MYAPP_PORT="Elixir.Application" ``` -------------------------------- ### Casting System Environment Variable to Positive Integer Source: https://github.com/gmtprime/skogsra/blob/master/README.md Demonstrates casting a system environment variable to a positive integer using Skogsra. The value is retrieved and then accessed via the application's config function. ```elixir iex(1)> System.get_env("MYAPP_PORT") "42" iex(2)> MyApp.Config.my_port() {:ok, 42} ``` -------------------------------- ### Per AppEnv Change Binding Order Source: https://github.com/gmtprime/skogsra/blob/master/README.md Customize the binding order for a specific application environment variable. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "My port" app_env :my_port, :myapp, :port, binding_order: [:config, :system] end ``` -------------------------------- ### Generated Unix Environment File Content Source: https://github.com/gmtprime/skogsra/blob/master/README.md The content of the 'env' file generated for Unix systems, defining the MYAPP_PORT environment variable using export. ```bash # DOCS My port # TYPE integer export MYAPP_PORT='Elixir.Application' ``` -------------------------------- ### AppEnv Declaration with Custom JSON Binding Source: https://github.com/gmtprime/skogsra/blob/master/README.md Declare an application environment variable to use a custom JSON binding, specifying the path to the JSON configuration file and a default value. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "My port" app_env :my_port, :myapp, :port, binding_order: [:system, :config, MyApp.Json], config_path: "#{Path.cwd!()}/priv/config.json", default: 4000 end ``` -------------------------------- ### Generated Windows Environment File Content Source: https://github.com/gmtprime/skogsra/blob/master/README.md The content of the 'env.bat' file generated for Windows systems, defining the MYAPP_PORT environment variable using SET. ```bat :: DOCS My port :: TYPE integer SET MYAPP_PORT="Elixir.Application" ``` -------------------------------- ### Define a Required Configuration Variable Source: https://github.com/gmtprime/skogsra/blob/master/README.md Mark a configuration variable as `required: true`. Skogsra will return an error if this variable is not defined in the OS environment or application configuration. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "My port" app_env :my_port, :myapp, :port, required: true end ``` -------------------------------- ### Globally Change Variable Binding Order Source: https://github.com/gmtprime/skogsra/blob/master/README.md Modify the default binding order globally to prioritize configuration over system environment variables. ```elixir config :skogsra, binding_order: [:config, :system] ``` -------------------------------- ### Casting Environment Variable to Atom Source: https://github.com/gmtprime/skogsra/blob/master/README.md Demonstrates how Skogsrå casts the system environment variable `MYAPP_ENVIRONMENT` to an atom (`:staging`) when the default type is `:atom`. ```elixir iex(1)> System.get_env("MYAPP_ENVIRONMENT") "staging" iex(2)> MyApp.Config.my_environment() {:ok, :staging} ``` -------------------------------- ### Define App Environment Variable with Custom List Type Source: https://github.com/gmtprime/skogsra/blob/master/README.md This snippet defines an application environment variable that uses the custom `MyList` type. Skogsra will use the `MyList.cast/1` function to process the input value. ```elixir defmodule MyApp.Config do use Skogsra app_env :my_integers, :myapp, :integers, type: MyList end ``` -------------------------------- ### Casting Application Environment Variable to Custom List Type Source: https://github.com/gmtprime/skogsra/blob/master/README.md Shows how Skogsra casts an application environment variable to a custom list of integers using the `MyList` type. The value is retrieved from the application environment and then accessed via the config function. ```elixir iex(1)> Application.get_env(:myapp, :integers) [1, 2, 3] iex(2)> MyApp.Config.my_integers() {:ok, [1, 2, 3]} ``` -------------------------------- ### Define App Environment Variable with Positive Integer Type Source: https://github.com/gmtprime/skogsra/blob/master/README.md This snippet shows how to define an application environment variable with a type explicitly set to `:pos_integer`. Skogsra will attempt to cast values to a positive integer. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "Port" app_env :my_port, :myapp, :port, default: 4000, type: :pos_integer end ``` -------------------------------- ### Casting Application Environment Variable to Positive Integer Source: https://github.com/gmtprime/skogsra/blob/master/README.md Shows how Skogsra casts an application environment variable to a positive integer. The value is retrieved directly from the application environment and then accessed via the config function. ```elixir iex(1)> Application.get_env(:myapp, :port) 42 iex(2)> MyApp.Config.my_port() {:ok, 42} ``` -------------------------------- ### Casting Application Environment Variable to Atom Source: https://github.com/gmtprime/skogsra/blob/master/README.md Shows Skogsrå casting an application environment variable `:myapp, :environment` to an atom (`:staging`) when the default type is `:atom`. ```elixir iex(1)> Application.get_env(:myapp, :environment) "staging" iex(2)> MyApp.Config.my_environment() {:ok, :staging} ``` -------------------------------- ### Disable Automatic Documentation Generation in Skogsra Source: https://github.com/gmtprime/skogsra/blob/master/README.md Configure Skogsra globally to disable the automatic generation of documentation for configuration variables by setting `generate_docs: false` in your `config/config.exs`. ```elixir config :skogsra, generate_docs: false ``` -------------------------------- ### Implement Custom Type for List of Integers Source: https://github.com/gmtprime/skogsra/blob/master/README.md This snippet implements the `Skogsra.Type` behavior to cast a comma-separated string into a list of integers. It handles both binary and list inputs. ```elixir defmodule MyList do use Skogsra.Type @impl Skogsra.Type def cast(value) def cast(value) when is_binary(value) do list = value |> String.split(~r/,/) |> Stream.map(&String.trim/1) |> Enum.map(&String.to_integer/1) {:ok, list} end def cast(value) when is_list(value) do if Enum.all?(value, &is_integer/1), do: {:ok, value}, else: :error end def cast(_) do :error end end ``` -------------------------------- ### Disable Caching for Skogsra Variables Source: https://github.com/gmtprime/skogsra/blob/master/README.md Set `cached: false` in your `app_env` definition to prevent Skogsra from caching variable values. This can improve write performance at the cost of slower reads. ```elixir defmodule MyApp.Config do use Skogsra app_env :value, :myapp, :value, cached: false end ``` -------------------------------- ### Override OS Environment Variable Name Source: https://github.com/gmtprime/skogsra/blob/master/README.md Use the `os_env` option to specify a custom OS environment variable name for a configuration parameter. This overrides the default generated name. ```elixir defmodule MyApp.Config do use Skogsra app_env :db_hostname, :myapp, [:postgres, :hostname], os_env: "PGHOST" end ``` -------------------------------- ### Elixir Function Specs for Integer Type Source: https://github.com/gmtprime/skogsra/blob/master/README.md These specs are generated when the type is set to :integer, either explicitly or implicitly via a default value. They define the return types for functions handling integer configurations. ```elixir @spec my_port() :: {:ok, integer()} | {:error, binary()} @spec my_port(Skogsra.Env.namespace()) :: {:ok, integer()} | {:error, binary()} ``` -------------------------------- ### Elixir Type Specification for Optional Integer Variable Source: https://github.com/gmtprime/skogsra/blob/master/CHANGELOG.md Defines the type specification for a variable that can be nil or an integer, or return an error. ```elixir @spec my_var() :: {:ok, nil | integer()} | {:error, binary()} ``` -------------------------------- ### Define Environment Variable with Default Atom Type Source: https://github.com/gmtprime/skogsra/blob/master/README.md This snippet defines an environment variable `:my_environment` with a default value of `:prod`. Skogsrå will automatically cast environment variable inputs to the `:atom` type. ```elixir defmodule MyApp.Config do use Skogsra @envdoc "My environment" app_env :my_environment, :myapp, :environment, default: :prod end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.