scp-helper - автозаполнение scp-helper
This commit is contained in:
parent
8e09cb3ed9
commit
302bea298e
17
completion
Normal file
17
completion
Normal file
@ -0,0 +1,17 @@
|
||||
function _ssh_connector_completion()
|
||||
{
|
||||
latest="${COMP_WORDS[$COMP_CWORD]}"
|
||||
words=$(grep -v -E "\#" $SSH_HELPER_HOST_LIST | awk '{print $1}')
|
||||
COMPREPLY=($(compgen -W "$words" -- $latest))
|
||||
return 0
|
||||
}
|
||||
|
||||
function _scp_path_completition()
|
||||
{
|
||||
#COMPREPLY=(${COMP_WORDS})
|
||||
COMPREPLY=($(scp-completion.py ${COMP_WORDS[*]} $COMP_CWORD))
|
||||
return 0
|
||||
}
|
||||
|
||||
complete -F _ssh_connector_completion ssh-helper
|
||||
complete -F _scp_path_completition scp-helper
|
||||
@ -66,7 +66,7 @@ def add_sudo(s):
|
||||
return res
|
||||
|
||||
def make_ssh_cmd(host, port, user, cmd):
|
||||
res = f"ssh {user}@{host} -p {port} '{cmd}'"
|
||||
res = f"/usr/bin/ssh {user}@{host} -p {port} '{cmd}'"
|
||||
return res
|
||||
|
||||
def get_connection_config(con_name):
|
||||
|
||||
3
install_completition.sh
Executable file
3
install_completition.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
ln -s $(pwd)/completion /etc/bash_completion.d/ssh-helper
|
||||
96
scp-completion.py
Executable file
96
scp-completion.py
Executable file
@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
from helperlib import get_config, parse_arg, add_sudo, make_ssh_cmd, get_path_config
|
||||
|
||||
DEBUG = True
|
||||
SUDO_REMOTE = True
|
||||
|
||||
def get_conf_names():
|
||||
res = []
|
||||
for i in get_config().keys():
|
||||
res.append(f'{i}:')
|
||||
return res
|
||||
|
||||
def ls_cmd(path):
|
||||
cmd = f'ls {path}'
|
||||
return cmd
|
||||
|
||||
def make_full_cmd(config):
|
||||
cmd = ls_cmd(config['work_path'])
|
||||
|
||||
if SUDO_REMOTE and config['connection_name'] != 'local':
|
||||
cmd = add_sudo(cmd)
|
||||
|
||||
if config['connection_name'] != 'local':
|
||||
cmd = make_ssh_cmd(
|
||||
config['host'],
|
||||
config['port'],
|
||||
config['user'],
|
||||
cmd
|
||||
)
|
||||
|
||||
return cmd
|
||||
|
||||
def get_current_arg(args):
|
||||
# Возвращает заполняемый аргумент
|
||||
res = None
|
||||
if len(args.command) > args.pos_num:
|
||||
res = args.command[args.pos_num]
|
||||
|
||||
return res
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(
|
||||
|
||||
)
|
||||
parser.add_argument('command', nargs='*')
|
||||
parser.add_argument('pos_num', type=int)
|
||||
args = parser.parse_args()
|
||||
if DEBUG:
|
||||
print(f'{args}', file=sys.stderr)
|
||||
|
||||
cur_arg = get_current_arg(args)
|
||||
if DEBUG:
|
||||
print(f'{cur_arg}', file=sys.stderr)
|
||||
|
||||
if cur_arg is not None and ':' in cur_arg:
|
||||
# В нем уже прописали имя подключения
|
||||
cur_set = get_path_config(cur_arg)
|
||||
|
||||
cmd = make_full_cmd(cur_set)
|
||||
if DEBUG:
|
||||
print(f'{cmd}', file=sys.stderr)
|
||||
obj_list = subprocess.run(cmd, capture_output=True, shell=True, text=True).stdout
|
||||
if DEBUG:
|
||||
print(f'{obj_list}', file=sys.stderr)
|
||||
print(cur_set, file=sys.stderr)
|
||||
|
||||
comp_list = []
|
||||
for i in obj_list.split('\n'):
|
||||
i_res = ''
|
||||
if cur_set['connection_name'] != 'local':
|
||||
i_res = cur_set['connection_name'] + ':'
|
||||
|
||||
i_res += cur_set["work_path"]
|
||||
|
||||
if i_res[-1] != '/':
|
||||
i_res += '/'
|
||||
|
||||
i_res += i
|
||||
comp_list.append(i_res)
|
||||
|
||||
if DEBUG:
|
||||
print(f'{comp_list}', file=sys.stderr)
|
||||
|
||||
print(' '.join(comp_list))
|
||||
else:
|
||||
# Заполняем имя подключения
|
||||
print('/ '.join(get_conf_names()))
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
function _ssh_connector_completion()
|
||||
{
|
||||
latest="${COMP_WORDS[$COMP_CWORD]}"
|
||||
words=$(grep -v -E "\#" $SSH_CONNECTOR_HOST_LIST | awk '{print $1}')
|
||||
COMPREPLY=($(compgen -W "$words" -- $latest))
|
||||
return 0
|
||||
}
|
||||
|
||||
function _scp_path_completition()
|
||||
{
|
||||
latest="${COMP_WORDS[$COMP_CWORD]}"
|
||||
COMPREPLY=$(grep -v -E "\#" $SSH_CONNECTOR_HOST_LIST | awk '{print $1}')
|
||||
return 0
|
||||
}
|
||||
|
||||
complete -F _ssh_connector_completion ssh_helper
|
||||
complete -F _ssh_connector_completion scp_helper
|
||||
Loading…
x
Reference in New Issue
Block a user