This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/cli.py
2020-11-13 13:25:51 +01:00

54 lines
1.7 KiB
Python

import os
import click.testing
import ereuse_utils
import flask.cli
from ereuse_devicehub.config import DevicehubConfig
from ereuse_devicehub.devicehub import Devicehub
class DevicehubGroup(flask.cli.FlaskGroup):
# todo users cannot make cli to use a custom db this way!
CONFIG = DevicehubConfig
def main(self, *args, **kwargs):
# todo this should be taken as an argument for the cli
inventory = os.environ.get('dhi')
if not inventory:
raise ValueError('Please do "export dhi={inventory}"')
self.create_app = self.create_app_factory(inventory)
return super().main(*args, **kwargs)
@classmethod
def create_app_factory(cls, inventory):
return lambda: Devicehub(inventory, config=cls.CONFIG())
def get_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo('Devicehub {}'.format(ereuse_utils.version('ereuse-devicehub')), color=ctx.color)
flask.cli.get_version(ctx, param, value)
@click.option('--version',
help='Devicehub version.',
expose_value=False,
callback=get_version,
is_flag=True,
is_eager=True)
@click.group(cls=DevicehubGroup,
context_settings=Devicehub.cli_context_settings,
add_version_option=False,
help="""Manages the Devicehub of the inventory {}.
Use 'export dhi=xx' to set the inventory that this CLI
manages. For example 'export dhi=db1' and then executing
'dh tag add' adds a tag in the db1 database. Operations
that affect the common database (like creating an user)
are not affected by this.
""".format(os.environ.get('dhi')))
def cli():
pass