Skip to content

feat: implement standard completion command #917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/oci_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from oci_cli.cli_root import cli
from oci_cli import cli_session # noqa: F401,E402
from oci_cli import cli_setup # noqa: F401
from oci_cli import cli_completion # noqa: F401
from oci_cli import cli_setup_bootstrap # noqa: F401
from oci_cli import raw_request_cli # noqa: F401
from interactive import cli_interactive # noqa: F401
Expand Down
32 changes: 32 additions & 0 deletions src/oci_cli/cli_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8

# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.

from __future__ import print_function

import pathlib

from oci_cli import cli_util
from oci_cli.cli_root import cli

# from alloy import alloy_util

completion_instructions = """
This command prints completion code for the specified shell (bash).
"""


@cli.group('completion', help=completion_instructions)
@cli_util.help_option_group
def completion():
pass


@completion.command('bash', help="""Print completions for bash""")
def completion_bash():
from oci_cli.cli_setup import setup_autocomplete_non_windows_completion_script_file as csf

csf_contents = pathlib.Path(csf()).read_text()

print(csf_contents)
32 changes: 21 additions & 11 deletions src/oci_cli/cli_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,16 +475,21 @@ def setup_autocomplete():
setup_autocomplete_non_windows()


def setup_autocomplete_windows():
oci_tab_completion_file = 'OciTabExpansion.ps1'
oci_win_tab_completion_file = 'OciTabExpansion.ps1'


script_relative_path = os.path.join('bin', oci_tab_completion_file)
def setup_autocomplete_windows_find_completion_script_file():
script_relative_path = os.path.join('bin', oci_win_tab_completion_file)
path_to_install_dir = os.path.dirname(os.path.abspath(__file__))
completion_script_file = os.path.join(path_to_install_dir, script_relative_path)
if not os.path.exists(completion_script_file):
click.echo('Could not locate autocomplete script at {}. Exiting script.'.format(completion_script_file))
sys.exit(1)
return completion_script_file


def setup_autocomplete_windows():
completion_script_file = setup_autocomplete_windows_find_completion_script_file()
click.echo("Using tab completion script at: {}".format(completion_script_file))

# subprocess.check_output looks like it comes back as a byte string, so coerce to a regular string
Expand All @@ -499,8 +504,8 @@ def setup_autocomplete_windows():
f.seek(0)
content = f.read()

if oci_tab_completion_file in content:
click.echo("It looks like tab completion for oci is already configured in {ps_profile_file_path}. If you want to re-run the setup command please remove any lines containing '{oci_tab_completion_file}' from {ps_profile_file_path}.".format(oci_tab_completion_file=oci_tab_completion_file, ps_profile_file_path=ps_profile_file_path))
if oci_win_tab_completion_file in content:
click.echo("It looks like tab completion for oci is already configured in {ps_profile_file_path}. If you want to re-run the setup command please remove any lines containing '{oci_win_tab_completion_file}' from {ps_profile_file_path}.".format(oci_win_tab_completion_file=oci_win_tab_completion_file, ps_profile_file_path=ps_profile_file_path))
return

f.write('\n. {}\n'.format(completion_script_file))
Expand Down Expand Up @@ -582,19 +587,24 @@ def prompt_input_with_default(msg, default):
return prompt_input('{}: '.format(msg))


def setup_autocomplete_non_windows_completion_script_file():
script_relative_path = os.path.join('bin', 'oci_autocomplete.sh')
path_to_install_dir = os.path.dirname(os.path.abspath(__file__))
completion_script_file = os.path.join(path_to_install_dir, script_relative_path)
if not os.path.exists(completion_script_file):
click.echo('Could not locate autocomplete script at {}. Exiting script.'.format(completion_script_file))
sys.exit(1)
return completion_script_file


def setup_autocomplete_non_windows():
click.echo("To set up autocomplete, we would update few lines in rc/bash_profile file.")
rc_file_path = get_rc_file_path()
if not rc_file_path:
raise CLIInstallError('No suitable profile file found.')

# source bash completion script in CLI install directory
script_relative_path = os.path.join('bin', 'oci_autocomplete.sh')
path_to_install_dir = os.path.dirname(os.path.abspath(__file__))
completion_script_file = os.path.join(path_to_install_dir, script_relative_path)
if not os.path.exists(completion_script_file):
click.echo('Could not locate autocomplete script at {}. Exiting script.'.format(completion_script_file))
sys.exit(1)
completion_script_file = setup_autocomplete_non_windows_completion_script_file()

click.echo("Using tab completion script at: {}".format(completion_script_file))
soft_link = os.path.expanduser("~/lib/oci_autocomplete.sh")
Expand Down