diff options
author | Fred Hebert <mononcqc@ferd.ca> | 2019-05-27 15:28:28 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-27 15:28:28 -0400 |
commit | 6c677cddbf76eb7bea41405a19f9bcca50f34a57 (patch) | |
tree | 9fdf70680bc2716c9f99f35eb3b469951b4a9d2c /src | |
parent | 9ae20ec782fdfd1fe2504c167552aa240394694a (diff) | |
parent | ca22249d7c0920ff4a8cc916e4a8c8a2275b52b6 (diff) |
Merge pull request #2090 from tsloughter/env-file
add support for reading in environment variables from a file
Diffstat (limited to 'src')
-rw-r--r-- | src/rebar_prv_shell.erl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/rebar_prv_shell.erl b/src/rebar_prv_shell.erl index dd3e6f6..406b90a 100644 --- a/src/rebar_prv_shell.erl +++ b/src/rebar_prv_shell.erl @@ -88,6 +88,9 @@ init(State) -> {start_clean, undefined, "start-clean", boolean, "Cancel any applications in the 'apps' list " "or release."}, + {env_file, undefined, "env-file", string, + "Path to file of os environment variables to setup " + "before expanding vars in config files."}, {user_drv_args, undefined, "user_drv_args", string, "Arguments passed to user_drv start function for " "creating custom shells."}]} @@ -309,6 +312,7 @@ run_script_file(File) -> Result. maybe_boot_apps(State) -> + _ = maybe_set_env_vars(State), case find_apps_to_boot(State) of undefined -> %% try to read in sys.config file @@ -577,6 +581,45 @@ consult_env_config(State, Filename) -> rebar_file_utils:consult_config_terms(State, Terms) end. +maybe_set_env_vars(State) -> + EnvFile =debug_get_value(env_file, rebar_state:get(State, shell, []), undefined, + "Found env_file from config."), + {Opts, _} = rebar_state:command_parsed_args(State), + EnvFile1 = debug_get_value(env_file, Opts, EnvFile, + "Found env_file from command line option."), + + case maybe_read_file(EnvFile1) of + ignore -> + ok; + {error, _} -> + ?WARN("Failed to read file with environment variables: ~p", [EnvFile1]); + {ok, Bin} -> + Lines = string:split(unicode:characters_to_list(Bin), "\n", all), + [handle_env_var_line(Line) || Line <- Lines] + end. + +handle_env_var_line(Line) -> + Trimmed = rebar_string:trim(Line, both, [$\s]), + %% ignore lines starting with # and + %% fail if there are spaces around = + case re:run(Trimmed, "^(?<key>[^#][^\s=]*)=(?<value>[^\s]\.*)", + [{capture, [key, value], list}, unicode]) of + {match, [Key, Value]} -> + os:putenv(Key, Value); + _ -> + case Trimmed of + [$# | _] -> ignore; + [] -> ignore; + Other -> + ?WARN("Unable to parse environment variable from this line: ~ts", [Other]) + end + end. + +maybe_read_file(undefined) -> + ignore; +maybe_read_file(EnvFile) -> + file:read_file(EnvFile). + %% @doc quick and simple variable substitution writeup. %% Supports `${varname}' but not `$varname' nor nested %% values such as `${my_${varname}}'. |