Merge pull request #13507 from lafirest/feat/env_func
feat(variform): add a builtin function to get env vars
This commit is contained in:
commit
1925ed2f55
|
@ -252,6 +252,9 @@
|
|||
timezone_to_offset_seconds/1
|
||||
]).
|
||||
|
||||
%% System functions
|
||||
-export([getenv/1]).
|
||||
|
||||
%% See extra_functions_module/0 and set_extra_functions_module/1 in the
|
||||
%% emqx_rule_engine module
|
||||
-callback handle_rule_function(atom(), list()) -> any() | {error, no_match_for_function}.
|
||||
|
@ -1262,3 +1265,9 @@ convert_timestamp(MillisecondsTimestamp) ->
|
|||
|
||||
uuid_str(UUID, DisplayOpt) ->
|
||||
uuid:uuid_to_string(UUID, DisplayOpt).
|
||||
|
||||
%%------------------------------------------------------------------------------
|
||||
%% System Funcs
|
||||
%%------------------------------------------------------------------------------
|
||||
getenv(Env) ->
|
||||
emqx_variform_bif:getenv(Env).
|
||||
|
|
|
@ -79,6 +79,12 @@
|
|||
%% Number compare functions
|
||||
-export([num_comp/2, num_eq/2, num_lt/2, num_lte/2, num_gt/2, num_gte/2]).
|
||||
|
||||
%% System
|
||||
-export([getenv/1]).
|
||||
|
||||
-define(CACHE(Key), {?MODULE, Key}).
|
||||
-define(ENV_CACHE(Env), ?CACHE({env, Env})).
|
||||
|
||||
%%------------------------------------------------------------------------------
|
||||
%% String Funcs
|
||||
%%------------------------------------------------------------------------------
|
||||
|
@ -569,3 +575,24 @@ num_lte(A, B) ->
|
|||
num_gte(A, B) ->
|
||||
R = num_comp(A, B),
|
||||
R =:= gt orelse R =:= eq.
|
||||
|
||||
%%------------------------------------------------------------------------------
|
||||
%% System
|
||||
%%------------------------------------------------------------------------------
|
||||
getenv(Bin) when is_binary(Bin) ->
|
||||
EnvKey = ?ENV_CACHE(Bin),
|
||||
case persistent_term:get(EnvKey, undefined) of
|
||||
undefined ->
|
||||
Name = erlang:binary_to_list(Bin),
|
||||
Result =
|
||||
case os:getenv(Name) of
|
||||
false ->
|
||||
<<>>;
|
||||
Value ->
|
||||
erlang:list_to_binary(Value)
|
||||
end,
|
||||
persistent_term:put(EnvKey, Result),
|
||||
Result;
|
||||
Result ->
|
||||
Result
|
||||
end.
|
||||
|
|
|
@ -72,3 +72,10 @@ base64_encode_decode_test() ->
|
|||
RandBytes = crypto:strong_rand_bytes(100),
|
||||
Encoded = emqx_variform_bif:base64_encode(RandBytes),
|
||||
?assertEqual(RandBytes, emqx_variform_bif:base64_decode(Encoded)).
|
||||
|
||||
system_test() ->
|
||||
EnvName = erlang:atom_to_list(?MODULE),
|
||||
EnvVal = erlang:atom_to_list(?FUNCTION_NAME),
|
||||
EnvNameBin = erlang:list_to_binary(EnvName),
|
||||
os:putenv(EnvName, EnvVal),
|
||||
?assertEqual(erlang:list_to_binary(EnvVal), emqx_variform_bif:getenv(EnvNameBin)).
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Added a new builtin function `getenv` in the rule engine and variform expression to access the environment variables.
|
||||
Note this value is immutable once loaded from the environment.
|
Loading…
Reference in New Issue