Skip to content

Commit

Permalink
Add bash completion for migration console link cli
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <akurait@amazon.com>
  • Loading branch information
AndreKurait committed Jun 25, 2024
1 parent 13903ea commit f268d4c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ RUN pipenv install --system --deploy --ignore-pipfile
WORKDIR /root
#CMD pipenv run python manage.py runserver_plus 0.0.0.0:8000

# Ensure bash completion is installed
RUN apt-get install -y bash-completion

CMD /root/loadServicesFromParameterStore.sh && tail -f /dev/null
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from console_link.environment import Environment
from console_link.models.metrics_source import Component, MetricStatistic
from console_link.models.snapshot import SnapshotStatus
from click.shell_completion import get_completion_class

import logging

Expand Down Expand Up @@ -284,8 +285,55 @@ def get_metrics_data_cmd(ctx, component, metric_name, statistic, lookback):
metric_data
)

# ##################### UTILITIES ###################

@cli.command()
@click.option(
"--config-file", default="/etc/migration_services.yaml", help="Path to config file"
)
@click.option("--json", is_flag=True)
@click.argument('shell', type=click.Choice(['bash', 'zsh', 'fish']))
@click.pass_obj
def completion(ctx, config_file, json, shell):
"""Generate shell completion script and instructions for setup.
Supported shells: bash, zsh, fish
To enable completion:
Bash:
console completion bash > /etc/bash_completion.d/console
# Then restart your shell
Zsh:
# If shell completion is not already enabled in your environment,
# you will need to enable it. You can execute the following once:
echo "autoload -U compinit; compinit" >> ~/.zshrc
console completion zsh > "${fpath[1]}/_console"
# Then restart your shell
Fish:
console completion fish > ~/.config/fish/completions/console.fish
# Then restart your shell
"""
completion_class = get_completion_class(shell)
if completion_class is None:
click.echo(f"Error: {shell} shell is currently not supported", err=True)
ctx.exit(1)

try:
completion_script = completion_class(lambda: cli(ctx, config_file, json),
{},
"console",
"_CONSOLE_COMPLETE").source()
click.echo(completion_script)
except RuntimeError as exc:
click.echo(f"Error: {exc}", err=True)
ctx.exit(1)

#################################################

if __name__ == "__main__":
cli()

Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ if [ $? -ne 0 ]; then
fi

echo "Parameter value successfully written to $OUTPUT_FILE"

# Generate bash completion script
console completion bash > /usr/share/bash-completion/completions/console

# Source the completion script to enable it for the current session
source /usr/share/bash-completion/completions/console

# Add sourcing of the completion script to .bashrc for persistence across sessions
echo '. /etc/bash_completion' >> ~/.bashrc

echo "Bash completion for console command has been set up and enabled."

0 comments on commit f268d4c

Please sign in to comment.