|
1 |
| -# dotenv for MATLAB |
2 |
| -> A MATLAB implementation of the dotenv pattern. |
3 |
| -
|
4 |
| -Storing configuration in the environment separate from code is based on [The Twelve-Factor App](https://12factor.net/config) methodology. dotenv for MATLAB® lets you store configuration data (passwords, API keys, server names, etc.) outside of your code. This lets you share your code without sharing your configuration data. |
5 |
| - |
6 |
| - |
7 |
| - |
8 |
| -[](https://circleci.com/gh/mathworks/dotenv-for-MATLAB) |
9 |
| - |
10 |
| -[](https://www.mathworks.com/matlabcentral/fileexchange/73988-dotenv-for-matlab) |
11 |
| -## Installation |
12 |
| -Put `dotenv.m` somewhere on your [search path](https://www.mathworks.com/help/matlab/ref/path.html). |
13 |
| - |
14 |
| -## Usage Example |
15 |
| -`dotenv()` will try and load a file named `.env` from the current working folder. Alternatively, you can specify the path with `dotenv('path/to/file.env')`. |
16 |
| - |
17 |
| -`.env` |
18 |
| -```text |
19 |
| -API_KEY=ybvxtzwaxa:r42DtRhuUT7TywYpzBABOFZ0IIomwuIEXnfFVq2VSXjRC |
20 |
| -``` |
21 |
| - |
22 |
| -`file.m` |
23 |
| -```matlab |
24 |
| -d = dotenv(); |
25 |
| -opts = weboptions('HeaderField', ["accept", "any"; "authorization", d.env.API_KEY]) |
26 |
| -url = "https://myurl.com" |
27 |
| -response = webread(url, opts); |
28 |
| -``` |
29 |
| -Common places you may need to do this are [`weboptions()`](https://www.mathworks.com/help/matlab/ref/weboptions.html), the [Database Toolbox](https://www.mathworks.com/help/database/ug/database.odbc.connection.html), or [`ftp()`](https://www.mathworks.com/help/matlab/ref/ftp.html). |
30 |
| - |
31 |
| -## Rules |
32 |
| -The parsing engine currently supports the following rules: |
33 |
| -* empty lines are skipped |
34 |
| -* lines beginning with `#` are treated as comments |
35 |
| -* empty values become empty strings (`DB_HOST=` becomes `DBHOST: ''`) |
36 |
| -* whitespace is removed from both ends of unquoted values (`DB_HOST=some server` becomes `DB_HOST:'some server'`) |
37 |
| -* quoted values are escaped (`DB_PASS=" some password "` becomes `DB_PASS:' some password '`) |
38 |
| - |
39 |
| -## FAQ |
40 |
| -### Should I commit my `.env` file? |
41 |
| -No. You should put `*.env` in your `.gitignore` file. |
42 |
| - |
43 |
| -## Development Setup |
44 |
| -Clone the repository. You can run `runtests('tests')` from the project root to run the unit test suite. |
45 |
| - |
46 |
| -## Bugs? |
47 |
| -I would love to hear if this breaks on any weird strings or doesn't work the way you expected. |
| 1 | +# dotenv for MATLAB |
| 2 | + |
| 3 | +> A MATLAB implementation of dotenv |
| 4 | +
|
| 5 | +Storing [configuration in the environment](https://12factor.net/config) is one of the tenets of a [12-factor app](https://12factor.net/). dotenv for MATLAB® lets you store configuration data (passwords, API keys, server names, etc.) outside of your code. This lets you share your code without sharing your configuration data. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +[](https://circleci.com/gh/mathworks/dotenv-for-MATLAB) |
| 10 | + |
| 11 | +[](https://www.mathworks.com/matlabcentral/fileexchange/73988-dotenv-for-matlab) |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Put `dotenv.m` somewhere on your [search path](https://www.mathworks.com/help/matlab/ref/path.html). |
| 16 | + |
| 17 | +## Usage Example |
| 18 | + |
| 19 | +`dotenv()` will try and load a file named `.env` from the current working folder. Alternatively, you can specify the path with `dotenv('path/to/file.env')`. |
| 20 | + |
| 21 | +`.env` |
| 22 | + |
| 23 | +```perl |
| 24 | +# Obligatory XKCD https://xkcd.com/936/ |
| 25 | +password=correct-horse-battery-staple |
| 26 | +``` |
| 27 | + |
| 28 | +`file.m` |
| 29 | + |
| 30 | +```matlab |
| 31 | +d = dotenv(); |
| 32 | +opts = weboptions('HeaderField', ["accept", "any"; "authorization", d.env.password]) |
| 33 | +url = "https://myurl.com" |
| 34 | +response = webread(url, opts); |
| 35 | +``` |
| 36 | + |
| 37 | +Common places you may need to do this are [`weboptions()`](https://www.mathworks.com/help/matlab/ref/weboptions.html), [working with remote data like S3 buckets](https://www.mathworks.com/help/matlab/import_export/work-with-remote-data.html), the [Database Toolbox](https://www.mathworks.com/help/database/ug/database.odbc.connection.html), or [`ftp()`](https://www.mathworks.com/help/matlab/ref/ftp.html). |
| 38 | + |
| 39 | +## Rules |
| 40 | + |
| 41 | +The parsing engine currently supports the following rules: |
| 42 | + |
| 43 | +* empty lines are skipped |
| 44 | +* lines beginning with `#` are treated as comments |
| 45 | +* empty values become empty strings (`DB_HOST=` becomes `DBHOST: ''`) |
| 46 | +* whitespace is removed from both ends of unquoted values (`DB_HOST=some server` becomes `DB_HOST:'some server'`) |
| 47 | +* quoted values are escaped (`DB_PASS=" some password "` becomes `DB_PASS:' some password '`) |
| 48 | + |
| 49 | +Examples are in the [config](config/) directory. |
| 50 | + |
| 51 | +## FAQ |
| 52 | + |
| 53 | +### Should I commit my `.env` file? |
| 54 | + |
| 55 | +No. You should put `*.env` in your `.gitignore` file. [MATLAB.gitignore](https://github.com/github/gitignore/blob/master/Global/MATLAB.gitignore) plus `*.env` is a good start. |
| 56 | + |
| 57 | +## Development Setup |
| 58 | + |
| 59 | +Clone the repository. You can run `runtests('tests')` from the project root to run the [unit test suite](tests/). |
| 60 | + |
| 61 | +## Bugs? |
| 62 | + |
| 63 | +I would love to hear if this breaks on any weird strings or doesn't work the way you expected. |
0 commit comments