From 8e625b29ad5ad5cf852178793fad1d8538fe2304 Mon Sep 17 00:00:00 2001 From: Lev Gorodetskiy Date: Thu, 28 Sep 2023 21:28:28 -0300 Subject: [PATCH] `--unsafe` and `--compose` flags to `config env` command --- CHANGELOG.md | 6 ++++++ src/dipdup/cli.py | 24 +++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad293695f..6a8684a8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning]. +## [Unreleased] + +### Added + +- cli: Added `--unsafe` and `--compose` flags to `config env` command. + ## [7.0.0] - 2023-09-25 ### Fixed diff --git a/src/dipdup/cli.py b/src/dipdup/cli.py index 48f50ca0a..53356c306 100644 --- a/src/dipdup/cli.py +++ b/src/dipdup/cli.py @@ -344,20 +344,34 @@ async def config_export(ctx: click.Context, unsafe: bool, full: bool) -> None: @config.command(name='env') @click.option('--output', '-o', type=str, default=None, help='Output to file instead of stdout.') +@click.option('--unsafe', is_flag=True, help='Resolve environment variables or use default values from the config.') +@click.option('--compose', is_flag=True, help='Output in docker-compose format.') @click.pass_context @_cli_wrapper -async def config_env(ctx: click.Context, output: str | None) -> None: +async def config_env(ctx: click.Context, output: str | None, unsafe: bool, compose: bool) -> None: """Dump environment variables used in DipDup config. If variable is not set, default value will be used. """ - from dipdup.config import DipDupConfig + from dipdup.yaml import DipDupYAMLConfig - config = DipDupConfig.load( + config, environment = DipDupYAMLConfig.load( paths=ctx.obj.config._paths, - environment=True, + environment=unsafe, ) - content = '\n'.join(f'{k}={v}' for k, v in sorted(config._environment.items())) + if compose: + content = '\nservices:\n dipdup:\n environment:\n' + _tab = ' ' * 6 + for k, v in sorted(environment.items()): + line = f'{_tab}- {k}=' + '${' + k + if v: + line += ':-' + v + '}' + else: + line += '}' + + content += line + '\n' + else: + content = '\n'.join(f'{k}={v}' for k, v in sorted(environment.items())) if output: Path(output).write_text(content) else: