connect command: pre and post
@@ -16,6 +16,8 @@ import argparse
import logging
import datetime
import getpass
+from typing import Optional
+
import requests
import tempfile
import json
@@ -438,13 +440,11 @@ class ConnectCommand(Command):
return value
@staticmethod
- def connect_endpoint(params, endpoint, record, new_data):
- # type: (KeeperParams, str, Record, dict) -> None
- command = record.get('connect:' + endpoint)
- temp_files = []
+ def get_command_string(params, record, template, temp_files, new_data):
+ # type: (KeeperParams, Record, str, list, bool) -> Optional[str]
+ command = template
store_non_shared = False
non_shared = None
- try:
while True:
m = endpoint_parameter_pattern.search(command)
if not m:
@@ -462,13 +462,13 @@ class ConnectCommand(Command):
break
if not attachment:
logging.error('Attachment file \"%s\" not found', file_name)
- return
+ return None
body = ConnectCommand.load_attachment_file(params, attachment, record)
if body:
ConnectCommand.attachment_cache[file_name] = body
if file_name not in ConnectCommand.attachment_cache:
logging.error('Attachment file \"%s\" not found', file_name)
- return
+ return None
body = ConnectCommand.attachment_cache[file_name] # type: bytes
if p.startswith('file:'):
tf = tempfile.NamedTemporaryFile(delete=False)
@@ -517,9 +517,34 @@ class ConnectCommand(Command):
if store_non_shared:
api.store_non_shared_data(params, record.record_uid, non_shared)
+
logging.debug(command)
+ return command
+
+ @staticmethod
+ def connect_endpoint(params, endpoint, record, new_data):
+ # type: (KeeperParams, str, Record, bool) -> None
+ temp_files = []
+ try:
+ command = record.get('connect:' + endpoint + ':pre')
+ if command:
+ command = ConnectCommand.get_command_string(params, record, command, temp_files, new_data)
+ if command:
+ os.system(command)
+
+ command = record.get('connect:' + endpoint)
+ if command:
+ command = ConnectCommand.get_command_string(params, record, command, temp_files, new_data)
+ if command:
logging.info('Connecting to %s...', endpoint)
os.system(command)
+
+ command = record.get('connect:' + endpoint + ':post')
+ if command:
+ command = ConnectCommand.get_command_string(params, record, command, temp_files, new_data)
+ if command:
+ os.system(command)
+
finally:
for file in temp_files:
os.remove(file)
