mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-12 07:59:01 +00:00
Add transparent sudo support for rules where it required
This commit is contained in:
parent
7010b3a7f6
commit
e7b78205f4
2
setup.py
2
setup.py
@ -15,6 +15,6 @@ setup(name='thefuck',
|
|||||||
'tests', 'release']),
|
'tests', 'release']),
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
install_requires=['pathlib', 'psutil', 'colorama'],
|
install_requires=['pathlib', 'psutil', 'colorama', 'six'],
|
||||||
entry_points={'console_scripts': [
|
entry_points={'console_scripts': [
|
||||||
'thefuck = thefuck.main:main']})
|
'thefuck = thefuck.main:main']})
|
||||||
|
17
tests/test_utils.py
Normal file
17
tests/test_utils.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from mock import Mock
|
||||||
|
from thefuck.utils import sudo_support
|
||||||
|
from thefuck.main import Command
|
||||||
|
|
||||||
|
|
||||||
|
def test_sudo_support():
|
||||||
|
fn = Mock(return_value=True, __name__='')
|
||||||
|
assert sudo_support(fn)(Command('sudo ls', 'out', 'err'), None)
|
||||||
|
fn.assert_called_once_with(Command('ls', 'out', 'err'), None)
|
||||||
|
|
||||||
|
fn.return_value = False
|
||||||
|
assert not sudo_support(fn)(Command('sudo ls', 'out', 'err'), None)
|
||||||
|
|
||||||
|
fn.return_value = 'pwd'
|
||||||
|
assert sudo_support(fn)(Command('sudo ls', 'out', 'err'), None) == 'sudo pwd'
|
||||||
|
|
||||||
|
assert sudo_support(fn)(Command('ls', 'out', 'err'), None) == 'pwd'
|
@ -1,10 +1,13 @@
|
|||||||
import re
|
import re
|
||||||
|
from thefuck.utils import sudo_support
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return command.script.startswith('cp ') \
|
return command.script.startswith('cp ') \
|
||||||
and 'cp: omitting directory' in command.stderr.lower()
|
and 'cp: omitting directory' in command.stderr.lower()
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return re.sub(r'^cp', 'cp -a', command.script)
|
return re.sub(r'^cp', 'cp -a', command.script)
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
import os
|
import os
|
||||||
|
from thefuck.utils import sudo_support
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return os.path.exists(command.script.split()[0]) \
|
return os.path.exists(command.script.split()[0]) \
|
||||||
and 'command not found' in command.stderr
|
and 'command not found' in command.stderr
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return u'./{}'.format(command.script)
|
return u'./{}'.format(command.script)
|
||||||
|
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
import re
|
import re
|
||||||
|
from thefuck.utils import sudo_support
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return (command.script.startswith('lein')
|
return (command.script.startswith('lein')
|
||||||
and "is not a task. See 'lein help'" in command.stderr
|
and "is not a task. See 'lein help'" in command.stderr
|
||||||
and 'Did you mean this?' in command.stderr)
|
and 'Did you mean this?' in command.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
broken_cmd = re.findall(r"'([^']*)' is not a task",
|
broken_cmd = re.findall(r"'([^']*)' is not a task",
|
||||||
command.stderr)[0]
|
command.stderr)[0]
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import re
|
import re
|
||||||
|
from thefuck.utils import sudo_support
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return ('mkdir' in command.script
|
return ('mkdir' in command.script
|
||||||
and 'No such file or directory' in command.stderr)
|
and 'No such file or directory' in command.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return re.sub('^mkdir (.*)', 'mkdir -p \\1', command.script)
|
return re.sub('^mkdir (.*)', 'mkdir -p \\1', command.script)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from difflib import get_close_matches
|
from difflib import get_close_matches
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from thefuck.utils import sudo_support
|
||||||
|
|
||||||
|
|
||||||
def _safe(fn, fallback):
|
def _safe(fn, fallback):
|
||||||
@ -17,12 +18,14 @@ def _get_all_bins():
|
|||||||
if not _safe(exe.is_dir, True)]
|
if not _safe(exe.is_dir, True)]
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return 'not found' in command.stderr and \
|
return 'not found' in command.stderr and \
|
||||||
bool(get_close_matches(command.script.split(' ')[0],
|
bool(get_close_matches(command.script.split(' ')[0],
|
||||||
_get_all_bins()))
|
_get_all_bins()))
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
old_command = command.script.split(' ')[0]
|
old_command = command.script.split(' ')[0]
|
||||||
new_command = get_close_matches(old_command,
|
new_command = get_close_matches(old_command,
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
|
from thefuck.utils import sudo_support
|
||||||
# add 'python' suffix to the command if
|
# add 'python' suffix to the command if
|
||||||
# 1) The script does not have execute permission or
|
# 1) The script does not have execute permission or
|
||||||
# 2) is interpreted as shell script
|
# 2) is interpreted as shell script
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
toks = command.script.split()
|
toks = command.script.split()
|
||||||
return (len(toks) > 0
|
return (len(toks) > 0
|
||||||
@ -10,5 +13,6 @@ def match(command, settings):
|
|||||||
'command not found' in command.stderr))
|
'command not found' in command.stderr))
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return 'python ' + command.script
|
return 'python ' + command.script
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import re
|
import re
|
||||||
|
from thefuck.utils import sudo_support
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return ('rm' in command.script
|
return ('rm' in command.script
|
||||||
and 'is a directory' in command.stderr)
|
and 'is a directory' in command.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return re.sub('^rm (.*)', 'rm -rf \\1', command.script)
|
return re.sub('^rm (.*)', 'rm -rf \\1', command.script)
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
|
from thefuck.utils import sudo_support
|
||||||
|
|
||||||
|
|
||||||
enabled_by_default = False
|
enabled_by_default = False
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return ({'rm', '/'}.issubset(command.script.split())
|
return ({'rm', '/'}.issubset(command.script.split())
|
||||||
and '--no-preserve-root' not in command.script
|
and '--no-preserve-root' not in command.script
|
||||||
and '--no-preserve-root' in command.stderr)
|
and '--no-preserve-root' in command.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
return u'{} --no-preserve-root'.format(command.script)
|
return u'{} --no-preserve-root'.format(command.script)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
import os
|
import os
|
||||||
|
import six
|
||||||
|
from thefuck.main import Command
|
||||||
|
|
||||||
|
|
||||||
def which(program):
|
def which(program):
|
||||||
@ -41,3 +43,22 @@ def wrap_settings(params):
|
|||||||
return fn(command, settings)
|
return fn(command, settings)
|
||||||
return wrapper
|
return wrapper
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def sudo_support(fn):
|
||||||
|
"""Removes sudo before calling fn and adds it after."""
|
||||||
|
@wraps(fn)
|
||||||
|
def wrapper(command, settings):
|
||||||
|
if not command.script.startswith('sudo '):
|
||||||
|
return fn(command, settings)
|
||||||
|
|
||||||
|
result = fn(Command(command.script[5:],
|
||||||
|
command.stdout,
|
||||||
|
command.stderr),
|
||||||
|
settings)
|
||||||
|
|
||||||
|
if result and isinstance(result, six.string_types):
|
||||||
|
return u'sudo {}'.format(result)
|
||||||
|
else:
|
||||||
|
return result
|
||||||
|
return wrapper
|
||||||
|
Loading…
x
Reference in New Issue
Block a user