dotenv_1.0.0_a19c7797/src/dotenv.ads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
with Ada.Containers.Hashed_Maps;
with Ada.Strings.Hash;
with Ada.Strings.Unbounded;
with Ada.Text_IO;

--  @description
--  This package provide a set of sub-programs to parse and configure environment variable from a file.
--
--  Environment variables in file shall have the following format: Key=Value
--  The value can be single quoted or doubly quoted. If this is the case, they are removed in the result value.
--  Values that contains "\n" (in string format) are converted to Line Feed if it surronded by double quoted.
package Dotenv is
   use Ada.Strings.Unbounded, Ada.Text_IO;

   function Hash (Key : Unbounded_String) return Ada.Containers.Hash_Type is (Ada.Strings.Hash (To_String (Key)));

   function Equivalent_Key (Left, Right : Unbounded_String) return Boolean is (Left = Right);

   package Environment_Variable_Map is new Ada.Containers.Hashed_Maps (Key_Type        => Unbounded_String,
                                                                       Element_Type    => Unbounded_String,
                                                                       Hash            => Hash,
                                                                       Equivalent_Keys => Equivalent_Key);
   use Environment_Variable_Map;

   DEFAULT_DEBUG_VALUE     : constant Boolean := False;

   --  The parameter "wcem=8" indicates that we want to open a file in
   --  UTF-8 format (which can therefore contain accents)
   DEFAULT_FILE_FORM_VALUE : constant String  := "wcem=8";

   DEFAULT_FILENAME        : constant String  := ".env";

   DEFAULT_OVERWRITE_VALUE : constant Boolean := False;

   --  For example, if we have the following environment variables:
   --  - FIRSTNAME=John
   --  - LASTNAME=Doe
   --  - HELLO=Hello ${FIRSTNAME} $LASTNAME
   --  Then the final value of HELLO will be "Hello John Doe" if Interpolation is True
   DEFAULT_INTERPOLATE_VALUE : constant Boolean := False;

   DOTENV_CONFIG_DEBUG       : constant String  := "DOTENV_CONFIG_DEBUG";
   DOTENV_CONFIG_FILE_FORM   : constant String  := "DOTENV_CONFIG_FILE_FORM";
   DOTENV_CONFIG_INTERPOLATE : constant String  := "DOTENV_CONFIG_INTERPOLATE";
   DOTENV_CONFIG_OVERWRITE   : constant String  := "DOTENV_CONFIG_OVERWRITE";
   DOTENV_CONFIG_PATH        : constant String  := "DOTENV_CONFIG_PATH";

   --  Read a file to find environment variable.
   --  @param File File to read
   --  @param Debug True to show debug information, False otherwise. Default: DEFAULT_DEBUG_VALUE
   --  @return Return a map that contains in key, the environment name, and in value the environment value.
   function Parse (File : File_Type; Debug : Boolean := DEFAULT_DEBUG_VALUE) return Map;

   --  Read a file to find environment variable.
   --  @param Path File to read
   --  @param Debug True to show debug information, False otherwise. Default to: DEFAULT_DEBUG_VALUE
   --  @return Return a map that contains in key, the environment name, and in value the environment value.
   function Parse (Path : String; Debug : Boolean := DEFAULT_DEBUG_VALUE) return Map;

   --  Interpolate Value using elements provided by Env_Var and Environment Variable.
   --  For example, if we have the following environment variables:
   --  - FIRSTNAME=John
   --  - LASTNAME=Doe
   --  - HELLO=Hello ${FIRSTNAME} $LASTNAME
   --  Then the final value of HELLO will be: Hello John Doe
   --  @param Env_Var Contains element that can be used in interpolation
   --  @param Value Value to interpolate
   --  @param Overwrite True to overwrite existing environment variable, False otherwise.
   --    Default: DEFAULT_OVERWRITE_VALUE
   --    Example: if we have the following environment variable:
   --    - GREETING=Hello
   --    - FIRSTNAME=John
   --    Env_Var content:
   --    - FIRSTNAME=Jane
   --    - LASTNAME=Doe
   --    And the Value: $GREETING ${FIRSTNAME} $LASTNAME
   --    If Overwrite is True, then the return will be: Hello Jane Doe
   --    If Overwrite is False, then the return will be: Hello John Doe
   --  @return Retuns the interpolated value
   function Interpolate_Element (Env_Var : Map; Value : String; Overwrite : Boolean) return String;

   --  Interpolate all values of the given map
   --  @param Overwrite True to overwrite existing environment variable, False otherwise.
   --    Default: DEFAULT_OVERWRITE_VALUE
   --    Example: if we have the following environment variable:
   --    - GREETING=Hello
   --    - FIRSTNAME=John
   --    Env_Var content:
   --    - FIRSTNAME=Jane
   --    - LASTNAME=Doe
   --    And the Value: $GREETING ${FIRSTNAME} $LASTNAME
   --    If Overwrite is True, then the return will be: Hello Jane Doe
   --    If Overwrite is False, then the return will be: Hello John Doe
   procedure Interpolate (Overwrite : Boolean;
                          Debug     : Boolean;
                          Env_Var   : in out Map);

   --  Configure environment variables.
   --  @param Overwrite True to overwrite existing environment variable, False otherwise.
   --    Default: DEFAULT_OVERWRITE_VALUE
   --  @param Debug True to show debug information, False otherwise. Default: DEFAULT_DEBUG_VALUE
   --  @param Path path to the environment file. If no Path provided, this procedure try to solve the name in the
   --    following way:
   --    - If DOTENV_CONFIG_PATH environment variable is set, it use it
   --    - Find a '.env' file in the current working directory
   --    - Find a '.env' file in the execution directory
   --  @param File_Form Set Form to file, like encoding, etc. Default: DEFAULT_FILE_FORM_VALUE
   --  @exception Ada.Directories.Name_Error Throwed when no valid path found for environment variable file.
   procedure Config (Overwrite   : Boolean;
                     Debug       : Boolean;
                     Interpolate : Boolean;
                     Path        : String  := "";
                     File_Form   : String  := "");

   --  Configure environment variables.
   --  This procedure check if DOTENV_CONFIG_OVERWRITE is set, otherwise it uses DEFAULT_OVERWRITE_VALUE.
   --  If the value of DOTENV_CONFIG_OVERWRITE is invalid, it uses ;
   --  This procedure check if DOTENV_CONFIG_DEBUG is set, otherwise it uses DEFAULT_DEBUG_VALUE.
   --  If the value of DEFAULT_DEBUG_VALUE is invalid, it uses DEFAULT_DEBUG_VALUE;
   --  @param Path path to the environment file. If no Path provided, this procedure try to solve the name in the
   --    following way:
   --    - If DOTENV_CONFIG_PATH environment variable is set, it use it
   --    - Find a '.env' file in the current working directory
   --    - Find a '.env' file in the execution directory
   --  @param File_Form Set Form to file, like encoding, etc. Default: DEFAULT_FILE_FORM_VALUE
   --  @exception Ada.Directories.Name_Error Throwed when no valid path found for environment variable file.
   procedure Config (Path      : String  := "";
                     File_Form : String  := "");

   --  Configure environment variables.
   --  This procedure check if DOTENV_CONFIG_DEBUG is set, otherwise it uses DEFAULT_DEBUG_VALUE.
   --  If the value of DEFAULT_DEBUG_VALUE is invalid, it uses DEFAULT_DEBUG_VALUE;
   --  @param Overwrite True to overwrite existing environment variable, False otherwise.
   --    Default: DEFAULT_OVERWRITE_VALUE
   --  @param Path path to the environment file. If no Path provided, this procedure try to solve the name in the
   --    following way:
   --    - If DOTENV_CONFIG_PATH environment variable is set, it use it
   --    - Find a '.env' file in the current working directory
   --    - Find a '.env' file in the execution directory
   --  @param File_Form Set Form to file, like encoding, etc. Default: DEFAULT_FILE_FORM_VALUE
   --  @exception Ada.Directories.Name_Error Throwed when no valid path found for environment variable file.
   procedure Config_Overwrite (Overwrite : Boolean;
                               Path      : String  := "";
                               File_Form : String  := "");

   --  Configure environment variables.
   --  This procedure check if DOTENV_CONFIG_OVERWRITE is set, otherwise it uses DEFAULT_OVERWRITE_VALUE.
   --  If the value of DOTENV_CONFIG_OVERWRITE is invalid, it uses DEFAULT_OVERWRITE_VALUE;
   --  @param Debug True to show debug information, False otherwise. Default: DEFAULT_DEBUG_VALUE
   --  @param Path path to the environment file. If no Path provided, this procedure try to solve the name in the
   --    following way:
   --    - If DOTENV_CONFIG_PATH environment variable is set, it use it
   --    - Find a '.env' file in the current working directory
   --    - Find a '.env' file in the execution directory
   --  @param File_Form Set Form to file, like encoding, etc. Default: DEFAULT_FILE_FORM_VALUE
   --  @exception Ada.Directories.Name_Error Throwed when no valid path found for environment variable file.
   procedure Config_Debug (Debug     : Boolean;
                           Path      : String  := "";
                           File_Form : String  := "");

   --  Configure environment variables.
   --  This procedure check if DOTENV_CONFIG_OVERWRITE is set, otherwise it uses DEFAULT_OVERWRITE_VALUE.
   --  If the value of DOTENV_CONFIG_OVERWRITE is invalid, it uses DEFAULT_OVERWRITE_VALUE;
   --  @param Interpolate True to interpolate values, False otherwise. Default: DEFAULT_INTERPOLATE_VALUE
   --    For example, if we have the following environment variables:
   --    - FIRSTNAME=John
   --    - LASTNAME=Doe
   --    - HELLO=Hello ${FIRSTNAME} $LASTNAME
   --    Then the value of final HELLO will be: Hello John Doe
   --  @param Path path to the environment file. If no Path provided, this procedure try to solve the name in the
   --    following way:
   --    - If DOTENV_CONFIG_PATH environment variable is set, it use it
   --    - Find a '.env' file in the current working directory
   --    - Find a '.env' file in the execution directory
   --  @param File_Form Set Form to file, like encoding, etc. Default: DEFAULT_FILE_FORM_VALUE
   --  @exception Ada.Directories.Name_Error Throwed when no valid path found for environment variable file.
   procedure Config_Interpolate (Interpolate : Boolean;
                                 Path        : String  := "";
                                 File_Form   : String  := "");
end Dotenv;