Skip to content

Commit

Permalink
fix(binary): fix orphan containers when using global binaries (#195)
Browse files Browse the repository at this point in the history
Close #195
  • Loading branch information
Toilal committed Feb 9, 2021
1 parent dc1c0db commit 40b3cae
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ddb/feature/core/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def execute(self):
last_release = check_for_update(github_repository, True)
if not last_release:
print('ddb is already up to date.')
if not config.args.force:
if 'force' not in config.args or not config.args.force:
return

if not last_release:
Expand Down
17 changes: 14 additions & 3 deletions ddb/feature/shell/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import os
from argparse import ArgumentParser
from typing import Iterable, ClassVar

from dotty_dict import Dotty
Expand Down Expand Up @@ -52,9 +53,19 @@ def actions(self) -> Iterable[Action]:

@property
def phases(self) -> Iterable[Phase]:
def activate_parser(parser: ArgumentParser):
parser.add_argument("--force", action="store_true",
help="Force activation when a project is already activated.")

def deactivate_parser(parser: ArgumentParser):
parser.add_argument("--force", action="store_true",
help="Force deactivation when a project is already deactivated")

return (
DefaultPhase("activate", "Write a shell script to be executed to activate environment"),
DefaultPhase("deactivate", "Write a shell script to be executed to deactivate environment"),
DefaultPhase("activate", "Write a shell script to be executed to activate environment",
activate_parser),
DefaultPhase("deactivate", "Write a shell script to be executed to deactivate environment",
deactivate_parser),
DefaultPhase("check-activated", "Check if project is activated in current shell"),
)

Expand All @@ -64,7 +75,7 @@ def commands(self) -> Iterable[Command]:
LifecycleCommand("activate",
"Write a shell script to be executed to activate environment",
"activate",
avoid_stdout=True
avoid_stdout=True,
),
LifecycleCommand("deactivate",
"Write a shell script to be executed to deactivate environment",
Expand Down
9 changes: 6 additions & 3 deletions ddb/feature/shell/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,11 @@ def execute(self):
Execute action
"""
try:
check_activated(True)
raise CheckIsActivatedException("project is already activated.")
check_activated('force' not in config.args or not config.args.force)
if config.args.force:
self._deactivate()
else:
raise CheckIsActivatedException("project is already activated.")
except CheckIsNotActivatedException:
pass

Expand Down Expand Up @@ -339,7 +342,7 @@ def execute(self):
Execute action
"""
try:
check_activated(True)
check_activated('force' not in config.args or not config.args.force)
except CheckAnotherProjectActivatedException:
pass

Expand Down
30 changes: 25 additions & 5 deletions ddb/feature/shell/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,17 @@ def remove_binary_shim(self, shims_path: str, name: str) -> Optional[str]:
return shim

def create_binary_shim(self, shims_path: str, name: str, global_: bool):
command = f"$(ddb run {name} \"$@\")"
if global_:
environment_variable = next(
self.set_environment_variable(config.env_prefix + "_PROJECT_HOME", config.paths.project_home)
)
command = f"{environment_variable}\n{command}"

command = f"$(ddb deactivate --force)\n" \
f"{environment_variable}\n" \
f"$(ddb activate --force)\n" \
f"$(ddb run {name} \"$@\")"
else:
command = f"$(ddb run {name} \"$@\")"

return self._write_shim(shims_path, name, 'binary', command)

Expand Down Expand Up @@ -203,10 +208,25 @@ def remove_binary_shim(self, shims_path: str, name: str) -> Optional[str]:
def create_binary_shim(self, shims_path: str, name: str, global_: bool):
commands = []
if global_:
environment_variable = next(
commands.extend([
"set command=(ddb deactivate --force)",
"%command%>cmd.txt",
"set /p execution=<cmd.txt",
"del cmd.txt",
"%execution%"
])

commands.append(next(
self.set_environment_variable(config.env_prefix + "_PROJECT_HOME", config.paths.project_home)
)
commands.append(environment_variable)
))

commands.extend([
"set command=(ddb activate --force)",
"%command%>cmd.txt",
"set /p execution=<cmd.txt",
"del cmd.txt",
"%execution%"
])

commands.extend([
f"set command=(ddb run {name} \"%*\")",
Expand Down
12 changes: 12 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,25 @@ optional arguments:
Display a script for the configured shell that must be evaluated to active the project environment inside the current
shell session.

```
optional arguments:
-h, --help show this help message and exit
--force Force activation when a project is already activated.
```

Read more: [Shell feature](features/shell.md)

### ddb deactivate

Display a script for the configured shell that must be evaluated to deactivate the project environment inside the
current shell session.

```
optional arguments:
-h, --help show this help message and exit
--force Force deactivation when a project is already deactivated
```

Read more: [Shell feature](features/shell.md)

### ddb check-activated
Expand Down

0 comments on commit 40b3cae

Please sign in to comment.