[~] Refactor

This commit is contained in:
Siarhei Siniak 2022-11-07 15:30:40 +03:00
parent 0f1f526078
commit 88e0c6f5ee

@ -565,6 +565,12 @@ def http_server(argv):
def pass_ssh_osx(argv): def pass_ssh_osx(argv):
assert isinstance(argv, list) and all([isinstance(o, str) for o in argv]) assert isinstance(argv, list) and all([isinstance(o, str) for o in argv])
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option(
'--list',
dest='list',
default=False,
action='store_true',
)
parser.add_option( parser.add_option(
'--pass_option', '--pass_option',
dest='pass_option', dest='pass_option',
@ -591,6 +597,13 @@ def pass_ssh_osx(argv):
t1 = options.pass_option t1 = options.pass_option
assert len(t1) > 0 assert len(t1) > 0
reset_gpg_agent = r'''
gpgconf --kill gpg-agent && \
gpgconf --reload gpg-agent
'''
if not options.list:
print( print(
'select on of pass names\n%s' % '\n'.join([ 'select on of pass names\n%s' % '\n'.join([
'%d: %s' % (k, v) '%d: %s' % (k, v)
@ -607,10 +620,22 @@ def pass_ssh_osx(argv):
except: except:
continue continue
reset_gpg_agent = r''' command = r'''
gpgconf --kill gpg-agent; %s
gpgconf --reload gpg-agent; gpg \
''' --pinentry-mode=ask \
-q -u $(cat ~/.password-store/.gpg-id) \
--decrypt \
~/.password-store/%s.gpg && \
echo -n '['$?']' && \
%s
''' % (
reset_gpg_agent,
t1[t3],
reset_gpg_agent,
)
else:
command = 'pass list | less -R'
ssh_command = [ ssh_command = [
'ssh', '-C', 'ssh', '-C',
@ -618,20 +643,7 @@ def pass_ssh_osx(argv):
'-o', 'ServerAliveInterval 1', '-o', 'ServerAliveInterval 1',
*args, *args,
'-t', '-t',
r''' command,
%s
gpg \
--pinentry-mode=ask \
-q -u $(cat ~/.password-store/.gpg-id) \
--decrypt \
~/.password-store/%s.gpg;
echo -n '['$?']';
%s
''' % (
reset_gpg_agent,
t1[t3],
reset_gpg_agent,
),
] ]
if options.debug: if options.debug:
@ -641,6 +653,19 @@ def pass_ssh_osx(argv):
) )
) )
if options.list:
subprocess.check_call(ssh_command)
else:
def cliboard_set(text):
with subprocess.Popen([
'pbcopy',
], stdin=subprocess.PIPE) as p:
p.stdin.write(text.encode('utf-8'))
p.stdin.flush()
p.stdin.close()
p.wait(1)
assert p.poll() == 0
with subprocess.Popen( with subprocess.Popen(
ssh_command, ssh_command,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
@ -687,6 +712,7 @@ def pass_ssh_osx(argv):
assert not last_chunk is None assert not last_chunk is None
assert last_chunk['returncode'] == 0 assert last_chunk['returncode'] == 0
if options.debug: if options.debug:
pprint.pprint(last_chunk['data']) pprint.pprint(last_chunk['data'])
@ -706,20 +732,13 @@ def pass_ssh_osx(argv):
if pos2 == -1: if pos2 == -1:
last_line2 = last_line last_line2 = last_line
else: else:
last_line2 = last_line[pos2 + 1:] last_line2 = last_line[
pos2 + len(pinentry_delimeter):
]
password = last_line2.decode('utf-8').rstrip('\r\n') password = last_line2.decode('utf-8').rstrip('\r\n')
assert not password is None assert not password is None
def cliboard_set(text):
with subprocess.Popen([
'pbcopy',
], stdin=subprocess.PIPE) as p:
p.stdin.write(text.encode('utf-8'))
p.stdin.flush()
p.stdin.close()
p.wait(1)
assert p.poll() == 0
cliboard_set(password) cliboard_set(password)
get_time = lambda : datetime.datetime.now().timestamp() get_time = lambda : datetime.datetime.now().timestamp()