From b026237b80cc4355b6820b48c7adc3ff716e790a Mon Sep 17 00:00:00 2001 From: Lingjia Liu Date: Fri, 26 Nov 2021 10:29:40 +1100 Subject: [PATCH] allow url be among options --- thefuck/rules/ping.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/thefuck/rules/ping.py b/thefuck/rules/ping.py index 09a2a08..ca3c81d 100644 --- a/thefuck/rules/ping.py +++ b/thefuck/rules/ping.py @@ -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:])