mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
email: Parse command arguments with a mutually exclusive group
This commit is contained in:
parent
7166e63b02
commit
df14e74972
@ -23,16 +23,22 @@ def reserved_for_root(fun):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('ipc', nargs=3)
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
arguments = parser.parse_args()
|
group.add_argument('-i', nargs=2, dest='ipc')
|
||||||
subcommand_ipc(arguments)
|
group.add_argument('-t', nargs=1, dest='touch_file')
|
||||||
|
|
||||||
|
adict = vars(parser.parse_args())
|
||||||
|
generator = (kv for kv in adict.items() if kv[1] is not None)
|
||||||
|
subcommand, arguments = next(generator)
|
||||||
|
|
||||||
|
function = globals()['subcommand_' + subcommand]
|
||||||
|
function(*arguments)
|
||||||
|
|
||||||
|
|
||||||
@reserved_for_root
|
@reserved_for_root
|
||||||
def subcommand_ipc(arguments):
|
def subcommand_ipc(module_name, action_name):
|
||||||
import plinth.modules.email_server.audit as audit
|
import plinth.modules.email_server.audit as audit
|
||||||
|
|
||||||
_, module_name, action_name = arguments.ipc
|
|
||||||
# We only run actions defined in the audit module
|
# We only run actions defined in the audit module
|
||||||
if module_name not in audit.__all__:
|
if module_name not in audit.__all__:
|
||||||
logger.critical('Bad module name: %r', module_name)
|
logger.critical('Bad module name: %r', module_name)
|
||||||
@ -47,5 +53,16 @@ def subcommand_ipc(arguments):
|
|||||||
function()
|
function()
|
||||||
|
|
||||||
|
|
||||||
|
def subcommand_touch_file(path):
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
if os.getuid == 0:
|
||||||
|
logger.critical('Do not run as root')
|
||||||
|
sys.exit(EXIT_PERM)
|
||||||
|
|
||||||
|
# mode is influenced by umask
|
||||||
|
pathlib.Path(path).touch(mode=0o660, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@ -64,11 +64,11 @@ def repair():
|
|||||||
POST /audit/ldap/repair
|
POST /audit/ldap/repair
|
||||||
"""
|
"""
|
||||||
logger.debug('Updating postconf: %r', default_config)
|
logger.debug('Updating postconf: %r', default_config)
|
||||||
actions.superuser_run('email_server', ['ipc', 'ldap', 'set_sasl'])
|
actions.superuser_run('email_server', ['-i', 'ldap', 'set_sasl'])
|
||||||
|
|
||||||
logger.debug('Setting up postfix services:\n %r\n %r',
|
logger.debug('Setting up postfix services:\n %r\n %r',
|
||||||
default_submission_options, default_smtps_options)
|
default_submission_options, default_smtps_options)
|
||||||
actions.superuser_run('email_server', ['ipc', 'ldap', 'set_submission'])
|
actions.superuser_run('email_server', ['-i', 'ldap', 'set_submission'])
|
||||||
|
|
||||||
|
|
||||||
def action_set_sasl():
|
def action_set_sasl():
|
||||||
|
|||||||
@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
def repair():
|
def repair():
|
||||||
logger.debug('Updating postconf: %r', milter_config)
|
logger.debug('Updating postconf: %r', milter_config)
|
||||||
actions.superuser_run('email_server', ['ipc', 'spam', 'set_filter'])
|
actions.superuser_run('email_server', ['-i', 'spam', 'set_filter'])
|
||||||
|
|
||||||
|
|
||||||
def action_set_filter():
|
def action_set_filter():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user