2019-01-23 15:55:04 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import click.testing
|
2019-02-11 20:34:45 +00:00
|
|
|
import ereuse_utils
|
2019-01-23 15:55:04 +00:00
|
|
|
import flask.cli
|
|
|
|
|
|
|
|
from ereuse_devicehub.config import DevicehubConfig
|
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
|
|
|
|
|
|
|
|
|
|
|
class DevicehubGroup(flask.cli.FlaskGroup):
|
2019-02-04 12:01:36 +00:00
|
|
|
# todo users cannot make cli to use a custom db this way!
|
2019-01-23 15:55:04 +00:00
|
|
|
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)
|
|
|
|
|
2019-02-11 20:34:45 +00:00
|
|
|
@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,
|
2019-06-19 11:35:26 +00:00
|
|
|
help="""Manages the Devicehub of the inventory {}.
|
2019-02-11 20:34:45 +00:00
|
|
|
|
|
|
|
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')))
|
2019-01-23 15:55:04 +00:00
|
|
|
def cli():
|
|
|
|
pass
|