From 7bf405e9c369c79236fc50ec3ff608bf6112f5d2 Mon Sep 17 00:00:00 2001 From: JordonPhillips Date: Thu, 29 Sep 2016 14:19:43 -0700 Subject: [PATCH] Add aws cli rule This rule corrects spelling mistakes for aws cli commands and subcommands. --- README.md | 1 + tests/rules/test_aws_cli.py | 101 ++++++++++++++++++++++++++++++++++++ thefuck/rules/aws_cli.py | 17 ++++++ 3 files changed, 119 insertions(+) create mode 100644 tests/rules/test_aws_cli.py create mode 100644 thefuck/rules/aws_cli.py diff --git a/README.md b/README.md index d4dbac48..536ca3c2 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ sudo -H pip install thefuck --upgrade The Fuck tries to match a rule for the previous command, creates a new command using the matched rule and runs it. Rules enabled by default are as follows: +* `aws_cli` – fixes misspelled commands like `aws dynamdb scan` * `cargo` – runs `cargo build` instead of `cargo`; * `cargo_no_command` – fixes wrongs commands like `cargo buid`; * `cd_correction` – spellchecks and correct failed cd commands; diff --git a/tests/rules/test_aws_cli.py b/tests/rules/test_aws_cli.py new file mode 100644 index 00000000..48b10593 --- /dev/null +++ b/tests/rules/test_aws_cli.py @@ -0,0 +1,101 @@ +import pytest + +from thefuck.rules.aws_cli import match, get_new_command +from tests.utils import Command + + +no_suggestions = '''\ +usage: aws [options] [ ...] [parameters] +To see help text, you can run: + + aws help + aws help + aws help +aws: error: argument command: Invalid choice, valid choices are: + +dynamodb | dynamodbstreams +ec2 | ecr +''' + + +misspelled_command = '''\ +usage: aws [options] [ ...] [parameters] +To see help text, you can run: + + aws help + aws help + aws help +aws: error: argument command: Invalid choice, valid choices are: + +dynamodb | dynamodbstreams +ec2 | ecr + + +Invalid choice: 'dynamdb', maybe you meant: + + * dynamodb +''' + + +misspelled_subcommand = '''\ +usage: aws [options] [ ...] [parameters] +To see help text, you can run: + + aws help + aws help + aws help +aws: error: argument operation: Invalid choice, valid choices are: + +query | scan +update-item | update-table + + +Invalid choice: 'scn', maybe you meant: + + * scan +''' + + +misspelled_subcommand_with_multiple_options = '''\ +usage: aws [options] [ ...] [parameters] +To see help text, you can run: + + aws help + aws help + aws help +aws: error: argument operation: Invalid choice, valid choices are: + +describe-table | get-item +list-tables | put-item + + +Invalid choice: 't-item', maybe you meant: + + * put-item + * get-item +''' + + +@pytest.mark.parametrize('command', [ + Command('aws dynamdb scan', stderr=misspelled_command), + Command('aws dynamodb scn', stderr=misspelled_subcommand), + Command('aws dynamodb t-item', + stderr=misspelled_subcommand_with_multiple_options)]) +def test_match(command): + assert match(command) + + +def test_not_match(): + assert not match(Command('aws dynamodb invalid', stderr=no_suggestions)) + + +@pytest.mark.parametrize('command, result', [ + (Command('aws dynamdb scan', stderr=misspelled_command), + ['aws dynamodb scan']), + (Command('aws dynamodb scn', stderr=misspelled_subcommand), + ['aws dynamodb scan']), + (Command('aws dynamodb t-item', + stderr=misspelled_subcommand_with_multiple_options), + ['aws dynamodb put-item', 'aws dynamodb get-item'])]) +def test_get_new_command(command, result): + assert get_new_command(command) == result diff --git a/thefuck/rules/aws_cli.py b/thefuck/rules/aws_cli.py new file mode 100644 index 00000000..565173f6 --- /dev/null +++ b/thefuck/rules/aws_cli.py @@ -0,0 +1,17 @@ +import re + +from thefuck.utils import for_app, replace_argument + +INVALID_CHOICE = "(?<=Invalid choice: ')(.*)(?=', maybe you meant:)" +OPTIONS = "^\s*\*\s(.*)" + + +@for_app('aws') +def match(command): + return "usage:" in command.stderr and "maybe you meant:" in command.stderr + + +def get_new_command(command): + mistake = re.search(INVALID_CHOICE, command.stderr).group(0) + options = re.findall(OPTIONS, command.stderr, flags=re.MULTILINE) + return [replace_argument(command.script, mistake, o) for o in options]