Update docker commands. (#940)
* Add: Tests for newer version docker support. * Add: Support for newer versions of docker (Modified rules.docker_not_command). * Fix: Updated disabling memoize. * Change: removed empty list check. * Fix: _parse_commands now uses line.strip() internally and ends_with arg now doesn't end with newline. * Change: Replaced disable_memoize in favor of no_memoize fixture. * Fix: removed unused import.
This commit is contained in:
@@ -8,16 +8,30 @@ from thefuck.specific.sudo import sudo_support
|
||||
@sudo_support
|
||||
@for_app('docker')
|
||||
def match(command):
|
||||
return 'is not a docker command' in command.output
|
||||
return 'is not a docker command' in command.output or 'Usage: docker' in command.output
|
||||
|
||||
|
||||
def _parse_commands(lines, starts_with):
|
||||
lines = dropwhile(lambda line: not line.startswith(starts_with), lines)
|
||||
lines = islice(lines, 1, None)
|
||||
lines = list(takewhile(lambda line: line.strip(), lines))
|
||||
return [line.strip().split(' ')[0] for line in lines]
|
||||
|
||||
|
||||
def get_docker_commands():
|
||||
proc = subprocess.Popen('docker', stdout=subprocess.PIPE)
|
||||
lines = [line.decode('utf-8') for line in proc.stdout.readlines()]
|
||||
lines = dropwhile(lambda line: not line.startswith('Commands:'), lines)
|
||||
lines = islice(lines, 1, None)
|
||||
lines = list(takewhile(lambda line: line != '\n', lines))
|
||||
return [line.strip().split(' ')[0] for line in lines]
|
||||
proc = subprocess.Popen('docker', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
# Old version docker returns its output to stdout, while newer version returns to stderr.
|
||||
lines = proc.stdout.readlines() or proc.stderr.readlines()
|
||||
lines = [line.decode('utf-8') for line in lines]
|
||||
|
||||
# Only newer versions of docker have management commands in the help text.
|
||||
if 'Management Commands:\n' in lines:
|
||||
management_commands = _parse_commands(lines, 'Management Commands:')
|
||||
else:
|
||||
management_commands = []
|
||||
regular_commands = _parse_commands(lines, 'Commands:')
|
||||
return management_commands + regular_commands
|
||||
|
||||
|
||||
if which('docker'):
|
||||
@@ -26,6 +40,10 @@ if which('docker'):
|
||||
|
||||
@sudo_support
|
||||
def get_new_command(command):
|
||||
if 'Usage:' in command.output and len(command.script_parts) > 1:
|
||||
management_subcommands = _parse_commands(command.output.split('\n'), 'Commands:')
|
||||
return replace_command(command, command.script_parts[2], management_subcommands)
|
||||
|
||||
wrong_command = re.findall(
|
||||
r"docker: '(\w+)' is not a docker command.", command.output)[0]
|
||||
return replace_command(command, wrong_command, get_docker_commands())
|
||||
|
||||
Reference in New Issue
Block a user