Skip to content

Module cli

This module represends CLI to interact with MailCow

main()

CLI manipulating mailcow instance

Source code in mailcow/cli.py
def main():
    '''CLI manipulating mailcow instance'''
    # Allow creation of example configuration file
    # before building argparse menu
    if '--create-example-config' in sys.argv:
        create_cfg(CONF)
        sys.exit(0)

    moo = MailCow()
    # add `--fields` argument before creating menu
    for section, modifiers in moo.endpoints.items():
        if 'get' in modifiers:
            moo.endpoints[section]['get'].update(FIELDS)
    args = menu(moo.endpoints)
    args_as_dict = vars(args)

    debug_mailcow(args.debug)
    debug_msg(f'ArgParse Object: {args}')
    for msg in [f'MailCow Server: {moo.server}',
                f'MailCow Server URL: {moo.url}']:
        debug_msg(msg)

    if args.modifier == 'delete':
        moo.data = moo.deleteRequest(args.section, args.items)

    if args.modifier == 'get':
        moo.data = moo.getRequest(
            prepare_getRequest(**args_as_dict, endpoints=moo.endpoints))

    attrs = build_attributes(**args_as_dict, endpoints=moo.endpoints)
    debug_msg(f'Attributes build: {attrs}')

    if args.modifier == 'add':
        # /api/v1/add/transport/all - The only API endpoint that differ in uri
        if args.section == 'transport':
            section = 'transport/all'
        else:
            section = args.section

        moo.data = moo.addRequest(section, attrs)

    if args.modifier == 'edit':
        # /api/v1/edit/mailq - The only API endpoint that's payload differ
        if args.section == 'mailq' and args.flush:
            moo.data = moo.editRequest(args.section, action='flush')
        else:
            moo.data = moo.editRequest(args.section, args.items, attrs)

    if moo.data:
        if args.yaml:
            print(moo.as_yaml())

        if args.json and not args.yaml:
            print(moo.as_json())

        if args.table and not (args.yaml or args.json):
            tables = moo.as_table(vertical=args.vertical)

            for table in tables:
                fields = parse_fields(
                    args_as_dict.get('fields'),
                    args.vertical)

                if fields:
                    print(table.get_string(fields=fields))
                else:
                    print(table)

Last update: March 22, 2021

Comments