mirror of
https://github.com/nvbn/thefuck.git
synced 2025-03-14 14:48:49 +00:00
Add: Support for newer versions of docker (Modified rules.docker_not_command).
This commit is contained in:
parent
a9f1b419e3
commit
bdcf0132bf
@ -8,24 +8,42 @@ 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 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)
|
||||
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 != '\n', lines))
|
||||
return [line.strip().split(' ')[0] for line in lines]
|
||||
|
||||
|
||||
def get_docker_commands():
|
||||
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:\n')
|
||||
else:
|
||||
management_commands = []
|
||||
regular_commands = _parse_commands(lines, 'Commands:\n')
|
||||
return management_commands + regular_commands if management_commands else regular_commands
|
||||
|
||||
|
||||
if which('docker'):
|
||||
get_docker_commands = cache(which('docker'))(get_docker_commands)
|
||||
|
||||
|
||||
@sudo_support
|
||||
def get_new_command(command):
|
||||
if 'Usage:' in command.output and len(command.script_parts) > 1:
|
||||
management_subcommands = _parse_commands(map(lambda x: x + '\n', command.output.split('\n')), 'Commands:\n')
|
||||
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())
|
||||
|
Loading…
x
Reference in New Issue
Block a user