allow url be among options

This commit is contained in:
Lingjia Liu 2021-11-26 10:29:40 +11:00
parent 2914021364
commit b026237b80

View File

@ -1,19 +1,37 @@
# -*- encoding: utf-8 -*-
from six.moves.urllib.parse import urlparse
from thefuck.utils import for_app
from thefuck.utils import for_app, memoize
@memoize
def get_hostname_from_url(maybeUrl):
try:
results = urlparse(maybeUrl)
return results.hostname
except Exception:
return None
@memoize
def get_index_of_url(parts):
for i, part in enumerate(parts):
if get_hostname_from_url(part):
return i
return None
@for_app('ping')
def match(command):
# It only fix if the target host is a valid url
try:
results = urlparse(command.script_parts[-1])
is_valid_url = results.hostname is not None
except Exception:
is_valid_url = False
return 'cannot resolve' in command.output and is_valid_url
if 'cannot resolve' not in command.output:
return False
# It only fix if the command has a valid url
index_of_url = get_index_of_url(command.script_parts)
return index_of_url is not None
def get_new_command(command):
result = urlparse(command.script_parts[-1])
return ' '.join(command.script_parts[:-1]) + ' ' + result.hostname
index_of_url = get_index_of_url(command.script_parts)
url = command.script_parts[index_of_url]
hostname = get_hostname_from_url(url)
return ' '.join(command.script_parts[:index_of_url] + [hostname] + command.script_parts[index_of_url + 1:])