This commit is contained in:
@@ -3,8 +3,10 @@ import os
|
||||
import shlex
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
from subprocess import Popen
|
||||
from typing import Any, Dict, IO, Iterator, List
|
||||
from typing import Any, Dict, IO, Iterator, List, Optional
|
||||
|
||||
if sys.platform == 'win32':
|
||||
from subprocess import Popen
|
||||
|
||||
try:
|
||||
import click
|
||||
@@ -17,7 +19,7 @@ from .main import dotenv_values, set_key, unset_key
|
||||
from .version import __version__
|
||||
|
||||
|
||||
def enumerate_env():
|
||||
def enumerate_env() -> Optional[str]:
|
||||
"""
|
||||
Return a path for the ${pwd}/.env file.
|
||||
|
||||
@@ -161,14 +163,13 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None:
|
||||
if not commandline:
|
||||
click.echo('No command given.')
|
||||
exit(1)
|
||||
ret = run_command(commandline, dotenv_as_dict)
|
||||
exit(ret)
|
||||
run_command(commandline, dotenv_as_dict)
|
||||
|
||||
|
||||
def run_command(command: List[str], env: Dict[str, str]) -> int:
|
||||
"""Run command in sub process.
|
||||
def run_command(command: List[str], env: Dict[str, str]) -> None:
|
||||
"""Replace the current process with the specified command.
|
||||
|
||||
Runs the command in a sub process with the variables from `env`
|
||||
Replaces the current process with the specified command and the variables from `env`
|
||||
added in the current environment variables.
|
||||
|
||||
Parameters
|
||||
@@ -180,8 +181,8 @@ def run_command(command: List[str], env: Dict[str, str]) -> int:
|
||||
|
||||
Returns
|
||||
-------
|
||||
int
|
||||
The return code of the command
|
||||
None
|
||||
This function does not return any value. It replaces the current process with the new one.
|
||||
|
||||
"""
|
||||
# copy the current environment variables and add the vales from
|
||||
@@ -189,11 +190,16 @@ def run_command(command: List[str], env: Dict[str, str]) -> int:
|
||||
cmd_env = os.environ.copy()
|
||||
cmd_env.update(env)
|
||||
|
||||
p = Popen(command,
|
||||
universal_newlines=True,
|
||||
bufsize=0,
|
||||
shell=False,
|
||||
env=cmd_env)
|
||||
_, _ = p.communicate()
|
||||
if sys.platform == 'win32':
|
||||
# execvpe on Windows returns control immediately
|
||||
# rather than once the command has finished.
|
||||
p = Popen(command,
|
||||
universal_newlines=True,
|
||||
bufsize=0,
|
||||
shell=False,
|
||||
env=cmd_env)
|
||||
_, _ = p.communicate()
|
||||
|
||||
return p.returncode
|
||||
exit(p.returncode)
|
||||
else:
|
||||
os.execvpe(command[0], args=command, env=cmd_env)
|
||||
|
||||
Reference in New Issue
Block a user