Skip to content

Commit

Permalink
fix(shell): improve binary shim by generating temporary file (#211)
Browse files Browse the repository at this point in the history
Close #211
  • Loading branch information
Toilal committed Nov 5, 2021
1 parent 1597d85 commit 22046e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ddb/feature/shell/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import base64
import json
import os
import shlex
from typing import Iterable

import zgitignore
Expand Down Expand Up @@ -213,7 +214,7 @@ def execute(self):
if not shim_directory:
continue

binary = DefaultBinary(alias, aliases[alias])
binary = DefaultBinary(alias, shlex.split(aliases[alias]))
written, shim = self.shell.create_alias_binary_shim(shim_directory, binary)
if written:
context.log.success("Shim created: %s", shim)
Expand Down
26 changes: 17 additions & 9 deletions ddb/feature/shell/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ 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 = ""
if global_:
ddb_project_home_variable = next(
self.set_environment_variable(config.env_prefix + "_PROJECT_HOME", config.paths.project_home)
Expand All @@ -138,18 +139,25 @@ def create_binary_shim(self, shims_path: str, name: str, global_: bool):
self.set_environment_variable("COMPOSE_IGNORE_ORPHANS", "1")
)

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

command += 'shim=$(tempfile -p ddb.run.)\n' \
'echo "rm $shim">>"$shim"\n' \
f'echo $(ddb run {name} \"$@\") "$@">>"$shim"\n' \
'source "$shim"\n'

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

def create_alias_binary_shim(self, shims_path: str, binary: Binary) -> Tuple[bool, str]:
return self._write_shim(shims_path, binary.name, 'alias', binary.command())
alias_command = []
for command_item in binary.command():
alias_command.append(command_item)
alias_command.append('"$@"')

return self._write_shim(shims_path, binary.name, 'alias', alias_command)

@staticmethod
def _write_shim(shims_path: str, shim_name: str, shim_type: str, command: Iterable[str]) -> Tuple[bool, str]:
Expand All @@ -160,7 +168,7 @@ def _write_shim(shims_path: str, shim_name: str, shim_type: str, command: Iterab
os.makedirs(shims_path, exist_ok=True)
shim = os.path.join(os.path.normpath(shims_path), shim_name)

content = '\n'.join(["#!/usr/bin/env bash", "# ddb:shim:" + shim_type, ''.join(command) + " \"$@\""]) + "\n"
content = '\n'.join(["#!/usr/bin/env bash", "# ddb:shim:" + shim_type, ' '.join(command)]) + "\n"

written = write_if_different(shim, content, newline="\n")

Expand Down

0 comments on commit 22046e9

Please sign in to comment.