From a26dc51670cfde27438c23488baaad3da35656d9 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Thu, 27 May 2021 12:25:56 -0400 Subject: [PATCH 1/2] Support reading env vars from .env files --- README.rst | 9 +++++++++ setup.cfg | 3 ++- src/tinuous/__main__.py | 10 +++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index f7aca98..bb66c23 100644 --- a/README.rst +++ b/README.rst @@ -49,6 +49,12 @@ Global Options -c FILE, --config FILE Read configuration from the given file [default value: ``config.yml``] +-E FILE, --env FILE Load environment variables from the given + ``.env`` file. By default, environment + variables are loaded from the first file named + "``.env``" found by searching from the current + directory upwards. + -l LEVEL, --log-level LEVEL Set the log level to the given value. Possible values are "``CRITICAL``", "``ERROR``", "``WARNING``", "``INFO``", "``DEBUG``" (all @@ -326,6 +332,9 @@ Path templates may also contain custom placeholders defined in the top-level Authentication -------------- +Note that environment variables can be loaded from a ``.env`` file as an +alternative to setting them directly in the environment. + GitHub ~~~~~~ diff --git a/setup.cfg b/setup.cfg index 7e61747..22a5400 100644 --- a/setup.cfg +++ b/setup.cfg @@ -40,13 +40,14 @@ package_dir = =src python_requires = ~=3.8 install_requires = - click ~= 7.0 + click >= 7.0 click-loglevel ~= 0.2 datalad ~= 0.14 in_place ~= 0.4 pydantic ~= 1.7 PyGithub ~= 1.55 python-dateutil ~= 2.7 + python-dotenv ~= 0.11 PyYAML ~= 5.0 requests ~= 2.20 diff --git a/src/tinuous/__main__.py b/src/tinuous/__main__.py index c6991ad..367b7bd 100644 --- a/src/tinuous/__main__.py +++ b/src/tinuous/__main__.py @@ -22,6 +22,7 @@ from click_loglevel import LogLevel from datalad.api import Dataset from dateutil.parser import isoparse +from dotenv import load_dotenv from github import Github from github.Repository import Repository from github.Workflow import Workflow @@ -949,6 +950,12 @@ def _validate_since(cls, v: datetime) -> datetime: # noqa: B902, U100 help="Read configuration from the given file", show_default=True, ) +@click.option( + "-E", + "--env", + type=click.Path(exists=True, dir_okay=False), + help="Load environment variables from given .env file", +) @click.option( "-l", "--log-level", @@ -958,8 +965,9 @@ def _validate_since(cls, v: datetime) -> datetime: # noqa: B902, U100 show_default=True, ) @click.pass_context -def main(ctx: click.Context, config: str, log_level: int) -> None: +def main(ctx: click.Context, config: str, log_level: int, env: Optional[str]) -> None: """ Download build logs from GitHub Actions, Travis, and Appveyor """ + load_dotenv(env) logging.basicConfig( format="%(asctime)s [%(levelname)-8s] %(name)s %(message)s", datefmt="%Y-%m-%dT%H:%M:%S%z", From 8958fb76e2085923bddcdf6e1a9bb413aeb0a9f2 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Fri, 28 May 2021 08:18:35 -0400 Subject: [PATCH 2/2] Add a warning about .env in Git repos --- README.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.rst b/README.rst index bb66c23..fc4e13d 100644 --- a/README.rst +++ b/README.rst @@ -55,6 +55,13 @@ Global Options "``.env``" found by searching from the current directory upwards. + **Warning**: Care must be taken when this file + is located in a Git repository so as not to + publicly expose it: either list the file in + ``.gitignore`` or, if using Datalad or + git-annex, configure git-annex to prohibit + public sharing of the file. + -l LEVEL, --log-level LEVEL Set the log level to the given value. Possible values are "``CRITICAL``", "``ERROR``", "``WARNING``", "``INFO``", "``DEBUG``" (all