Skip to content

Commit ca195aa

Browse files
authored
Merge pull request #16 from britive/headless-machine-and-older-bash-completion
Headless machine and older bash completion
2 parents d52eb33 + ee6e785 commit ca195aa

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pybritive
3-
version = 0.2.0
3+
version = 0.2.1
44
author = Britive Inc.
55
author_email = support@britive.com
66
description = A pure Python CLI for Britive
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# register the powershell completion script since it is not required to be registered anywhere else
1+
# register the completion scripts since they are not required to be registered anywhere else
22
from . import powershell_completion
3+
from .import bash_gte_42
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import re
2+
from click.shell_completion import add_completion_class, BashComplete
3+
from gettext import gettext as _
4+
5+
6+
# inspired by https://github.com/pallets-eco/click-bash4.2-completion
7+
_bash_42_source = """\
8+
%(complete_func)s() {
9+
local IFS=$'\\n'
10+
local response
11+
response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD \
12+
%(complete_var)s=bash_complete $1)
13+
for completion in $response; do
14+
IFS=',' read type value <<< "$completion"
15+
if [[ $type == 'dir' ]]; then
16+
COMPREPLY=()
17+
compopt -o dirnames
18+
elif [[ $type == 'file' ]]; then
19+
COMPREPLY=()
20+
compopt -o default
21+
elif [[ $type == 'plain' ]]; then
22+
COMPREPLY+=($value)
23+
fi
24+
done
25+
return 0
26+
}
27+
%(complete_func)s_setup() {
28+
local COMPLETION_OPTIONS=""
29+
local BASH_VERSION_ARR=(${BASH_VERSION//./ })
30+
# Only BASH version 4.4 and later have the nosort option.
31+
if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] \
32+
&& [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then
33+
COMPLETION_OPTIONS="-o nosort"
34+
fi
35+
complete $COMPLETION_OPTIONS -F %(complete_func)s %(prog_name)s
36+
}
37+
%(complete_func)s_setup;
38+
"""
39+
40+
41+
@add_completion_class
42+
class _PatchedBashComplete(BashComplete):
43+
""" Patched Shell completion for Bash """
44+
source_template = _bash_42_source
45+
name = 'bash' # this will override the default bash provided by click
46+
47+
# change the original version check
48+
def _check_version(self) -> None:
49+
import subprocess
50+
51+
output = subprocess.run(
52+
["bash", "-c", "echo ${BASH_VERSION}"], stdout=subprocess.PIPE
53+
)
54+
match = re.search(r"^(\d+)\.(\d+)\.\d+", output.stdout.decode())
55+
56+
if match is not None:
57+
major, minor = match.groups()
58+
59+
if major < "4" or major == "4" and minor < "2":
60+
raise RuntimeError(
61+
_(
62+
"Shell completion is not supported for Bash"
63+
" versions older than 4.2."
64+
)
65+
)
66+
else:
67+
raise RuntimeError(
68+
_("Couldn't detect Bash version, shell completion is not supported.")
69+
)

src/pybritive/helpers/credentials.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import base64
33
import hashlib
44
import time
5+
import webbrowser
56
import requests
67
from pathlib import Path
78
import click
@@ -57,7 +58,14 @@ def __init__(self, tenant_name: str, tenant_alias: str, cli: ConfigManager):
5758
def perform_interactive_login(self):
5859
self.cli.print(f'Performing interactive login against tenant {self.tenant}')
5960
url = f'{self.base_url}/login?token={self.auth_token}'
60-
click.launch(url)
61+
62+
try:
63+
webbrowser.get()
64+
webbrowser.open(url)
65+
except webbrowser.Error:
66+
self.cli.print('No web browser found. Please manually navigate to the link below and authenticate.')
67+
self.cli.print(url)
68+
6169
time.sleep(3)
6270
num_tries = 1
6371
while True:

0 commit comments

Comments
 (0)