ssh-helper/scp-completion.py
2024-04-11 11:23:51 +03:00

97 lines
2.3 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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()))