Skip to content

Commit

Permalink
Properly setup Breeze2 completion in bash if it is first completion (#…
Browse files Browse the repository at this point in the history
…22726)

In case you've never setup bash completion in Bash you'd miss
the .bash_completion file and autocompletion setup in Breeze2
would fail.

In case the file is missing we skip backing it up and create it
now. Also installing click is done before modifying the scripts,
this way we always install latest click version for the user
when running setup-autocomplete and the 'source' instructions
are printed last so the user will not miss it.
  • Loading branch information
potiuk authored Apr 4, 2022
1 parent f9e1847 commit 2cf1ae3
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions dev/breeze/src/airflow_breeze/breeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,11 +948,15 @@ def write_to_shell(command_to_execute: str, dry_run: bool, script_path: str, for
else:
backup(script_path_file)
remove_autogenerated_code(script_path)
console.print(f"\nModifying the {script_path} file!\n")
console.print(f"\nCopy of the file is held in {script_path}.bak !\n")
if not dry_run:
backup(script_path_file)
text = script_path_file.read_text()
text = ''
if script_path_file.exists():
console.print(f"\nModifying the {script_path} file!\n")
console.print(f"\nCopy of the original file is held in {script_path}.bak !\n")
if not dry_run:
backup(script_path_file)
text = script_path_file.read_text()
else:
console.print(f"\nCreating the {script_path} file!\n")
if not dry_run:
script_path_file.write_text(
text
Expand All @@ -965,7 +969,8 @@ def write_to_shell(command_to_execute: str, dry_run: bool, script_path: str, for
else:
console.print(f"[bright_blue]The autocomplete script would be added to {script_path}[/]")
console.print(
f"\n[bright_yellow]Please exit and re-enter your shell or run:[/]\n\n `source {script_path}`\n"
f"\n[bright_yellow]IMPORTANT!!!! Please exit and re-enter your shell or run:[/]"
f"\n\n `source {script_path}`\n"
)
return True

Expand Down Expand Up @@ -1000,14 +1005,15 @@ def setup_autocomplete(verbose: bool, dry_run: bool, force_setup: bool):
"install 'click' package in your default python installation destination.[/]\n"
)
if click.confirm("Should we proceed ?"):
run_command(['pip', 'install', '--upgrade', 'click'], verbose=True, dry_run=dry_run, check=False)
if detected_shell == 'bash':
script_path = str(Path('~').expanduser() / '.bash_completion')
command_to_execute = f"source {autocomplete_path}"
updated = write_to_shell(command_to_execute, dry_run, script_path, force_setup)
write_to_shell(command_to_execute, dry_run, script_path, force_setup)
elif detected_shell == 'zsh':
script_path = str(Path('~').expanduser() / '.zshrc')
command_to_execute = f"source {autocomplete_path}"
updated = write_to_shell(command_to_execute, dry_run, script_path, force_setup)
write_to_shell(command_to_execute, dry_run, script_path, force_setup)
elif detected_shell == 'fish':
# Include steps for fish shell
script_path = str(Path('~').expanduser() / f'.config/fish/completions/{NAME}.fish')
Expand All @@ -1020,7 +1026,6 @@ def setup_autocomplete(verbose: bool, dry_run: bool, force_setup: bool):
with open(autocomplete_path) as source_file, open(script_path, 'w') as destination_file:
for line in source_file:
destination_file.write(line)
updated = True
else:
# Include steps for powershell
subprocess.check_call(['powershell', 'Set-ExecutionPolicy Unrestricted -Scope CurrentUser'])
Expand All @@ -1029,8 +1034,6 @@ def setup_autocomplete(verbose: bool, dry_run: bool, force_setup: bool):
)
command_to_execute = f". {autocomplete_path}"
write_to_shell(command_to_execute, dry_run, script_path, force_setup)
if updated:
run_command(['pip', 'install', '--upgrade', 'click'], verbose=True, dry_run=dry_run, check=False)
else:
console.print(
"\nPlease follow the https://click.palletsprojects.com/en/8.1.x/shell-completion/ "
Expand Down

0 comments on commit 2cf1ae3

Please sign in to comment.