Skip to content

Commit

Permalink
fix some problems
Browse files Browse the repository at this point in the history
  • Loading branch information
duarte-pompeu committed Dec 4, 2023
1 parent c585a7b commit 9b1b4af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/dotenv/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import os
import shlex
import sys
Expand All @@ -16,6 +17,8 @@
from .main import dotenv_values, set_key, unset_key
from .version import __version__

logger = logging.getLogger(__name__)


def enumerate_env():
"""
Expand Down Expand Up @@ -44,9 +47,9 @@ def enumerate_env():
help="Whether to write the dot file as an executable bash script.")
@click.version_option(version=__version__)
@click.pass_context
def cli(ctx: click.Context, files: list[Any], quote: Any, export: Any) -> None:
def cli(ctx: click.Context, file: list[Any], quote: Any, export: Any) -> None:
"""This script is used to set, get or unset values from a .env file."""
ctx.obj = {'QUOTE': quote, 'EXPORT': export, 'FILES': files}
ctx.obj = {'QUOTE': quote, 'EXPORT': export, 'FILES': file}


@contextmanager
Expand Down Expand Up @@ -103,12 +106,15 @@ def set(ctx: click.Context, key: Any, value: Any) -> None:
quote = ctx.obj['QUOTE']
export = ctx.obj['EXPORT']

successes = []
for file in files:
success, key, value = set_key(file, key, value, quote, export)
if success:
click.echo(f'{key}={value}')
else:
exit(1)
successes.append(success)

if all(successes):
click.echo(f'{key}={value}')
else:
exit(1)


@cli.command()
Expand Down Expand Up @@ -143,16 +149,17 @@ def unset(ctx: click.Context, key: Any) -> None:
global_success = False
success_files = []
for file in files:
success, key = unset_key(file, key, quote)
success, key = unset_key(file, key, quote, warn_key_not_found=False)
if success:
global_success = True
success_files.append(file)

if global_success:
source = success_files[0] if len(success_files) == 1 else success_files
click.echo("Successfully removed %s from %s" % (key, source))

click.echo("Successfully removed %s from %s." % (key, source))
else:
source = files[0] if len(files) == 1 else files
logger.warning("Key %s not removed from %s - key doesn't exist.", key, source)
exit(1)


Expand Down
3 changes: 2 additions & 1 deletion src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def unset_key(
key_to_unset: str,
quote_mode: str = "always",
encoding: Optional[str] = "utf-8",
warn_key_not_found: bool = True,
) -> Tuple[Optional[bool], str]:
"""
Removes a given key from the given `.env` file.
Expand All @@ -218,7 +219,7 @@ def unset_key(
else:
dest.write(mapping.original.string)

if not removed:
if not removed and warn_key_not_found:
logger.warning("Key %s not removed from %s - key doesn't exist.", key_to_unset, dotenv_path)
return None, key_to_unset

Expand Down

0 comments on commit 9b1b4af

Please sign in to comment.