Deployment and lifecycle management for Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

agent: clean up config module

+12 -18
+12 -18
apps/sower_agent/lib/sower_agent/config.ex
··· 77 77 Logger.error(msg: "Failed to read configuration", errors: errors) 78 78 Kernel.exit(1) 79 79 end 80 - 81 - # process side effects 82 - cfg = 83 - cfg 80 + # pull apart and put back together 84 81 |> Map.to_list() 85 - |> Enum.map(&external_config/1) 86 - |> Enum.reject(&is_nil/1) 82 + # process side effects 83 + |> Enum.reduce([], fn config, acc -> process_side_effects(config, acc) end) 87 84 |> Map.new() 88 - 89 - # cast back into a struct 90 - cfg = struct(__MODULE__, cfg) 85 + |> then(&struct(__MODULE__, &1)) 91 86 92 87 Application.put_env(@app, :config, cfg) 93 88 end ··· 96 91 external_config is processed for each child in the config. 97 92 It allows for mapping from a config file format to elixir native config manually. 98 93 """ 99 - def external_config({:endpoint, endpoint}) do 94 + def process_side_effects({:endpoint, endpoint}, acc) do 100 95 uri = URI.parse(endpoint) 101 96 102 97 uri = ··· 119 114 Application.put_env(SowerAgent.Client, :uri, uri) 120 115 Application.put_env(SowerAgent.Client, :reconnect_after_msec, [200, 500, 1_000, 2_000]) 121 116 122 - nil 117 + acc 123 118 end 124 119 125 - def external_config({:state_directory, dir}) do 126 - {:state_directory, Path.expand(dir)} 120 + def process_side_effects({:state_directory, dir}, acc) do 121 + Keyword.put(acc, :state_directory, Path.expand(dir)) 127 122 end 128 123 129 - def external_config({:subscriptions, subscriptions}) 124 + def process_side_effects({:subscriptions, subscriptions}, acc) 130 125 when is_list(subscriptions) do 131 126 normalized_subscriptions = 132 127 Enum.map(subscriptions, fn subscription -> ··· 146 141 end 147 142 end) 148 143 149 - {:subscriptions, normalized_subscriptions} 144 + Keyword.put(acc, :subscriptions, normalized_subscriptions) 150 145 end 151 146 152 - def external_config({:__struct__, _}), do: nil 153 - 154 - def external_config(cfg), do: cfg 147 + def process_side_effects({:__struct__, _}, acc), do: acc 148 + def process_side_effects({key, val}, acc), do: Keyword.put(acc, key, val) 155 149 156 150 def add_config_file(cfg) do 157 151 cfg